From 3b6b4668dd696e483623e28408c7c5555a222292 Mon Sep 17 00:00:00 2001 From: Don Mitchell Date: Wed, 20 Aug 2014 15:57:55 -0400 Subject: [PATCH] On rerun, override the course start date LMS-11011 --- .../tests/test_course_create_rerun.py | 67 +++++++++++++++++++ cms/djangoapps/contentstore/views/course.py | 15 +++-- 2 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 cms/djangoapps/contentstore/tests/test_course_create_rerun.py diff --git a/cms/djangoapps/contentstore/tests/test_course_create_rerun.py b/cms/djangoapps/contentstore/tests/test_course_create_rerun.py new file mode 100644 index 0000000000..e23f5f4236 --- /dev/null +++ b/cms/djangoapps/contentstore/tests/test_course_create_rerun.py @@ -0,0 +1,67 @@ +""" +Test view handler for rerun (and eventually create) +""" +from django.test.client import RequestFactory +from opaque_keys.edx.keys import CourseKey + +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory +from student.roles import CourseInstructorRole, CourseStaffRole +from student.tests.factories import UserFactory +from contentstore.tests.utils import AjaxEnabledTestClient, parse_json +from datetime import datetime +from xmodule.course_module import CourseFields + + +class TestCourseListing(ModuleStoreTestCase): + """ + Unit tests for getting the list of courses for a logged in user + """ + def setUp(self): + """ + Add a user and a course + """ + super(TestCourseListing, self).setUp() + # create and log in a staff user. + # create and log in a non-staff user + self.user = UserFactory() + self.factory = RequestFactory() + self.client = AjaxEnabledTestClient() + self.client.login(username=self.user.username, password='test') + + source_course = CourseFactory.create( + org='origin', + number='the_beginning', + run='first', + display_name='the one and only', + start=datetime.utcnow() + ) + self.source_course_key = source_course.id + + for role in [CourseInstructorRole, CourseStaffRole]: + role(self.source_course_key).add_users(self.user) + + + def tearDown(self): + """ + Reverse the setup + """ + self.client.logout() + ModuleStoreTestCase.tearDown(self) + + def test_rerun(self): + """ + Just testing the functionality the view handler adds over the tasks tested in test_clone_course + """ + response = self.client.ajax_post('/course/', { + 'source_course_key': unicode(self.source_course_key), + 'org': self.source_course_key.org, 'course': self.source_course_key.course, 'run': 'copy', + 'display_name': 'not the same old name', + }) + self.assertEqual(response.status_code, 200) + data = parse_json(response) + dest_course_key = CourseKey.from_string(data['destination_course_key']) + + self.assertEqual(dest_course_key.run, 'copy') + dest_course = self.store.get_course(dest_course_key) + self.assertEqual(dest_course.start, CourseFields.start.default) diff --git a/cms/djangoapps/contentstore/views/course.py b/cms/djangoapps/contentstore/views/course.py index e2e88c636e..c6d97ec54d 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -67,6 +67,7 @@ from student import auth from course_action_state.models import CourseRerunState, CourseRerunUIStateManager from course_action_state.managers import CourseActionStateItemNotFoundError from microsite_configuration import microsite +from xmodule.course_module import CourseFields __all__ = ['course_info_handler', 'course_handler', 'course_info_update_handler', @@ -508,24 +509,28 @@ def _create_or_rerun_course(request): try: org = request.json.get('org') - number = request.json.get('number') + course = request.json.get('number', request.json.get('course')) display_name = request.json.get('display_name') + # force the start date for reruns and allow us to override start via the client + start = request.json.get('start', CourseFields.start.default) run = request.json.get('run') # allow/disable unicode characters in course_id according to settings if not settings.FEATURES.get('ALLOW_UNICODE_COURSE_ID'): - if _has_non_ascii_characters(org) or _has_non_ascii_characters(number) or _has_non_ascii_characters(run): + if _has_non_ascii_characters(org) or _has_non_ascii_characters(course) or _has_non_ascii_characters(run): return JsonResponse( {'error': _('Special characters not allowed in organization, course number, and course run.')}, status=400 ) - fields = {'display_name': display_name} if display_name is not None else {} + fields = {'start': start} + if display_name is not None: + fields['display_name'] = display_name if 'source_course_key' in request.json: - return _rerun_course(request, org, number, run, fields) + return _rerun_course(request, org, course, run, fields) else: - return _create_new_course(request, org, number, run, fields) + return _create_new_course(request, org, course, run, fields) except DuplicateCourseError: return JsonResponse({