Skip to content
Snippets Groups Projects
Commit 1f849e44 authored by Calen Pennington's avatar Calen Pennington
Browse files

Fix management commands

parent 9dc48c00
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,9 @@ import csv
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError
from student.models import anonymous_id_for_user
from xmodule.modulestore.locations import SlashSeparatedCourseKey
class Command(BaseCommand):
......@@ -35,16 +37,16 @@ class Command(BaseCommand):
raise CommandError("Usage: unique_id_mapping %s" %
" ".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.
# 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
students = User.objects.filter(courseenrollment__course_id=course_id)
students = User.objects.filter(courseenrollment__course_id=course_key)
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
# Write mapping to output file in CSV format with a simple header
......@@ -60,7 +62,7 @@ class Command(BaseCommand):
csv_writer.writerow((
student.id,
anonymous_id_for_user(student, None),
anonymous_id_for_user(student, course_id)
anonymous_id_for_user(student, course_key)
))
except IOError:
raise CommandError("Error writing to file: %s" % output_filename)
......
from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError
from optparse import make_option
from student.models import CourseEnrollment, User
from xmodule.modulestore.keys import CourseKey
from xmodule.modulestore.locations import SlashSeparatedCourseKey
class Command(BaseCommand):
......@@ -55,8 +59,14 @@ class Command(BaseCommand):
raise CommandError("You must specify a course id for this command")
if not options['from_mode'] or not options['to_mode']:
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(
course_id=options['course_id'],
course_id=course_key,
mode=options['from_mode']
)
if options['user']:
......
......@@ -7,12 +7,12 @@ from student.models import CourseEnrollment
from student.views import _do_create_account, get_random_post_override
def create(n, course_id):
"""Create n users, enrolling them in course_id if it's not None"""
def create(n, course_key):
"""Create n users, enrolling them in course_key if it's not None"""
for i in range(n):
(user, user_profile, _) = _do_create_account(get_random_post_override())
if course_id is not None:
CourseEnrollment.enroll(user, course_id)
if course_key is not None:
CourseEnrollment.enroll(user, course_key)
class Command(BaseCommand):
......@@ -32,5 +32,13 @@ Examples:
return
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)
......@@ -3,6 +3,7 @@ from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from student.models import CourseEnrollment
from shoppingcart.models import CertificateItem
from xmodule.modulestore.locations import SlashSeparatedCourseKey
class Command(BaseCommand):
......@@ -30,32 +31,35 @@ class Command(BaseCommand):
)
def handle(self, *args, **options):
source = options['source_course']
dest = options['dest_course']
source_key = SlashSeparatedCourseKey.from_deprecated_string(options['source_course'])
dest_key = SlashSeparatedCourseKey.from_deprecated_string(options['dest_course'])
source_students = User.objects.filter(
courseenrollment__course_id=source)
courseenrollment__course_id=source_key
)
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
# with the enrollment in the destination course.
CourseEnrollment.unenroll(user,source)
print("Unenrolled {} from {}".format(user.username, source))
CourseEnrollment.unenroll(user, source_key)
print("Unenrolled {} from {}".format(user.username, source_key.to_deprecated_string()))
msg = "Skipping {}, already enrolled in destination course {}"
print(msg.format(user.username, dest))
print(msg.format(user.username, dest_key.to_deprecated_string()))
continue
print("Moving {}.".format(user.username))
# Find the old enrollment.
enrollment = CourseEnrollment.objects.get(user=user,
course_id=source)
enrollment = CourseEnrollment.objects.get(
user=user,
course_id=source_key
)
# Move the Student between the classes.
mode = enrollment.mode
old_is_active = enrollment.is_active
CourseEnrollment.unenroll(user,source)
new_enrollment = CourseEnrollment.enroll(user, dest, mode=mode)
CourseEnrollment.unenroll(user, source_key)
new_enrollment = CourseEnrollment.enroll(user, dest_key, mode=mode)
# Unenroll from the new coures if the user had unenrolled
# form the old course.
......@@ -65,12 +69,13 @@ class Command(BaseCommand):
if mode == 'verified':
try:
certificate_item = CertificateItem.objects.get(
course_id=source,
course_enrollment=enrollment)
course_id=source_key,
course_enrollment=enrollment
)
except CertificateItem.DoesNotExist:
print("No certificate for {}".format(user))
continue
certificate_item.course_id = dest
certificate_item.course_id = dest_key
certificate_item.course_enrollment = new_enrollment
certificate_item.save()
......@@ -40,7 +40,7 @@ class Command(BaseCommand):
if len(args) == 2:
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:
print self.help
return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment