Commit 1f849e44 by Calen Pennington

Fix management commands

parent 9dc48c00
...@@ -13,7 +13,9 @@ import csv ...@@ -13,7 +13,9 @@ import csv
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, CommandError
from opaque_keys import InvalidKeyError
from student.models import anonymous_id_for_user from student.models import anonymous_id_for_user
from xmodule.modulestore.locations import SlashSeparatedCourseKey
class Command(BaseCommand): class Command(BaseCommand):
...@@ -35,16 +37,16 @@ class Command(BaseCommand): ...@@ -35,16 +37,16 @@ class Command(BaseCommand):
raise CommandError("Usage: unique_id_mapping %s" % raise CommandError("Usage: unique_id_mapping %s" %
" ".join(("<%s>" % arg for arg in Command.args))) " ".join(("<%s>" % arg for arg in Command.args)))
course_id = args[0] course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0])
# Generate the output filename from the course ID. # Generate the output filename from the course ID.
# Change slashes to dashes first, and then append .csv extension. # Change slashes to dashes first, and then append .csv extension.
output_filename = course_id.replace('/', '-') + ".csv" output_filename = course_key.to_deprecated_string().replace('/', '-') + ".csv"
# Figure out which students are enrolled in the course # Figure out which students are enrolled in the course
students = User.objects.filter(courseenrollment__course_id=course_id) students = User.objects.filter(courseenrollment__course_id=course_key)
if len(students) == 0: if len(students) == 0:
self.stdout.write("No students enrolled in %s" % course_id) self.stdout.write("No students enrolled in %s" % course_key.to_deprecated_string())
return return
# Write mapping to output file in CSV format with a simple header # Write mapping to output file in CSV format with a simple header
...@@ -60,7 +62,7 @@ class Command(BaseCommand): ...@@ -60,7 +62,7 @@ class Command(BaseCommand):
csv_writer.writerow(( csv_writer.writerow((
student.id, student.id,
anonymous_id_for_user(student, None), anonymous_id_for_user(student, None),
anonymous_id_for_user(student, course_id) anonymous_id_for_user(student, course_key)
)) ))
except IOError: except IOError:
raise CommandError("Error writing to file: %s" % output_filename) raise CommandError("Error writing to file: %s" % output_filename)
......
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError
from optparse import make_option from optparse import make_option
from student.models import CourseEnrollment, User from student.models import CourseEnrollment, User
from xmodule.modulestore.keys import CourseKey
from xmodule.modulestore.locations import SlashSeparatedCourseKey
class Command(BaseCommand): class Command(BaseCommand):
...@@ -55,8 +59,14 @@ class Command(BaseCommand): ...@@ -55,8 +59,14 @@ class Command(BaseCommand):
raise CommandError("You must specify a course id for this command") raise CommandError("You must specify a course id for this command")
if not options['from_mode'] or not options['to_mode']: if not options['from_mode'] or not options['to_mode']:
raise CommandError('You must specify a "to" and "from" mode as parameters') raise CommandError('You must specify a "to" and "from" mode as parameters')
try:
course_key = CourseKey.from_string(options['course_id'])
except InvalidKeyError:
course_key = SlashSeparatedCourseKey.from_deprecated_string(options['course_id'])
filter_args = dict( filter_args = dict(
course_id=options['course_id'], course_id=course_key,
mode=options['from_mode'] mode=options['from_mode']
) )
if options['user']: if options['user']:
......
...@@ -7,12 +7,12 @@ from student.models import CourseEnrollment ...@@ -7,12 +7,12 @@ from student.models import CourseEnrollment
from student.views import _do_create_account, get_random_post_override from student.views import _do_create_account, get_random_post_override
def create(n, course_id): def create(n, course_key):
"""Create n users, enrolling them in course_id if it's not None""" """Create n users, enrolling them in course_key if it's not None"""
for i in range(n): for i in range(n):
(user, user_profile, _) = _do_create_account(get_random_post_override()) (user, user_profile, _) = _do_create_account(get_random_post_override())
if course_id is not None: if course_key is not None:
CourseEnrollment.enroll(user, course_id) CourseEnrollment.enroll(user, course_key)
class Command(BaseCommand): class Command(BaseCommand):
...@@ -32,5 +32,13 @@ Examples: ...@@ -32,5 +32,13 @@ Examples:
return return
n = int(args[0]) n = int(args[0])
course_id = args[1] if len(args) == 2 else None
if len(args) == 2:
try:
course_key = CourseKey.from_string(args[1])
except InvalidKeyError:
course_key = SlashSeparatedCourseKey.from_deprecated_string(args[1])
else:
course_id = None
create(n, course_id) create(n, course_id)
...@@ -3,6 +3,7 @@ from django.core.management.base import BaseCommand ...@@ -3,6 +3,7 @@ from django.core.management.base import BaseCommand
from django.contrib.auth.models import User from django.contrib.auth.models import User
from student.models import CourseEnrollment from student.models import CourseEnrollment
from shoppingcart.models import CertificateItem from shoppingcart.models import CertificateItem
from xmodule.modulestore.locations import SlashSeparatedCourseKey
class Command(BaseCommand): class Command(BaseCommand):
...@@ -30,32 +31,35 @@ class Command(BaseCommand): ...@@ -30,32 +31,35 @@ class Command(BaseCommand):
) )
def handle(self, *args, **options): def handle(self, *args, **options):
source = options['source_course'] source_key = SlashSeparatedCourseKey.from_deprecated_string(options['source_course'])
dest = options['dest_course'] dest_key = SlashSeparatedCourseKey.from_deprecated_string(options['dest_course'])
source_students = User.objects.filter( source_students = User.objects.filter(
courseenrollment__course_id=source) courseenrollment__course_id=source_key
)
for user in source_students: for user in source_students:
if CourseEnrollment.is_enrolled(student, dest): if CourseEnrollment.is_enrolled(user, dest_key):
# Un Enroll from source course but don't mess # Un Enroll from source course but don't mess
# with the enrollment in the destination course. # with the enrollment in the destination course.
CourseEnrollment.unenroll(user,source) CourseEnrollment.unenroll(user, source_key)
print("Unenrolled {} from {}".format(user.username, source)) print("Unenrolled {} from {}".format(user.username, source_key.to_deprecated_string()))
msg = "Skipping {}, already enrolled in destination course {}" msg = "Skipping {}, already enrolled in destination course {}"
print(msg.format(user.username, dest)) print(msg.format(user.username, dest_key.to_deprecated_string()))
continue continue
print("Moving {}.".format(user.username)) print("Moving {}.".format(user.username))
# Find the old enrollment. # Find the old enrollment.
enrollment = CourseEnrollment.objects.get(user=user, enrollment = CourseEnrollment.objects.get(
course_id=source) user=user,
course_id=source_key
)
# Move the Student between the classes. # Move the Student between the classes.
mode = enrollment.mode mode = enrollment.mode
old_is_active = enrollment.is_active old_is_active = enrollment.is_active
CourseEnrollment.unenroll(user,source) CourseEnrollment.unenroll(user, source_key)
new_enrollment = CourseEnrollment.enroll(user, dest, mode=mode) new_enrollment = CourseEnrollment.enroll(user, dest_key, mode=mode)
# Unenroll from the new coures if the user had unenrolled # Unenroll from the new coures if the user had unenrolled
# form the old course. # form the old course.
...@@ -65,12 +69,13 @@ class Command(BaseCommand): ...@@ -65,12 +69,13 @@ class Command(BaseCommand):
if mode == 'verified': if mode == 'verified':
try: try:
certificate_item = CertificateItem.objects.get( certificate_item = CertificateItem.objects.get(
course_id=source, course_id=source_key,
course_enrollment=enrollment) course_enrollment=enrollment
)
except CertificateItem.DoesNotExist: except CertificateItem.DoesNotExist:
print("No certificate for {}".format(user)) print("No certificate for {}".format(user))
continue continue
certificate_item.course_id = dest certificate_item.course_id = dest_key
certificate_item.course_enrollment = new_enrollment certificate_item.course_enrollment = new_enrollment
certificate_item.save() certificate_item.save()
...@@ -40,7 +40,7 @@ class Command(BaseCommand): ...@@ -40,7 +40,7 @@ class Command(BaseCommand):
if len(args) == 2: if len(args) == 2:
course_id = SlashSeparatedCourseKey.from_deprecated_string(args[0]) course_id = SlashSeparatedCourseKey.from_deprecated_string(args[0])
usage_key = UsageKey.from_string(args[1]) usage_key = course_id.make_usage_key_from_deprecated_string(args[1])
else: else:
print self.help print self.help
return return
......
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