'''
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.')
Comments
[HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] Other About blog [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] About blog [HTML_REMOVED]
#
[HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] Other About blog [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] About blog [HTML_REMOVED]
#
[HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] Other About blog [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] About blog [HTML_REMOVED]
#
[HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] Other About blog [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] About blog [HTML_REMOVED]
#
[HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] Other About blog [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] About blog [HTML_REMOVED]
#