summaryrefslogtreecommitdiff
path: root/yaksh/management/commands/create_mod_group.py
diff options
context:
space:
mode:
authorankitjavalkar2018-03-01 16:00:58 +0530
committerankitjavalkar2018-03-05 15:58:47 +0530
commitcc263e33660f9070309187509e1d177bda4e49f2 (patch)
tree17e4098bdd9f0977b3390fd971d6a4be5d819298 /yaksh/management/commands/create_mod_group.py
parent438f8657021981fc7b2e5adbacc13eb332a2e6d3 (diff)
downloadonline_test-cc263e33660f9070309187509e1d177bda4e49f2.tar.gz
online_test-cc263e33660f9070309187509e1d177bda4e49f2.tar.bz2
online_test-cc263e33660f9070309187509e1d177bda4e49f2.zip
Add a management command to add users to moderator group
Diffstat (limited to 'yaksh/management/commands/create_mod_group.py')
-rw-r--r--yaksh/management/commands/create_mod_group.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/yaksh/management/commands/create_mod_group.py b/yaksh/management/commands/create_mod_group.py
new file mode 100644
index 0000000..0bf08df
--- /dev/null
+++ b/yaksh/management/commands/create_mod_group.py
@@ -0,0 +1,30 @@
+'''
+ This command creates a 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 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')