|
|
|
@ -1,11 +1,9 @@
@@ -1,11 +1,9 @@
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError |
|
|
|
|
from django.contrib.auth.models import User |
|
|
|
|
from django.contrib.auth.models import Group |
|
|
|
|
|
|
|
|
|
from django.db.utils import IntegrityError |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#from polls.models import Question as Poll |
|
|
|
|
|
|
|
|
|
class Command(BaseCommand): |
|
|
|
|
help = 'Setting up admin and tusd users for VRE' |
|
|
|
|
|
|
|
|
@ -14,27 +12,35 @@ class Command(BaseCommand):
@@ -14,27 +12,35 @@ class Command(BaseCommand):
|
|
|
|
|
parser.add_argument('password', help='Password') |
|
|
|
|
parser.add_argument('email', help='Email address') |
|
|
|
|
|
|
|
|
|
parser.add_argument('--nokey', help='Skip token key') |
|
|
|
|
parser.add_argument('--key', help='Token key') |
|
|
|
|
parser.add_argument('--secret', help='Token secret') |
|
|
|
|
parser.add_argument('--group', help='Groupname to add the user to') |
|
|
|
|
|
|
|
|
|
def handle(self, *args, **options): |
|
|
|
|
try: |
|
|
|
|
user = User.objects.create_superuser(username=options['username'], password=options['password'], email=options['email']) |
|
|
|
|
self.stdout.write(self.style.SUCCESS('Successfully created user "%s"' % options['username'])) |
|
|
|
|
self.stdout.write(self.style.SUCCESS('Successfully created user "%s"' % (options['username'],))) |
|
|
|
|
|
|
|
|
|
if options['key'] is not None and options['secret'] is not None: |
|
|
|
|
user.token.key=options['key'] |
|
|
|
|
user.token.secret=options['secret'] |
|
|
|
|
user.token.save() |
|
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS('Successfully created token for user "%s"' % options['username'])) |
|
|
|
|
self.stdout.write(self.style.SUCCESS('Successfully created token for user "%s"' % (options['username'],))) |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
elif options['nokey'] is not None: |
|
|
|
|
# We do not want an token for the admin |
|
|
|
|
user.token.delete() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except IntegrityError as ex: |
|
|
|
|
if 'unique constraint' in str(ex).lower(): |
|
|
|
|
self.stdout.write(self.style.WARNING('User "%s" already exists' % options['username'])) |
|
|
|
|
self.stdout.write(self.style.WARNING('User "%s" already exists' % (options['username'],))) |
|
|
|
|
else: |
|
|
|
|
raise CommandError('Could not create user "%s": %s' % (options['username'],ex)) |
|
|
|
|
raise CommandError('Could not create user "%s": %s' % (options['username'],ex)) |
|
|
|
|
|
|
|
|
|
if options['group'] is not None: |
|
|
|
|
user = User.objects.get(username=options['username']) |
|
|
|
|
new_group, created = Group.objects.get_or_create(name=options['group']) |
|
|
|
|
user.groups.add(new_group) |
|
|
|
|
self.stdout.write(self.style.SUCCESS('Added user "%s" to group "%s"' % (options['username'],options['group']))) |