def test_ssl_cms_redirection(self): """ Auto signup auth user and ensure they return to the original url they visited after being logged in. """ course = CourseFactory.create(org='MITx', number='999', display_name='Robot Super Course') external_auth.views.ssl_login(self._create_ssl_request('/')) user = User.objects.get(email=self.USER_EMAIL) CourseEnrollment.enroll(user, course.id) CourseStaffRole(course.location).add_users(user) location = Location([ 'i4x', 'MITx', '999', 'course', Location.clean('Robot Super Course'), None ]) new_location = loc_mapper().translate_location(location.course_id, location, True, True) course_private_url = new_location.url_reverse('course/', '') self.assertFalse(SESSION_KEY in self.client.session) response = self.client.get(course_private_url, follow=True, SSL_CLIENT_S_DN=self.AUTH_DN.format( self.USER_NAME, self.USER_EMAIL), HTTP_ACCEPT='text/html') self.assertEqual( ('http://testserver{0}'.format(course_private_url), 302), response.redirect_chain[-1]) self.assertIn(SESSION_KEY, self.client.session)
def _create(cls, target_class, **kwargs): # All class attributes (from this class and base classes) are # passed in via **kwargs. However, some of those aren't actual field values, # so pop those off for use separately org = kwargs.pop('org', None) number = kwargs.pop('number', kwargs.pop('course', None)) store = kwargs.pop('modulestore') location = Location('i4x', org, number, 'course', Location.clean(kwargs.get('display_name'))) # Write the data to the mongo datastore new_course = store.create_xmodule(location, metadata=kwargs.get( 'metadata', None)) new_course.start = datetime.datetime.now(UTC).replace(microsecond=0) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.save_xmodule(new_course) return new_course
def _create(cls, target_class, **kwargs): org = kwargs.pop('org', None) number = kwargs.pop('number', kwargs.pop('course', None)) display_name = kwargs.pop('display_name', None) location = Location('i4x', org, number, 'course', Location.clean(display_name)) store = editable_modulestore('direct') # Write the data to the mongo datastore new_course = store.create_xmodule(location, metadata=kwargs.get( 'metadata', None)) # This metadata code was copied from cms/djangoapps/contentstore/views.py if display_name is not None: new_course.display_name = display_name new_course.start = datetime.datetime.now(UTC).replace(microsecond=0) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.save_xmodule(new_course) return new_course
def test_ssl_cms_redirection(self): """ Auto signup auth user and ensure they return to the original url they visited after being logged in. """ course = CourseFactory.create( org='MITx', number='999', display_name='Robot Super Course' ) external_auth.views.ssl_login(self._create_ssl_request('/')) user = User.objects.get(email=self.USER_EMAIL) CourseEnrollment.enroll(user, course.id) CourseStaffRole(course.location).add_users(user) location = Location(['i4x', 'MITx', '999', 'course', Location.clean('Robot Super Course'), None]) new_location = loc_mapper().translate_location( location.course_id, location, True, True ) course_private_url = new_location.url_reverse('course/', '') self.assertFalse(SESSION_KEY in self.client.session) response = self.client.get( course_private_url, follow=True, SSL_CLIENT_S_DN=self.AUTH_DN.format(self.USER_NAME, self.USER_EMAIL), HTTP_ACCEPT='text/html' ) self.assertEqual(('http://testserver{0}'.format(course_private_url), 302), response.redirect_chain[-1]) self.assertIn(SESSION_KEY, self.client.session)
def _create(cls, target_class, **kwargs): org = kwargs.pop('org', None) number = kwargs.pop('number', kwargs.pop('course', None)) display_name = kwargs.pop('display_name', None) location = Location('i4x', org, number, 'course', Location.clean(display_name)) store = editable_modulestore('direct') # Write the data to the mongo datastore new_course = store.create_xmodule(location) # This metadata code was copied from cms/djangoapps/contentstore/views.py if display_name is not None: new_course.display_name = display_name new_course.start = datetime.datetime.now(UTC).replace(microsecond=0) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.save_xmodule(new_course) return new_course
def create_new_course(request): if settings.MITX_FEATURES.get('DISABLE_COURSE_CREATION', False) and not request.user.is_staff: raise PermissionDenied() # This logic is repeated in xmodule/modulestore/tests/factories.py # so if you change anything here, you need to also change it there. # TODO: write a test that creates two courses, one with the factory and # the other with this method, then compare them to make sure they are # equivalent. template = Location(request.POST['template']) org = request.POST.get('org') number = request.POST.get('number') display_name = request.POST.get('display_name') try: dest_location = Location('i4x', org, number, 'course', Location.clean(display_name)) except InvalidLocationError as error: return HttpResponse(json.dumps({'ErrMsg': "Unable to create course '" + display_name + "'.\n\n" + error.message})) # see if the course already exists existing_course = None try: existing_course = modulestore('direct').get_item(dest_location) except ItemNotFoundError: pass if existing_course is not None: return HttpResponse(json.dumps({'ErrMsg': 'There is already a course defined with this name.'})) course_search_location = ['i4x', dest_location.org, dest_location.course, 'course', None] courses = modulestore().get_items(course_search_location) if len(courses) > 0: return HttpResponse(json.dumps({'ErrMsg': 'There is already a course defined with the same organization and course number.'})) new_course = modulestore('direct').clone_item(template, dest_location) # clone a default 'about' module as well about_template_location = Location(['i4x', 'edx', 'templates', 'about', 'overview']) dest_about_location = dest_location._replace(category='about', name='overview') modulestore('direct').clone_item(about_template_location, dest_about_location) if display_name is not None: new_course.display_name = display_name # set a default start date to now new_course.start = datetime.datetime.now(UTC()) initialize_course_tabs(new_course) create_all_course_groups(request.user, new_course.location) # seed the forums seed_permissions_roles(new_course.location.course_id) return HttpResponse(json.dumps({'id': new_course.location.url()}))
def test_clean(): pairs = general_pairs + [ ('a:b', 'a_b'), # no colons in non-name components ('a-b', 'a-b'), # dashes ok ('a.b', 'a.b'), # dot ok ] for input, output in pairs: assert_equals(Location.clean(input), output)
def create_new_course(request): """ Create a new course """ if not is_user_in_creator_group(request.user): raise PermissionDenied() org = request.POST.get("org") number = request.POST.get("number") display_name = request.POST.get("display_name") try: dest_location = Location("i4x", org, number, "course", Location.clean(display_name)) except InvalidLocationError as error: return JsonResponse( {"ErrMsg": "Unable to create course '{name}'.\n\n{err}".format(name=display_name, err=error.message)} ) # see if the course already exists existing_course = None try: existing_course = modulestore("direct").get_item(dest_location) except ItemNotFoundError: pass if existing_course is not None: return JsonResponse({"ErrMsg": "There is already a course defined with this name."}) course_search_location = ["i4x", dest_location.org, dest_location.course, "course", None] courses = modulestore().get_items(course_search_location) if len(courses) > 0: return JsonResponse( {"ErrMsg": "There is already a course defined with the same organization and course number."} ) # instantiate the CourseDescriptor and then persist it # note: no system to pass if display_name is None: metadata = {} else: metadata = {"display_name": display_name} modulestore("direct").create_and_save_xmodule(dest_location, metadata=metadata) new_course = modulestore("direct").get_item(dest_location) # clone a default 'about' overview module as well dest_about_location = dest_location.replace(category="about", name="overview") overview_template = AboutDescriptor.get_template("overview.yaml") modulestore("direct").create_and_save_xmodule( dest_about_location, system=new_course.system, definition_data=overview_template.get("data") ) initialize_course_tabs(new_course) create_all_course_groups(request.user, new_course.location) # seed the forums seed_permissions_roles(new_course.location.course_id) return JsonResponse({"id": new_course.location.url()})
def _create(cls, target_class, **kwargs): template = Location('i4x', 'edx', 'templates', 'course', 'Empty') org = kwargs.pop('org', None) number = kwargs.pop('number', None) display_name = kwargs.pop('display_name', None) location = Location('i4x', org, number, 'course', Location.clean(display_name)) try: store = modulestore('direct') except KeyError: store = modulestore() # Write the data to the mongo datastore new_course = store.clone_item(template, location) # This metadata code was copied from cms/djangoapps/contentstore/views.py if display_name is not None: new_course.display_name = display_name new_course.lms.start = datetime.datetime.now(UTC) new_course.tabs = kwargs.pop('tabs', [{ "type": "courseware" }, { "type": "course_info", "name": "Course Info" }, { "type": "discussion", "name": "Discussion" }, { "type": "wiki", "name": "Wiki" }, { "type": "progress", "name": "Progress" }]) data = kwargs.pop('data', None) if data is not None: store.update_item(new_course.location, data) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.update_metadata(new_course.location.url(), own_metadata(new_course)) # update_item updates the the course as it exists in the modulestore, but doesn't # update the instance we are working with, so have to refetch the course after updating it. new_course = store.get_instance(new_course.id, new_course.location) return new_course
def assign_textbook_id(textbook, used_ids=()): """ Return an ID that can be assigned to a textbook and doesn't match the used_ids """ tid = Location.clean(textbook["tab_title"]) if not tid[0].isdigit(): # stick a random digit in front tid = random.choice(string.digits) + tid while tid in used_ids: # add a random ASCII character to the end tid = tid + random.choice(string.ascii_lowercase) return tid
def test_course_overview_view_with_course(self): """Test viewing the course overview page with an existing course""" CourseFactory.create(org="MITx", course="999", display_name="Robot Super Course") data = {"org": "MITx", "course": "999", "name": Location.clean("Robot Super Course")} resp = self.client.get(reverse("course_index", kwargs=data)) self.assertContains( resp, '<article class="courseware-overview" data-course-id="i4x://MITx/999/course/Robot_Super_Course">', status_code=200, html=True, )
def _create(cls, target_class, *args, **kwargs): template = Location('i4x', 'edx', 'templates', 'course', 'Empty') org = kwargs.get('org') number = kwargs.get('number') display_name = kwargs.get('display_name') location = Location('i4x', org, number, 'course', Location.clean(display_name)) try: store = modulestore('direct') except KeyError: store = modulestore() # Write the data to the mongo datastore new_course = store.clone_item(template, location) # This metadata code was copied from # cms/djangoapps/contentstore/views.py if display_name is not None: new_course.display_name = display_name new_course.lms.start = gmtime() new_course.tabs = kwargs.get('tabs', [{ "type": "courseware" }, { "type": "course_info", "name": "Course Info" }, { "type": "discussion", "name": "Discussion" }, { "type": "wiki", "name": "Wiki" }, { "type": "progress", "name": "Progress" }]) new_course.discussion_link = kwargs.get('discussion_link') # Update the data in the mongo datastore store.update_metadata(new_course.location.url(), own_metadata(new_course)) data = kwargs.get('data') if data is not None: store.update_item(new_course.location, data) return new_course
def _create(cls, target_class, **kwargs): template = Location('i4x', 'edx', 'templates', 'course', 'Empty') org = kwargs.pop('org', None) number = kwargs.pop('number', None) display_name = kwargs.pop('display_name', None) location = Location('i4x', org, number, 'course', Location.clean(display_name)) try: store = modulestore('direct') except KeyError: store = modulestore() # Write the data to the mongo datastore new_course = store.clone_item(template, location) # This metadata code was copied from cms/djangoapps/contentstore/views.py if display_name is not None: new_course.display_name = display_name new_course.lms.start = datetime.datetime.now(UTC) new_course.tabs = kwargs.pop( 'tabs', [ {"type": "courseware"}, {"type": "course_info", "name": "Course Info"}, {"type": "discussion", "name": "Discussion"}, {"type": "wiki", "name": "Wiki"}, {"type": "progress", "name": "Progress"} ] ) data = kwargs.pop('data', None) if data is not None: store.update_item(new_course.location, data) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.update_metadata(new_course.location.url(), own_metadata(new_course)) # update_item updates the the course as it exists in the modulestore, but doesn't # update the instance we are working with, so have to refetch the course after updating it. new_course = store.get_instance(new_course.id, new_course.location) return new_course
def test_course_overview_view_with_course(self): """Test viewing the course overview page with an existing course""" CourseFactory.create(org='MITx', course='999', display_name='Robot Super Course') data = { 'org': 'MITx', 'course': '999', 'name': Location.clean('Robot Super Course'), } resp = self.client.get(reverse('course_index', kwargs=data)) self.assertContains( resp, '<article class="courseware-overview" data-course-id="i4x://MITx/999/course/Robot_Super_Course">', status_code=200, html=True )
def _create(cls, target_class, *args, **kwargs): template = Location('i4x', 'edx', 'templates', 'course', 'Empty') org = kwargs.get('org') number = kwargs.get('number') display_name = kwargs.get('display_name') location = Location('i4x', org, number, 'course', Location.clean(display_name)) try: store = modulestore('direct') except KeyError: store = modulestore() # Write the data to the mongo datastore new_course = store.clone_item(template, location) # This metadata code was copied from # cms/djangoapps/contentstore/views.py if display_name is not None: new_course.display_name = display_name new_course.lms.start = gmtime() new_course.tabs = kwargs.get( 'tabs', [ {"type": "courseware"}, {"type": "course_info", "name": "Course Info"}, {"type": "discussion", "name": "Discussion"}, {"type": "wiki", "name": "Wiki"}, {"type": "progress", "name": "Progress"} ] ) new_course.discussion_link = kwargs.get('discussion_link') # Update the data in the mongo datastore store.update_metadata( new_course.location.url(), own_metadata(new_course)) data = kwargs.get('data') if data is not None: store.update_item(new_course.location, data) return new_course
def _create(cls, target_class, **kwargs): # All class attributes (from this class and base classes) are # passed in via **kwargs. However, some of those aren't actual field values, # so pop those off for use separately org = kwargs.pop('org', None) # because the factory provides a default 'number' arg, prefer the non-defaulted 'course' arg if any number = kwargs.pop('course', kwargs.pop('number', None)) store = kwargs.pop('modulestore') location = Location('i4x', org, number, 'course', Location.clean(kwargs.get('display_name'))) # Write the data to the mongo datastore new_course = store.create_xmodule(location, metadata=kwargs.get('metadata', None)) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.save_xmodule(new_course) return new_course
def _create(cls, target_class, **kwargs): org = kwargs.pop('org', None) number = kwargs.pop('number', kwargs.pop('course', None)) display_name = kwargs.pop('display_name', None) location = Location('i4x', org, number, 'course', Location.clean(display_name)) try: store = modulestore('direct') except KeyError: store = modulestore() # Write the data to the mongo datastore new_course = store.create_xmodule(location) # This metadata code was copied from cms/djangoapps/contentstore/views.py if display_name is not None: new_course.display_name = display_name new_course.lms.start = datetime.datetime.now(UTC).replace(microsecond=0) new_course.tabs = kwargs.pop( 'tabs', [ {"type": "courseware"}, {"type": "course_info", "name": "Course Info"}, {"type": "discussion", "name": "Discussion"}, {"type": "wiki", "name": "Wiki"}, {"type": "progress", "name": "Progress"} ] ) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.save_xmodule(new_course) return new_course
def _create(cls, target_class, **kwargs): # All class attributes (from this class and base classes) are # passed in via **kwargs. However, some of those aren't actual field values, # so pop those off for use separately org = kwargs.pop('org', None) number = kwargs.pop('number', kwargs.pop('course', None)) store = kwargs.pop('modulestore') location = Location('i4x', org, number, 'course', Location.clean(kwargs.get('display_name'))) # Write the data to the mongo datastore new_course = store.create_xmodule(location, metadata=kwargs.get('metadata', None)) new_course.start = datetime.datetime.now(UTC).replace(microsecond=0) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.save_xmodule(new_course) return new_course
def _create(cls, target_class, **kwargs): # All class attributes (from this class and base classes) are # passed in via **kwargs. However, some of those aren't actual field values, # so pop those off for use separately org = kwargs.pop("org", None) # because the factory provides a default 'number' arg, prefer the non-defaulted 'course' arg if any number = kwargs.pop("course", kwargs.pop("number", None)) store = kwargs.pop("modulestore") location = Location("i4x", org, number, "course", Location.clean(kwargs.get("display_name"))) # Write the data to the mongo datastore new_course = store.create_xmodule(location, metadata=kwargs.get("metadata", None)) new_course.start = datetime.datetime.now(UTC).replace(microsecond=0) # The rest of kwargs become attributes on the course: for k, v in kwargs.iteritems(): setattr(new_course, k, v) # Update the data in the mongo datastore store.save_xmodule(new_course) return new_course
def test_clean(self, pair): self.assertEquals(Location.clean(pair[0]), pair[1])
def create_new_course(request): """ Create a new course """ if not is_user_in_creator_group(request.user): raise PermissionDenied() org = request.POST.get('org') number = request.POST.get('number') display_name = request.POST.get('display_name') try: dest_location = Location('i4x', org, number, 'course', Location.clean(display_name)) except InvalidLocationError as error: return JsonResponse({ "ErrMsg": "Unable to create course '{name}'.\n\n{err}".format( name=display_name, err=error.message) }) # see if the course already exists existing_course = None try: existing_course = modulestore('direct').get_item(dest_location) except ItemNotFoundError: pass if existing_course is not None: return JsonResponse( {'ErrMsg': 'There is already a course defined with this name.'}) course_search_location = [ 'i4x', dest_location.org, dest_location.course, 'course', None ] courses = modulestore().get_items(course_search_location) if len(courses) > 0: return JsonResponse({ 'ErrMsg': 'There is already a course defined with the same organization and course number.' }) # instantiate the CourseDescriptor and then persist it # note: no system to pass if display_name is None: metadata = {} else: metadata = {'display_name': display_name} modulestore('direct').create_and_save_xmodule(dest_location, metadata=metadata) new_course = modulestore('direct').get_item(dest_location) # clone a default 'about' overview module as well dest_about_location = dest_location.replace(category='about', name='overview') overview_template = AboutDescriptor.get_template('overview.yaml') modulestore('direct').create_and_save_xmodule( dest_about_location, system=new_course.system, definition_data=overview_template.get('data')) initialize_course_tabs(new_course) create_all_course_groups(request.user, new_course.location) # seed the forums seed_permissions_roles(new_course.location.course_id) return JsonResponse({'id': new_course.location.url()})