Unverified Commit c4c1e7eb by Troy Sankey Committed by GitHub

Merge pull request #16467 from edx/pwnage101/django_comment_client_mgmt_cleanup

django_comment_client management command cleanup for Django 1.11
parents ae0854b0 9567649d
from optparse import make_option from __future__ import print_function
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from django_comment_common.models import Role from django_comment_common.models import Role
class Command(BaseCommand): class Command(BaseCommand):
option_list = BaseCommand.option_list + ( help = 'Assign a discussion forum role to a user.'
make_option('--remove',
action='store_true', def add_arguments(self, parser):
dest='remove', parser.add_argument('name_or_email',
default=False, help='username or email address of the user to assign a role')
help='Remove the role instead of adding it'), parser.add_argument('role',
) help='the role to which the user will be assigned')
parser.add_argument('course_id',
args = '<user|email> <role> <course_id>' help='the edx course_id')
help = 'Assign a discussion forum role to a user ' parser.add_argument('--remove',
action='store_true',
help='remove the role instead of adding/assigning it')
def handle(self, *args, **options): def handle(self, *args, **options):
if len(args) != 3: name_or_email = options['name_or_email']
raise CommandError('Usage is assign_role {0}'.format(self.args)) role = options['role']
course_id = options['course_id']
name_or_email, role, course_id = args
role = Role.objects.get(name=role, course_id=course_id) role = Role.objects.get(name=role, course_id=course_id)
...@@ -36,4 +36,4 @@ class Command(BaseCommand): ...@@ -36,4 +36,4 @@ class Command(BaseCommand):
else: else:
user.roles.add(role) user.roles.add(role)
print 'Success!' print('Success!')
...@@ -4,26 +4,26 @@ This must be run only after seed_permissions_roles.py! ...@@ -4,26 +4,26 @@ This must be run only after seed_permissions_roles.py!
Creates default roles for all users in the provided course. Just runs through Creates default roles for all users in the provided course. Just runs through
Enrollments. Enrollments.
""" """
from django.core.management.base import BaseCommand, CommandError from __future__ import print_function
from django.core.management.base import BaseCommand
from django_comment_common.models import assign_default_role_on_enrollment from django_comment_common.models import assign_default_role_on_enrollment
from student.models import CourseEnrollment from student.models import CourseEnrollment
class Command(BaseCommand): class Command(BaseCommand):
args = 'course_id' help = 'Add roles for all users in a course.'
help = 'Add roles for all users in a course'
def add_arguments(self, parser):
parser.add_argument('course_id',
help='the edx course_id')
def handle(self, *args, **options): def handle(self, *args, **options):
if len(args) == 0: course_id = options['course_id']
raise CommandError("Please provide a course id")
if len(args) > 1:
raise CommandError("Too many arguments")
course_id = args[0]
print "Updated roles for ", print('Updated roles for ', end=' ')
for i, enrollment in enumerate(CourseEnrollment.objects.filter(course_id=course_id, is_active=1), start=1): for i, enrollment in enumerate(CourseEnrollment.objects.filter(course_id=course_id, is_active=1), start=1):
assign_default_role_on_enrollment(None, enrollment) assign_default_role_on_enrollment(None, enrollment)
if i % 1000 == 0: if i % 1000 == 0:
print "{0}...".format(i), print('{0}...'.format(i), end=' ')
print print()
...@@ -4,23 +4,20 @@ This must be run only after seed_permissions_roles.py! ...@@ -4,23 +4,20 @@ This must be run only after seed_permissions_roles.py!
Creates default roles for all users currently in the database. Just runs through Creates default roles for all users currently in the database. Just runs through
Enrollments. Enrollments.
""" """
from django.core.management.base import BaseCommand, CommandError from __future__ import print_function
from django.core.management.base import BaseCommand
from django_comment_common.models import assign_default_role_on_enrollment from django_comment_common.models import assign_default_role_on_enrollment
from student.models import CourseEnrollment from student.models import CourseEnrollment
class Command(BaseCommand): class Command(BaseCommand):
args = 'course_id' help = 'Seed default permisssions and roles.'
help = 'Seed default permisssions and roles'
def handle(self, *args, **options): def handle(self, *args, **options):
if len(args) != 0: print('Updated roles for ', end=' ')
raise CommandError("This Command takes no arguments")
print "Updated roles for ",
for i, enrollment in enumerate(CourseEnrollment.objects.filter(is_active=1), start=1): for i, enrollment in enumerate(CourseEnrollment.objects.filter(is_active=1), start=1):
assign_default_role_on_enrollment(None, enrollment) assign_default_role_on_enrollment(None, enrollment)
if i % 1000 == 0: if i % 1000 == 0:
print "{0}...".format(i), print('{0}...'.format(i), end=' ')
print print()
from courseware.courses import get_course
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from courseware.courses import get_course
class Command(BaseCommand): class Command(BaseCommand):
args = "<course_id>" help = 'Write a discussion link for a given course on standard output.'
def add_arguments(self, parser):
parser.add_argument('course_id',
help='course for which to write a discussion link')
def handle(self, *args, **options): def handle(self, *args, **options):
if not args: course_id = options['course_id']
raise CommandError("Course id not specified")
if len(args) > 1:
raise CommandError("Only one course id may be specifiied")
course_id = args[0]
course_key = CourseKey.from_string(course_id) course_key = CourseKey.from_string(course_id)
course = get_course(course_key) course = get_course(course_key)
if not course: if not course:
raise CommandError("Invalid course id: {}".format(course_id)) raise CommandError('Invalid course id: {}'.format(course_id))
if course.discussion_link: if course.discussion_link:
self.stdout.write(course.discussion_link) self.stdout.write(course.discussion_link)
""" """
Reload forum (comment client) users from existing users. Reload forum (comment client) users from existing users.
""" """
from django.contrib.auth.models import User from __future__ import print_function
from django.core.management.base import BaseCommand
import lms.lib.comment_client as cc import lms.lib.comment_client as cc
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
class Command(BaseCommand): class Command(BaseCommand):
help = 'Reload forum (comment client) users from existing users' help = 'Reload forum (comment client) users from existing users.'
def add_arguments(self, parser):
parser.add_argument('usernames',
nargs='*',
metavar='username',
help='zero or more usernames (zero implies all users)')
def adduser(self, user): def adduser(self, user):
print user print(user)
try: try:
cc_user = cc.User.from_django_user(user) cc_user = cc.User.from_django_user(user)
cc_user.save() cc_user.save()
except Exception as err: except Exception as err:
print "update user info to discussion failed for user with id: %s, error=%s" % (user, str(err)) print('update user info to discussion failed for user with id: {}, error={}'.format(user, str(err)))
def handle(self, *args, **options): def handle(self, *args, **options):
if len(args) != 0: if len(options['usernames']) >= 1:
uset = [User.objects.get(username=x) for x in args] user_list = User.objects.filter(username__in=options['usernames'])
else: else:
uset = User.objects.all() user_list = User.objects.all()
for user in uset: for user in user_list:
self.adduser(user) self.adduser(user)
""" """
Management command to seed default permissions and roles. Management command to seed default permissions and roles.
""" """
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
from opaque_keys.edx.keys import CourseKey
from django_comment_common.utils import seed_permissions_roles from django_comment_common.utils import seed_permissions_roles
from opaque_keys.edx.keys import CourseKey
class Command(BaseCommand): class Command(BaseCommand):
args = 'course_id' help = 'Seed default permisssions and roles.'
help = 'Seed default permisssions and roles'
def add_arguments(self, parser):
parser.add_argument('course_id',
help='the edx course_id')
def handle(self, *args, **options): def handle(self, *args, **options):
if len(args) == 0: course_id = options['course_id']
raise CommandError("Please provide a course id")
if len(args) > 1:
raise CommandError("Too many arguments")
course_id = CourseKey.from_string(args[0])
seed_permissions_roles(course_id) course_key = CourseKey.from_string(course_id)
seed_permissions_roles(course_key)
from __future__ import print_function
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand
class Command(BaseCommand): class Command(BaseCommand):
args = 'user' help = "Show a user's roles and permissions."
help = "Show a user's roles and permissions"
def add_arguments(self, parser):
parser.add_argument('email_or_username',
help='the email or username of the user')
def handle(self, *args, **options): def handle(self, *args, **options):
print args email_or_username = options['email_or_username']
if len(args) != 1:
raise CommandError("The number of arguments does not match. ")
try: try:
if '@' in args[0]: if '@' in email_or_username:
user = User.objects.get(email=args[0]) user = User.objects.get(email=email_or_username)
else: else:
user = User.objects.get(username=args[0]) user = User.objects.get(username=email_or_username)
except User.DoesNotExist: except User.DoesNotExist:
print "User %s does not exist. " % args[0] print('User {} does not exist. '.format(email_or_username))
print "Available users: " print('Available users: ')
print User.objects.all() print(User.objects.all())
return return
roles = user.roles.all() roles = user.roles.all()
print "%s has %d roles:" % (user, len(roles)) print('{} has %d roles:'.format(user, len(roles)))
for role in roles: for role in roles:
print "\t%s" % role print('\t{}'.format(role))
for role in roles: for role in roles:
print "%s has permissions: " % role print('{} has permissions: '.format(role))
print role.permissions.all() print(role.permissions.all())
...@@ -3,17 +3,16 @@ One-off script to sync all user information to the ...@@ -3,17 +3,16 @@ One-off script to sync all user information to the
discussion service (later info will be synced automatically) discussion service (later info will be synced automatically)
""" """
import lms.lib.comment_client as cc
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
import lms.lib.comment_client as cc
class Command(BaseCommand): class Command(BaseCommand):
""" """
Management command for adding all users to the discussion service. Management command for adding all users to the discussion service.
""" """
help = 'Sync all user ids, usernames, and emails to the discussion service' help = 'Sync all user ids, usernames, and emails to the discussion service.'
def handle(self, *args, **options): def handle(self, *args, **options):
for user in User.objects.all().iterator(): for user in User.objects.all().iterator():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment