From 3e2cf18772909225167919e8f95f9d9c3773815b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Andr=C3=A9s=20Rocha?= Date: Tue, 23 Oct 2012 16:11:07 -0400 Subject: [PATCH] Add some extra console commands for handling user permissions --- .../management/commands/add_to_group.py | 62 +++++++++++++++++++ .../student/management/commands/set_staff.py | 34 ++++++---- .../management/commands/assign_role.py | 36 ++++++++--- 3 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 common/djangoapps/student/management/commands/add_to_group.py diff --git a/common/djangoapps/student/management/commands/add_to_group.py b/common/djangoapps/student/management/commands/add_to_group.py new file mode 100644 index 0000000000..209d25da85 --- /dev/null +++ b/common/djangoapps/student/management/commands/add_to_group.py @@ -0,0 +1,62 @@ +from optparse import make_option + +from django.core.management.base import BaseCommand, CommandError +from django.contrib.auth.models import User, Group + +class Command(BaseCommand): + option_list = BaseCommand.option_list + ( + make_option('--list', + action='store_true', + dest='list', + default=False, + help='List available groups'), + make_option('--create', + action='store_true', + dest='create', + default=False, + help='Create the group if it does not exist'), + make_option('--remove', + action='store_true', + dest='remove', + default=False, + help='Remove the user from the group instead of adding it'), + ) + + args = ' ' + help = 'Add a user to a group' + + def print_groups(self): + print 'Groups available:' + for group in Group.objects.all().distinct(): + print ' ', group.name + + def handle(self, *args, **options): + if options['list']: + self.print_groups() + return + + if len(args) != 2: + raise CommandError('Usage is add_to_group {0}'.format(self.args)) + + name_or_email, group_name = args + + if '@' in name_or_email: + user = User.objects.get(email=name_or_email) + else: + user = User.objects.get(username=name_or_email) + + try: + group = Group.objects.get(name=group_name) + except Group.DoesNotExist: + if options['create']: + group = Group(name=group_name) + group.save() + else: + raise CommandError('Group {} does not exist'.format(group_name)) + + if options['remove']: + user.groups.remove(group) + else: + user.groups.add(group) + + print 'Success!' diff --git a/common/djangoapps/student/management/commands/set_staff.py b/common/djangoapps/student/management/commands/set_staff.py index 5e7df1f585..30d0483f50 100644 --- a/common/djangoapps/student/management/commands/set_staff.py +++ b/common/djangoapps/student/management/commands/set_staff.py @@ -1,37 +1,47 @@ +from optparse import make_option + from django.contrib.auth.models import User from django.core.management.base import BaseCommand, CommandError import re class Command(BaseCommand): + option_list = BaseCommand.option_list + ( + make_option('--unset', + action='store_true', + dest='unset', + default=False, + help='Set is_staff to False instead of True'), + ) - args = '' + args = ' [user|email ...]>' help = """ - This command will set isstaff to true for one or more users. + This command will set is_staff to true for one or more users. Lookup by username or email address, assumes usernames do not look like email addresses. """ - def handle(self, *args, **kwargs): - + def handle(self, *args, **options): if len(args) < 1: - print Command.help - return + raise CommandError('Usage is set_staff {0}'.format(self.args)) for user in args: - if re.match('[^@]+@[^@]+\.[^@]+', user): try: v = User.objects.get(email=user) except: - raise CommandError("User {0} does not exist".format( - user)) + raise CommandError("User {0} does not exist".format(user)) else: try: v = User.objects.get(username=user) except: - raise CommandError("User {0} does not exist".format( - user)) + raise CommandError("User {0} does not exist".format(user)) + + if options['unset']: + v.is_staff = False + else: + v.is_staff = True - v.is_staff = True v.save() + + print 'Success!' diff --git a/lms/djangoapps/django_comment_client/management/commands/assign_role.py b/lms/djangoapps/django_comment_client/management/commands/assign_role.py index 82daa34622..655631008f 100644 --- a/lms/djangoapps/django_comment_client/management/commands/assign_role.py +++ b/lms/djangoapps/django_comment_client/management/commands/assign_role.py @@ -1,18 +1,38 @@ +from optparse import make_option + from django.core.management.base import BaseCommand, CommandError -from django_comment_client.models import Permission, Role +from django_comment_client.models import Role from django.contrib.auth.models import User class Command(BaseCommand): - args = 'user role course_id' - help = 'Assign a role to a user' + option_list = BaseCommand.option_list + ( + make_option('--remove', + action='store_true', + dest='remove', + default=False, + help='Remove the role instead of adding it'), + ) + + args = ' ' + help = 'Assign a discussion forum role to a user ' def handle(self, *args, **options): - role = Role.objects.get(name=args[1], course_id=args[2]) + if len(args) != 3: + raise CommandError('Usage is assign_role {0}'.format(self.args)) - if '@' in args[0]: - user = User.objects.get(email=args[0]) + name_or_email, role, course_id = args + + role = Role.objects.get(name=role, course_id=course_id) + + if '@' in name_or_email: + user = User.objects.get(email=name_or_email) else: - user = User.objects.get(username=args[0]) + user = User.objects.get(username=name_or_email) - user.roles.add(role) \ No newline at end of file + if options['remove']: + user.roles.remove(role) + else: + user.roles.add(role) + + print 'Success!'