manage.py reboot

 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

  1. Link Media Command by elwaywitvac 5 years, 7 months ago
  2. UnitTesting without create/destroy database by crucialfelix 5 years, 2 months ago
  3. freshdb management command by tomzee 6 years, 4 months ago
  4. backupdb command by msaelices 5 years, 10 months ago
  5. Add example contact with BaseCommand by magik_cypress 1 year, 8 months ago

Comments

(Forgotten your password?)