summaryrefslogtreecommitdiff
path: root/yaksh/management/commands
diff options
context:
space:
mode:
authorPrabhu Ramachandran2018-03-21 18:21:05 +0530
committerGitHub2018-03-21 18:21:05 +0530
commitaa64e8db6456260bf6f29086eff5012c970e14f3 (patch)
treecac947a2d739c336ae68c5c0d7c939d6d01a336e /yaksh/management/commands
parent3226e8f455d51967c29171d601783ea4de88306a (diff)
parent62229427b651a52a2fc4d24d2a0bf764553277c0 (diff)
downloadonline_test-aa64e8db6456260bf6f29086eff5012c970e14f3.tar.gz
online_test-aa64e8db6456260bf6f29086eff5012c970e14f3.tar.bz2
online_test-aa64e8db6456260bf6f29086eff5012c970e14f3.zip
Merge pull request #452 from ankitjavalkar/change-mgmt-command
Add a management command to add users to moderator group
Diffstat (limited to 'yaksh/management/commands')
-rw-r--r--yaksh/management/commands/add_group.py32
-rw-r--r--yaksh/management/commands/create_moderator.py48
2 files changed, 48 insertions, 32 deletions
diff --git a/yaksh/management/commands/add_group.py b/yaksh/management/commands/add_group.py
deleted file mode 100644
index 624ff3c..0000000
--- a/yaksh/management/commands/add_group.py
+++ /dev/null
@@ -1,32 +0,0 @@
-'''
- This command adds moderator group with permissions to add, change and delete
- the objects in the exam app.
- We can modify this command to add more groups by providing arguments.
- Arguments like group-name, app-name can be passed.
-'''
-
-# django imports
-from django.core.management.base import BaseCommand, CommandError
-from django.contrib.auth.models import Group, Permission
-from django.contrib.contenttypes.models import ContentType
-from django.db.utils import IntegrityError
-
-class Command(BaseCommand):
- help = 'Adds the moderator group'
-
- def handle(self, *args, **options):
- app_label = 'yaksh'
- group = Group(name='moderator')
- try:
- group.save()
- except IntegrityError:
- raise CommandError("The group already exits")
- else:
- # 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')
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))