Guest User

Untitled

a guest
Aug 3rd, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # This script was made by a master bash hacker, hence to following 2 settings
  4. set -o errexit
  5. set -o nounset
  6.  
  7. cat << EOF
  8. django-bootstrap -- generates a complete django project
  9.  
  10. usage: django-bootstrap [app_name] [project_name] [virtualenv_path/name] [admin_name (default=admin)] [admin_password (default=admin)]
  11.  
  12. A Django "project" is a collection of 1 or more "apps", and should go into a "virtualenv".
  13. This script will create a directory structure for you like so: virtualenv/project/app/...
  14. EOF
  15.  
  16. [[ ${#@} -ge 1 ]] && app=$1 || read -p "What would you like to name your app? " app;
  17. [[ ${#@} -ge 2 ]] && project=$2 || read -p "What would you like to name your project? (It cannot be $app.) " project;
  18. [[ ${#@} -ge 3 ]] && virtualenv=$3 || read -p "What would you like to name your virtualenv? [$project] " virtualenv;
  19. [[ ${#@} -ge 4 ]] && superusername=$4 || superusername=admin
  20. [[ ${#@} -ge 5 ]] && superuserpass=$5 || superuserpass=admin
  21. [[ $app == $project ]] && echo "Error: You cannot create an app with the same name ('$project') as your project." && exit
  22.  
  23. # prepare the virtualenv
  24. virtualenv $virtualenv
  25. cd $virtualenv
  26. virtualenv --relocatable .
  27. echo "export DJANGO_SETTINGS_MODULE=settings" >> bin/activate
  28.  
  29. # install Django
  30. set +o nounset # the activate script has a bug that makes it not safe to use with nounset
  31. source bin/activate
  32. set -o nounset
  33. pip install django
  34.  
  35. # create the Django project and app
  36. django-admin.py startproject $project
  37. (cd $project && django-admin.py startapp $app)
  38. patch -p0 << EOF
  39. --- $project/settings.py 2011-06-03 22:12:29.000000000 -0400
  40. +++ $project/settings.py 2011-06-03 22:17:41.000000000 -0400
  41. @@ -2,6 +2,8 @@
  42.  
  43. DEBUG = True
  44. TEMPLATE_DEBUG = DEBUG
  45. +import os
  46. +PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
  47.  
  48. ADMINS = (
  49. # ('Your Name', 'your_email@example.com'),
  50. @@ -11,8 +13,8 @@
  51.  
  52. DATABASES = {
  53. 'default': {
  54. - 'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
  55. - 'NAME': '', # Or path to database file if using sqlite3.
  56. + 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
  57. + 'NAME': os.path.join(PROJECT_DIR, 'data.sqlite3'), # Or path to database file if using sqlite3.
  58. 'USER': '', # Not used with sqlite3.
  59. 'PASSWORD': '', # Not used with sqlite3.
  60. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
  61. @@ -116,9 +118,10 @@
  62. 'django.contrib.messages',
  63. 'django.contrib.staticfiles',
  64. # Uncomment the next line to enable the admin:
  65. - # 'django.contrib.admin',
  66. + 'django.contrib.admin',
  67. # Uncomment the next line to enable admin documentation:
  68. # 'django.contrib.admindocs',
  69. + '$app',
  70. )
  71.  
  72. # A sample logging configuration. The only tangible logging
  73. --- $project/urls.py 2011-06-03 23:27:47.000000000 -0400
  74. +++ $project/urls.py 2011-06-03 23:44:31.000000000 -0400
  75. @@ -1,8 +1,8 @@
  76. from django.conf.urls.defaults import patterns, include, url
  77.  
  78. # Uncomment the next two lines to enable the admin:
  79. -# from django.contrib import admin
  80. -# admin.autodiscover()
  81. +from django.contrib import admin
  82. +admin.autodiscover()
  83.  
  84. urlpatterns = patterns('',
  85. # Examples:
  86. @@ -13,5 +13,5 @@
  87. # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
  88.  
  89. # Uncomment the next line to enable the admin:
  90. - # url(r'^admin/', include(admin.site.urls)),
  91. + url(r'^admin/', include(admin.site.urls)),
  92. )
  93. EOF
  94.  
  95. # add your project path the Python sys.path the your code can be found
  96. echo "../../../$project" > $(p=(lib/python*/site-packages); echo ${p[0]})/$project.pth
  97.  
  98. # prepare the database
  99. django-admin.py syncdb --noinput
  100. echo "creating django superuser with login:$superusername and password:$superuserpass...";
  101. django-admin.py shell << EOF > /dev/null 2>&1
  102. from django.contrib.auth.models import User
  103. (admin,created) = User.objects.get_or_create(username='$superusername')
  104. admin.is_staff=True
  105. admin.is_active=True
  106. admin.is_superuser=True
  107. admin.set_password('$superuserpass')
  108. admin.save()
  109. EOF
  110.  
  111. cat << EOF
  112. Your project has been setup at $(cd $project && pwd) and your SQLite3 database has been created.
  113. You can get started by running:
  114. cd $virtualenv/$project; source ../bin/activate; django-admin.py runserver
  115.  
  116. Tip: As you develop you may find yourself needing to blow away all of the data in your app(s) and keep the django (auth.user, etc.) tables. Here's a oneliner for that:
  117. echo '.tables' | sqlite3 data.sqlite3 | xargs -n1 | grep -vE '^(auth|django)' | while read table; do echo "drop table \$table;" | sqlite3 data.sqlite3; done; django-admin.py syncdb
  118. EOF
Add Comment
Please, Sign In to add comment