From 8abd57cf58a432ff173ad757d8a9c707013f96b9 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Wed, 23 Jul 2014 16:48:42 +0530 Subject: Command 'add_group' created for adding the moderator group --- testapp/exam/fixtures/initial_fixture.json | 29 ----------------------- testapp/exam/management/commands/add_group.py | 33 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 29 deletions(-) delete mode 100644 testapp/exam/fixtures/initial_fixture.json create mode 100644 testapp/exam/management/commands/add_group.py (limited to 'testapp') diff --git a/testapp/exam/fixtures/initial_fixture.json b/testapp/exam/fixtures/initial_fixture.json deleted file mode 100644 index e1b49b7..0000000 --- a/testapp/exam/fixtures/initial_fixture.json +++ /dev/null @@ -1,29 +0,0 @@ -[ - { - "fields": { - "name": "moderator", - "permissions": [ - 31, - 37, - 43, - 32, - 38, - 44, - 33, - 39, - 45, - 34, - 40, - 46, - 35, - 41, - 47, - 36, - 42, - 48 - ] - }, - "model": "auth.group", - "pk": 1 - } -] diff --git a/testapp/exam/management/commands/add_group.py b/testapp/exam/management/commands/add_group.py new file mode 100644 index 0000000..e6ac888 --- /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') -- cgit