From cc263e33660f9070309187509e1d177bda4e49f2 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 1 Mar 2018 16:00:58 +0530 Subject: Add a management command to add users to moderator group --- yaksh/management/commands/add_group.py | 32 ----------------------- yaksh/management/commands/add_moderator.py | 37 +++++++++++++++++++++++++++ yaksh/management/commands/create_mod_group.py | 30 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 32 deletions(-) delete mode 100644 yaksh/management/commands/add_group.py create mode 100644 yaksh/management/commands/add_moderator.py create mode 100644 yaksh/management/commands/create_mod_group.py (limited to 'yaksh') 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/add_moderator.py b/yaksh/management/commands/add_moderator.py new file mode 100644 index 0000000..461c03f --- /dev/null +++ b/yaksh/management/commands/add_moderator.py @@ -0,0 +1,37 @@ +''' + This command 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): + try: + group = Group.objects.get(name='moderator') + except Group.DoesNotExist: + raise CommandError('Moderator Group does not exist') + + 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)) + continue + group.user_set.add(user) + self.stdout.write('Successfully added User "{0}" to Moderator group'.format(uname)) 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') -- cgit From db267f32c158f4311c017ad3febe049a680ed5c0 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 5 Mar 2018 19:24:49 +0530 Subject: Add an else statement instead of continue keyword --- yaksh/management/commands/add_moderator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'yaksh') diff --git a/yaksh/management/commands/add_moderator.py b/yaksh/management/commands/add_moderator.py index 461c03f..5e63285 100644 --- a/yaksh/management/commands/add_moderator.py +++ b/yaksh/management/commands/add_moderator.py @@ -32,6 +32,6 @@ class Command(BaseCommand): 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)) - continue - group.user_set.add(user) - self.stdout.write('Successfully added User "{0}" to Moderator group'.format(uname)) + else: + group.user_set.add(user) + self.stdout.write('Successfully added User "{0}" to Moderator group'.format(uname)) -- cgit From 62229427b651a52a2fc4d24d2a0bf764553277c0 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 14 Mar 2018 12:32:37 +0530 Subject: Add one command to create moderator group and add users to the group --- yaksh/management/commands/add_moderator.py | 37 --------------------- yaksh/management/commands/create_mod_group.py | 30 ----------------- yaksh/management/commands/create_moderator.py | 48 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 67 deletions(-) delete mode 100644 yaksh/management/commands/add_moderator.py delete mode 100644 yaksh/management/commands/create_mod_group.py create mode 100644 yaksh/management/commands/create_moderator.py (limited to 'yaksh') diff --git a/yaksh/management/commands/add_moderator.py b/yaksh/management/commands/add_moderator.py deleted file mode 100644 index 5e63285..0000000 --- a/yaksh/management/commands/add_moderator.py +++ /dev/null @@ -1,37 +0,0 @@ -''' - This command 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): - try: - group = Group.objects.get(name='moderator') - except Group.DoesNotExist: - raise CommandError('Moderator Group does not exist') - - 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)) diff --git a/yaksh/management/commands/create_mod_group.py b/yaksh/management/commands/create_mod_group.py deleted file mode 100644 index 0bf08df..0000000 --- a/yaksh/management/commands/create_mod_group.py +++ /dev/null @@ -1,30 +0,0 @@ -''' - 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') 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)) -- cgit