summaryrefslogtreecommitdiff
path: root/testapp/exam
diff options
context:
space:
mode:
authorPrabhu Ramachandran2014-07-26 18:50:53 +0530
committerPrabhu Ramachandran2014-07-26 18:50:53 +0530
commitec33daea9185bb783c5dcd4f8daddc135b25a0fa (patch)
treee6d6bdc32e2f4930225800ce50887eb76af75d6c /testapp/exam
parentbefad018f2e59389e92eaa109c6968bea30d06f9 (diff)
parent0e6c82690650c61049ae8641a2801f0fcbc9c25c (diff)
downloadonline_test-ec33daea9185bb783c5dcd4f8daddc135b25a0fa.tar.gz
online_test-ec33daea9185bb783c5dcd4f8daddc135b25a0fa.tar.bz2
online_test-ec33daea9185bb783c5dcd4f8daddc135b25a0fa.zip
Merge pull request #33 from prathamesh920/initial_fixtures
Added initial fixture.
Diffstat (limited to 'testapp/exam')
-rw-r--r--testapp/exam/management/commands/add_group.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/testapp/exam/management/commands/add_group.py b/testapp/exam/management/commands/add_group.py
new file mode 100644
index 0000000..76cc655
--- /dev/null
+++ b/testapp/exam/management/commands/add_group.py
@@ -0,0 +1,33 @@
+'''
+ 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 = 'exam'
+ 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)
+ # Get list of permissions for the models
+ permission_list = Permission.objects.filter(content_type=content_types)
+ for permission in permission_list:
+ group.permissions.add(permission)
+ group.save()
+
+ self.stdout.write('Moderator group added successfully')