- Author:
- givity
- Posted:
- December 30, 2011
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
1) Simply create a "management/commands" folder in one of your INSTALLED_APPS folders. Then add a "reboot.py" file in your "management/commands"
2) In settings.py, you can OPTIONALLY add: PROJECT_NAME = 'Blah' PROJECT_DOMAIN = 'blah.com'
These two settings will be used to create a "Site" object as well as a superuser. If you choose not to use these settings, the reboot command simply reverts to using your DATABASES[default] username as superuser.
3) Execute the command via "python manage.py reboot"
Enjoy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | '''
Created on Dec 28, 2011
@author: givity
The manage.py reboot function deletes all database data
recreates tables via syncdb (populating with fixtures)
'''
import os
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
# http://stackoverflow.com/questions/1007481/how-do-i-replace-whitespaces-with-underscore-and-vice-versa
from django.template.defaultfilters import slugify
# https://docs.djangoproject.com/en/dev/topics/auth/
from django.contrib.auth.models import User
class Command(BaseCommand):
help = 'Deletes all data across all apps listed in settings.INSTALLED_APPS, then recreates the database via syncdb (with population from fixtures)'
def handle(self, *args, **options):
self.stdout.write('beginning reboot...\n')
# reset the db
try:
# use the DATABASES.default.user as the user/pass for recreated superuser, etc.
if 'default' in settings.DATABASES:
username = settings.DATABASES['default']['USER']
self.stdout.write('username found in settings.DATABASES: %s\n' % username)
elif hasattr(settings, 'PROJECT_NAME'):
username = slugify(settings.PROJECT_NAME)
self.stdout.write('username found in settings.PROJECT_NAME: %s\n' % username)
else:
username = 'root'
self.stdout.write('username not found in settings via (DATABASES[default] or PROJECT_NAME), reverting to: %s\n' % username)
# we need a domain name for setting up a root email
if hasattr(settings, 'PROJECT_DOMAIN'):
domain = settings.PROJECT_DOMAIN
self.stdout.write('domain found in settings.PROJECT_DOMAIN: %s\n' % domain)
else:
domain = 'example.com'
self.stdout.write('domain not found in settings via (PROJECT_DOMAIN), reverting to: %s\n' % domain)
# dump db with django-extensions
self.stdout.write('reset_db... ')
os.system('python manage.py reset_db --router=default --noinput')
self.stdout.write('done!\n')
# TODO: do something with the apps?
for app in settings.INSTALLED_APPS:
# split the actual app name from a package.class.app syntax
if '.' in app:
app = app.rsplit('.')[-1]
else:
pass # app does not need to stripped out of package.app.syntax
# sync data and use fixtures to pop db with presets
os.system('python manage.py syncdb --noinput --verbosity 0')
# create the super user
os.system('python manage.py createsuperuser --username=%s --email=%s@%s --noinput' % (username, username, domain))
# set the password
user = User.objects.get(username=username)
user.set_password(username)
user.save()
self.stdout.write('setting superuser username:password [%s:%s]... done!\n' % (username, username))
# create the super user
os.system('python manage.py loaddata user_profile.json')
# run the server!
os.system('python manage.py runserver')
except:
raise CommandError('process terminated.')
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.