change_enrollment.py 3.25 KB
Newer Older
1
from django.core.management.base import BaseCommand, CommandError
2
from opaque_keys import InvalidKeyError
3 4 5
from optparse import make_option
from student.models import CourseEnrollment, User

6 7
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey
8

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

class Command(BaseCommand):

    help = """
    Changes the enrollment status for students that meet
    the criteria specified by the parameters to this command.

    Example:

        Change enrollment for user joe from audit to honor:

          $ ... change_enrollment -u joe -c some/course/id --from audit --to honor

        Change enrollment for all users in some/course/id from audit to honor

John Jarvis committed
24
          $ ... change_enrollment -c some/course/id --from audit --to honor
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

    """

    option_list = BaseCommand.option_list + (
        make_option('-f', '--from',
                    metavar='FROM_MODE',
                    dest='from_mode',
                    default=False,
                    help='move from this enrollment mode'),
        make_option('-t', '--to',
                    metavar='TO_MODE',
                    dest='to_mode',
                    default=False,
                    help='move to this enrollment mode'),
        make_option('-u', '--user',
                    metavar='USER',
                    dest='user',
                    default=False,
                    help="User to move, if not specified will move all users in the course"),
        make_option('-c', '--course',
                    metavar='COURSE_ID',
                    dest='course_id',
                    default=False,
John Jarvis committed
48
                    help="course id to use for transfer"),
49 50 51 52 53 54 55 56 57 58 59 60 61
        make_option('-n', '--noop',
                    action='store_true',
                    dest='noop',
                    default=False,
                    help="display what will be done but don't actually do anything")

    )

    def handle(self, *args, **options):
        if not options['course_id']:
            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')
62 63 64 65 66 67

        try:
            course_key = CourseKey.from_string(options['course_id'])
        except InvalidKeyError:
            course_key = SlashSeparatedCourseKey.from_deprecated_string(options['course_id'])

68
        filter_args = dict(
69
            course_id=course_key,
70 71 72 73 74 75
            mode=options['from_mode']
        )
        if options['user']:
            if '@' in options['user']:
                user = User.objects.get(email=options['user'])
            else:
76
                user = User.objects.get(username=options['user'])
77 78 79 80 81 82 83 84 85 86 87 88
            filter_args['user'] = user
        enrollments = CourseEnrollment.objects.filter(**filter_args)
        if options['noop']:
            print "Would have changed {num_enrollments} students from {from_mode} to {to_mode}".format(
                num_enrollments=enrollments.count(),
                from_mode=options['from_mode'],
                to_mode=options['to_mode']
            )
        else:
            for enrollment in enrollments:
                enrollment.update_enrollment(mode=options['to_mode'])
                enrollment.save()