create_user.py 4.26 KB
Newer Older
John Jarvis committed
1
from optparse import make_option
2 3 4

from django.conf import settings
from django.contrib.auth.models import User
John Jarvis committed
5
from django.core.management.base import BaseCommand
6 7
from django.utils import translation

8
from opaque_keys import InvalidKeyError
9 10
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey
11
from student.forms import AccountCreationForm
Ned Batchelder committed
12
from student.models import CourseEnrollment, create_comments_service_user
13
from student.views import _do_create_account, AccountValidationError
14
from track.management.tracked_command import TrackedCommand
John Jarvis committed
15

16 17

class Command(TrackedCommand):
John Jarvis committed
18 19
    help = """
    This command creates and registers a user in a given course
John Jarvis committed
20
    as "audit", "verified" or "honor".
John Jarvis committed
21 22 23 24

    example:
        # Enroll a user test@example.com into the demo course
        # The username and name will default to "test"
John Jarvis committed
25
        manage.py ... create_user -e test@example.com -p insecure -c edX/Open_DemoX/edx_demo_course -m verified
John Jarvis committed
26 27 28 29
    """

    option_list = BaseCommand.option_list + (
        make_option('-m', '--mode',
John Jarvis committed
30
                    metavar='ENROLLMENT_MODE',
John Jarvis committed
31 32
                    dest='mode',
                    default='honor',
John Jarvis committed
33
                    choices=('audit', 'verified', 'honor'),
John Jarvis committed
34 35 36 37 38 39 40 41 42 43 44 45
                    help='Enrollment type for user for a specific course'),
        make_option('-u', '--username',
                    metavar='USERNAME',
                    dest='username',
                    default=None,
                    help='Username, defaults to "user" in the email'),
        make_option('-n', '--name',
                    metavar='NAME',
                    dest='name',
                    default=None,
                    help='Name, defaults to "user" in the email'),
        make_option('-p', '--password',
46
                    metavar='PASSWORD',
John Jarvis committed
47 48 49 50 51 52 53 54 55 56 57 58
                    dest='password',
                    default=None,
                    help='Password for user'),
        make_option('-e', '--email',
                    metavar='EMAIL',
                    dest='email',
                    default=None,
                    help='Email for user'),
        make_option('-c', '--course',
                    metavar='COURSE_ID',
                    dest='course',
                    default=None,
John Jarvis committed
59
                    help='course to enroll the user in (optional)'),
John Jarvis committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        make_option('-s', '--staff',
                    dest='staff',
                    default=False,
                    action='store_true',
                    help='give user the staff bit'),
    )

    def handle(self, *args, **options):
        username = options['username']
        name = options['name']
        if not username:
            username = options['email'].split('@')[0]
        if not name:
            name = options['email'].split('@')[0]

75 76 77 78 79 80 81 82 83
        # parse out the course into a coursekey
        if options['course']:
            try:
                course = CourseKey.from_string(options['course'])
            # if it's not a new-style course key, parse it from an old-style
            # course key
            except InvalidKeyError:
                course = SlashSeparatedCourseKey.from_deprecated_string(options['course'])

84 85 86 87 88 89 90 91 92
        form = AccountCreationForm(
            data={
                'username': username,
                'email': options['email'],
                'password': options['password'],
                'name': name,
            },
            tos_required=False
        )
93 94 95 96 97
        # django.utils.translation.get_language() will be used to set the new
        # user's preferred language.  This line ensures that the result will
        # match this installation's default locale.  Otherwise, inside a
        # management command, it will always return "en-us".
        translation.activate(settings.LANGUAGE_CODE)
98
        try:
99
            user, _, reg = _do_create_account(form)
John Jarvis committed
100 101 102 103 104
            if options['staff']:
                user.is_staff = True
                user.save()
            reg.activate()
            reg.save()
105 106 107
            create_comments_service_user(user)
        except AccountValidationError as e:
            print e.message
John Jarvis committed
108 109
            user = User.objects.get(email=options['email'])
        if options['course']:
110
            CourseEnrollment.enroll(user, course, mode=options['mode'])
111
        translation.deactivate()