summaryrefslogtreecommitdiff
path: root/yaksh/management/commands/create_moderator.py
diff options
context:
space:
mode:
authormaheshgudi2018-03-21 19:14:09 +0530
committermaheshgudi2018-03-21 19:14:09 +0530
commit09ab00808ba9ea288047662e5c7ee2a134f0ac41 (patch)
treeb9ad94f07ae3891295656203c169cb0b03b08d96 /yaksh/management/commands/create_moderator.py
parentefeeaae745f32c4da34f91ca3618d7c3441a5f32 (diff)
parent4b356aa2f6097cd0f46292218f31ded18b631e53 (diff)
downloadonline_test-09ab00808ba9ea288047662e5c7ee2a134f0ac41.tar.gz
online_test-09ab00808ba9ea288047662e5c7ee2a134f0ac41.tar.bz2
online_test-09ab00808ba9ea288047662e5c7ee2a134f0ac41.zip
Merge branch 'master' of https://github.com/fossee/online_test into arrange_options
Diffstat (limited to 'yaksh/management/commands/create_moderator.py')
-rw-r--r--yaksh/management/commands/create_moderator.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/yaksh/management/commands/create_moderator.py b/yaksh/management/commands/create_moderator.py
new file mode 100644
index 0000000..3bbe462
--- /dev/null
+++ b/yaksh/management/commands/create_moderator.py
@@ -0,0 +1,48 @@
+'''
+ This command creates a moderator group and adds users to the moderator group with permissions to add, change and delete
+ the objects in the exam app.
+'''
+
+# django imports
+from django.core.management.base import BaseCommand, CommandError
+from django.contrib.auth.models import User, Group, Permission
+from django.contrib.contenttypes.models import ContentType
+from django.db.utils import IntegrityError
+
+# Yaksh imports
+from yaksh.models import Profile
+
+class Command(BaseCommand):
+ help = 'Adds users to the moderator group'
+
+ def add_arguments(self, parser):
+ # Positional arguments
+ parser.add_argument('usernames', nargs='*', type=str)
+
+ def handle(self, *args, **options):
+ app_label = 'yaksh'
+
+ try:
+ group = Group.objects.get(name='moderator')
+ except Group.DoesNotExist:
+ group = Group(name='moderator')
+ group.save()
+ # Get the models for the given app
+ content_types = ContentType.objects.filter(app_label=app_label)
+ # Get list of permissions for the models
+ permission_list = Permission.objects.filter(content_type__in=content_types)
+ group.permissions.add(*permission_list)
+ group.save()
+ self.stdout.write('Moderator group added successfully')
+
+ if options['usernames']:
+ for uname in options['usernames']:
+ try:
+ user = User.objects.get(username=uname)
+ except User.DoesNotExist:
+ raise CommandError('User "{0}" does not exist'.format(uname))
+ if user in group.user_set.all():
+ self.stdout.write('User "{0}" is already a Moderator'.format(uname))
+ else:
+ group.user_set.add(user)
+ self.stdout.write('Successfully added User "{0}" to Moderator group'.format(uname))