diff options
author | prathamesh | 2014-07-23 17:48:43 +0530 |
---|---|---|
committer | prathamesh | 2014-07-23 17:48:43 +0530 |
commit | 24676bc8b02f94b6f700ccf25a49015134bfd0fb (patch) | |
tree | 173f7882af371bc7aa0ea98d71a4c5a150886611 /testapp/exam/management | |
parent | ff2d8ef28e2c49be940fd87b8364162efc0c4351 (diff) | |
parent | 8abd57cf58a432ff173ad757d8a9c707013f96b9 (diff) | |
download | online_test-24676bc8b02f94b6f700ccf25a49015134bfd0fb.tar.gz online_test-24676bc8b02f94b6f700ccf25a49015134bfd0fb.tar.bz2 online_test-24676bc8b02f94b6f700ccf25a49015134bfd0fb.zip |
Merge branch 'initial_fixtures' into reusable_app
Diffstat (limited to 'testapp/exam/management')
-rw-r--r-- | testapp/exam/management/commands/add_group.py | 33 |
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..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') |