From 4a910f8724eb6a2859d1ad1d57a22b0095c0acef Mon Sep 17 00:00:00 2001 From: John Jarvis Date: Mon, 4 Nov 2013 14:58:34 -0500 Subject: [PATCH 1/2] adding create_user script --- .../management/commands/create_user.py | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 common/djangoapps/student/management/commands/create_user.py diff --git a/common/djangoapps/student/management/commands/create_user.py b/common/djangoapps/student/management/commands/create_user.py new file mode 100644 index 0000000000..a86c470040 --- /dev/null +++ b/common/djangoapps/student/management/commands/create_user.py @@ -0,0 +1,88 @@ +from optparse import make_option +from django.core.management.base import BaseCommand +from student.models import CourseEnrollment, Registration +from student.views import _do_create_account +from django.contrib.auth.models import User + + +class Command(BaseCommand): + help = """ + This command creates and registers a user in a given course + as "audit", "verified_id" or "honor". + + example: + # Enroll a user test@example.com into the demo course + # The username and name will default to "test" + manage.py ... create_user -e test@example.com -p insecure -c edX/Open_DemoX/edx_demo_course -m verified_id + """ + + option_list = BaseCommand.option_list + ( + make_option('-m', '--mode', + metavar='ENROLLMENT_TYPE', + dest='mode', + default='honor', + choices=('audit', 'verified_id', 'honor'), + 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', + metavar='NAME', + 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, + help='course to enroll the user in (optiona)'), + make_option('-s', '--staff', + metavar='COURSE_ID', + 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] + + post_data = { + 'username': username, + 'email': options['email'], + 'password': options['password'], + 'name': name, + 'honor_code': u'true', + 'terms_of_service': u'true', + } + create_account = _do_create_account(post_data) + if isinstance(create_account, tuple): + user = create_account[0] + if options['staff']: + user.is_staff = True + user.save() + reg = Registration.objects.get(user=user) + reg.activate() + reg.save() + else: + print create_account + user = User.objects.get(email=options['email']) + if options['course']: + CourseEnrollment.enroll(user, options['course'], mode=options['mode']) From 24524ef305b051bfaf0a2fe2f9220f5df8e9215a Mon Sep 17 00:00:00 2001 From: John Jarvis Date: Mon, 4 Nov 2013 15:14:20 -0500 Subject: [PATCH 2/2] addressing comments --- .../student/management/commands/create_user.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/djangoapps/student/management/commands/create_user.py b/common/djangoapps/student/management/commands/create_user.py index a86c470040..bf4c084dca 100644 --- a/common/djangoapps/student/management/commands/create_user.py +++ b/common/djangoapps/student/management/commands/create_user.py @@ -8,20 +8,20 @@ from django.contrib.auth.models import User class Command(BaseCommand): help = """ This command creates and registers a user in a given course - as "audit", "verified_id" or "honor". + as "audit", "verified" or "honor". example: # Enroll a user test@example.com into the demo course # The username and name will default to "test" - manage.py ... create_user -e test@example.com -p insecure -c edX/Open_DemoX/edx_demo_course -m verified_id + manage.py ... create_user -e test@example.com -p insecure -c edX/Open_DemoX/edx_demo_course -m verified """ option_list = BaseCommand.option_list + ( make_option('-m', '--mode', - metavar='ENROLLMENT_TYPE', + metavar='ENROLLMENT_MODE', dest='mode', default='honor', - choices=('audit', 'verified_id', 'honor'), + choices=('audit', 'verified', 'honor'), help='Enrollment type for user for a specific course'), make_option('-u', '--username', metavar='USERNAME', @@ -47,7 +47,7 @@ class Command(BaseCommand): metavar='COURSE_ID', dest='course', default=None, - help='course to enroll the user in (optiona)'), + help='course to enroll the user in (optional)'), make_option('-s', '--staff', metavar='COURSE_ID', dest='staff',