def clear_courses(): # Flush and initialize the module store # Note that if your test module gets in some weird state # (though it shouldn't), do this manually # from the bash shell to drop it: # $ mongo test_xmodule --eval "db.dropDatabase()" editable_modulestore().collection.drop() contentstore().fs_files.drop()
def _create_item(self, category, name, data, metadata, parent_category, parent_name, runtime): location = self.course.location.replace(category=category, name=name) editable_modulestore('direct').create_and_save_xmodule(location, data, metadata, runtime) if parent_name: # add child to parent in mongo parent_location = self.course.location.replace(category=parent_category, name=parent_name) parent = editable_modulestore('direct').get_item(parent_location) parent.children.append(location.url()) editable_modulestore('direct').update_children(parent_location, parent.children)
def _create_item(self, category, name, data, metadata, parent_category, parent_name, runtime): location = self.course.location.replace(category=category, name=name) editable_modulestore('direct').create_and_save_xmodule( location, data, metadata, runtime) if parent_name: # add child to parent in mongo parent_location = self.course.location.replace( category=parent_category, name=parent_name) parent = editable_modulestore('direct').get_item(parent_location) parent.children.append(location.url()) editable_modulestore('direct').update_children( parent_location, parent.children)
def drop_mongo_collections(store_name='default'): """ If using a Mongo-backed modulestore & contentstore, drop the collections. """ # This will return the mongo-backed modulestore # even if we're using a mixed modulestore store = editable_modulestore(store_name) if hasattr(store, 'collection'): connection = store.collection.database.connection store.collection.drop() connection.close() elif hasattr(store, 'close_all_connections'): store.close_all_connections() elif hasattr(store, 'db'): connection = store.db.connection connection.drop_database(store.db.name) connection.close() if contentstore().fs_files: db = contentstore().fs_files.database db.connection.drop_database(db) db.connection.close() location_mapper = loc_mapper() if location_mapper.db: location_mapper.location_map.drop() location_mapper.db.connection.close()
def setUp(self): self.store = editable_modulestore() self.course = CourseFactory.create() self.course.days_early_for_beta = 5 self.course.enrollment_start = datetime.datetime.now( UTC) + datetime.timedelta(days=3) self.store.save_xmodule(self.course)
def drop_mongo_collections(store_name="default"): """ If using a Mongo-backed modulestore & contentstore, drop the collections. """ # This will return the mongo-backed modulestore # even if we're using a mixed modulestore store = editable_modulestore(store_name) if hasattr(store, "collection"): connection = store.collection.database.connection store.collection.drop() connection.close() elif hasattr(store, "close_all_connections"): store.close_all_connections() elif hasattr(store, "db"): connection = store.db.connection connection.drop_database(store.db.name) connection.close() if contentstore().fs_files: db = contentstore().fs_files.database db.connection.drop_database(db) db.connection.close() location_mapper = loc_mapper() if location_mapper.db: location_mapper.location_map.drop() location_mapper.db.connection.close()
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(cls, target_class, **kwargs): """ Uses ``**kwargs``: :parent_location: (required): the location of the parent module (e.g. the parent course or section) :category: the category of the resulting item. :data: (optional): the data for the item (e.g. XML problem definition for a problem item) :display_name: (optional): the display name of the item :metadata: (optional): dictionary of metadata attributes :boilerplate: (optional) the boilerplate for overriding field values :target_class: is ignored """ DETACHED_CATEGORIES = ['about', 'static_tab', 'course_info'] # catch any old style users before they get into trouble assert not 'template' in kwargs parent_location = Location(kwargs.get('parent_location')) data = kwargs.get('data') category = kwargs.get('category') display_name = kwargs.get('display_name') metadata = kwargs.get('metadata', {}) location = kwargs.get( 'location', XModuleItemFactory.location(parent_location, category, display_name)) assert location != parent_location if kwargs.get('boilerplate') is not None: template_id = kwargs.get('boilerplate') clz = XModuleDescriptor.load_class(category) template = clz.get_template(template_id) assert template is not None metadata.update(template.get('metadata', {})) if not isinstance(data, basestring): data.update(template.get('data')) store = editable_modulestore('direct') # This code was based off that in cms/djangoapps/contentstore/views.py parent = store.get_item(parent_location) # replace the display name with an optional parameter passed in from the caller if display_name is not None: metadata['display_name'] = display_name store.create_and_save_xmodule(location, metadata=metadata, definition_data=data) if location.category not in DETACHED_CATEGORIES: parent.children.append(location.url()) store.update_children(parent_location, parent.children) return store.get_item(location)
def test_translation_static_transcript(self): """ Set course static_asset_path and ensure we get redirected to that path if it isn't found in the contentstore """ self.course.static_asset_path = 'dummy/static' self.course.save() store = editable_modulestore() store.update_item(self.course, 'OEoXaMPEzfM') # Test youtube style en request = Request.blank('/translation/en?videoId=12345') response = self.item.transcript(request=request, dispatch='translation/en') self.assertEqual(response.status, '307 Temporary Redirect') self.assertIn( ('Location', '/static/dummy/static/subs_12345.srt.sjson'), response.headerlist ) # Test HTML5 video style self.item.sub = 'OEoXaMPEzfM' request = Request.blank('/translation/en') response = self.item.transcript(request=request, dispatch='translation/en') self.assertEqual(response.status, '307 Temporary Redirect') self.assertIn( ('Location', '/static/dummy/static/subs_OEoXaMPEzfM.srt.sjson'), response.headerlist ) # Test different language to ensure we are just ignoring it since we can't # translate with static fallback request = Request.blank('/translation/uk') response = self.item.transcript(request=request, dispatch='translation/uk') self.assertEqual(response.status, '404 Not Found')
def test_translation_static_transcript(self): """ Set course static_asset_path and ensure we get redirected to that path if it isn't found in the contentstore """ self.course.static_asset_path = "dummy/static" self.course.save() store = editable_modulestore() store.update_item(self.course, "OEoXaMPEzfM") # Test youtube style en request = Request.blank("/translation/en?videoId=12345") response = self.item.transcript(request=request, dispatch="translation/en") self.assertEqual(response.status, "307 Temporary Redirect") self.assertIn(("Location", "/static/dummy/static/subs_12345.srt.sjson"), response.headerlist) # Test HTML5 video style self.item.sub = "OEoXaMPEzfM" request = Request.blank("/translation/en") response = self.item.transcript(request=request, dispatch="translation/en") self.assertEqual(response.status, "307 Temporary Redirect") self.assertIn(("Location", "/static/dummy/static/subs_OEoXaMPEzfM.srt.sjson"), response.headerlist) # Test different language to ensure we are just ignoring it since we can't # translate with static fallback request = Request.blank("/translation/uk") response = self.item.transcript(request=request, dispatch="translation/uk") self.assertEqual(response.status, "404 Not Found")
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 setUp(self): self.store = editable_modulestore() self.factory = RequestFactory() self.course = CourseFactory.create() self.course.days_early_for_beta = 5 self.course.enrollment_start = datetime.datetime.now(UTC) + datetime.timedelta(days=3) self.store.update_item(self.course)
def add_grading_policy(self, grading_policy): """ Add a grading policy to the course. """ self.course.grading_policy = grading_policy store = editable_modulestore() store.update_item(self.course, '**replace_user**') self.refresh_course()
def add_grading_policy(self, grading_policy): """ Add a grading policy to the course. """ course_data = {'grading_policy': grading_policy} store = editable_modulestore('direct') store.update_item(self.course.location, course_data) self.refresh_course()
def add_grading_policy(self, grading_policy): """ Add a grading policy to the course. """ course_data = {"grading_policy": grading_policy} store = editable_modulestore("direct") store.update_item(self.course.location, course_data) self.refresh_course()
def _create(cls, target_class, **kwargs): """ Uses ``**kwargs``: :parent_location: (required): the location of the parent module (e.g. the parent course or section) :category: the category of the resulting item. :data: (optional): the data for the item (e.g. XML problem definition for a problem item) :display_name: (optional): the display name of the item :metadata: (optional): dictionary of metadata attributes :boilerplate: (optional) the boilerplate for overriding field values :target_class: is ignored """ DETACHED_CATEGORIES = ['about', 'static_tab', 'course_info'] # catch any old style users before they get into trouble assert not 'template' in kwargs parent_location = Location(kwargs.get('parent_location')) data = kwargs.get('data') category = kwargs.get('category') display_name = kwargs.get('display_name') metadata = kwargs.get('metadata', {}) location = kwargs.get('location', XModuleItemFactory.location(parent_location, category, display_name)) assert location != parent_location if kwargs.get('boilerplate') is not None: template_id = kwargs.get('boilerplate') clz = XModuleDescriptor.load_class(category) template = clz.get_template(template_id) assert template is not None metadata.update(template.get('metadata', {})) if not isinstance(data, basestring): data.update(template.get('data')) store = editable_modulestore('direct') # This code was based off that in cms/djangoapps/contentstore/views.py parent = store.get_item(parent_location) # replace the display name with an optional parameter passed in from the caller if display_name is not None: metadata['display_name'] = display_name store.create_and_save_xmodule(location, metadata=metadata, definition_data=data) if location.category not in DETACHED_CATEGORIES: parent.children.append(location.url()) store.update_children(parent_location, parent.children) return store.get_item(location)
def drop_mongo_collection(): """ If using a Mongo-backed modulestore, drop the collection. """ # This will return the mongo-backed modulestore # even if we're using a mixed modulestore store = editable_modulestore() if hasattr(store, 'collection'): store.collection.drop()
def update_course(course): """ Updates the version of course in the modulestore 'course' is an instance of CourseDescriptor for which we want to update metadata. """ store = editable_modulestore() store.update_item(course, '**replace_user**') updated_course = store.get_instance(course.id, course.location) return updated_course
def update_course(course): """ Updates the version of course in the modulestore 'course' is an instance of CourseDescriptor for which we want to update metadata. """ store = editable_modulestore('direct') store.update_item(course, '**replace_user**') updated_course = store.get_instance(course.id, course.location) return updated_course
def drop_mongo_collections(): """ If using a Mongo-backed modulestore & contentstore, drop the collections. """ # This will return the mongo-backed modulestore # even if we're using a mixed modulestore store = editable_modulestore() if hasattr(store, 'collection'): store.collection.drop() if contentstore().fs_files: db = contentstore().fs_files.database db.connection.drop_database(db)
def update_course(course, data): """ Updates the version of course in the modulestore with the metadata in 'data' and returns the updated version. 'course' is an instance of CourseDescriptor for which we want to update metadata. 'data' is a dictionary with an entry for each CourseField we want to update. """ store = editable_modulestore('direct') store.update_metadata(course.location, data) updated_course = store.get_instance(course.id, course.location) return updated_course
def setUp(self): self.store = editable_modulestore() self.course = CourseFactory.create(org='Stanford', number='456', display_name='NO SHIB') self.shib_course = CourseFactory.create(org='Stanford', number='123', display_name='Shib Only') self.shib_course.enrollment_domain = 'shib:https://idp.stanford.edu/' self.store.update_item(self.shib_course, '**replace_user**') self.user_w_map = UserFactory.create(email='*****@*****.**') self.extauth = ExternalAuthMap(external_id='*****@*****.**', external_email='*****@*****.**', external_domain='shib:https://idp.stanford.edu/', external_credentials="", user=self.user_w_map) self.user_w_map.save() self.extauth.save() self.user_wo_map = UserFactory.create(email='*****@*****.**') self.user_wo_map.save()
def initialize_course(self): """Create a course in the store, with a chapter and section.""" self.module_store = editable_modulestore() # Create the course self.course = CourseFactory.create(org=TEST_COURSE_ORG, number=TEST_COURSE_NUMBER, display_name=TEST_COURSE_NAME) # Add a chapter to the course chapter = ItemFactory.create(parent_location=self.course.location, display_name=TEST_SECTION_NAME) # add a sequence to the course to which the problems can be added self.problem_section = ItemFactory.create(parent_location=chapter.location, category='sequential', display_name=TEST_SECTION_NAME)
def setUp(self): self.store = editable_modulestore()
def modulestore(self): # Delayed import so that we only depend on django if the caller # hasn't provided their own modulestore from xmodule.modulestore.django import editable_modulestore return editable_modulestore('direct')