-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateadminuser.py
More file actions
33 lines (27 loc) · 1.2 KB
/
createadminuser.py
File metadata and controls
33 lines (27 loc) · 1.2 KB
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
from django.contrib.auth.management.commands import createsuperuser
from django.core.management import CommandError
class Command(createsuperuser.Command):
help = 'Create a superuser, and allow password to be provided'
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--password', dest='password', default=None,
help='Specifies the password for the superuser.',
)
def handle(self, *args, **options):
password = options.get('password')
username = options.get('username')
database = options.get('database')
if password and not username:
raise CommandError("--username is required if specifying --password")
user = self.UserModel._default_manager.db_manager(database).\
filter(username=username)
if user:
self.stdout.write('That username is already created')
return
super(Command, self).handle(*args, **options)
if password:
user = self.UserModel._default_manager.db_manager(database).\
get(username=username)
user.set_password(password)
user.save()