def test_map_into_course_asset_location(self): loc = AssetLocation('org', 'course', 'run', 'asset', 'foo.bar') course_key = SlashSeparatedCourseKey("edX", "toy", "2012_Fall") self.assertEquals( AssetLocation("edX", "toy", "2012_Fall", 'asset', 'foo.bar'), loc.map_into_course(course_key) )
def test_contentstore_attrs(self): """ Test getting, setting, and defaulting the locked attr and arbitrary attrs. """ location = Location('edX', 'toy', '2012_Fall', 'course', '2012_Fall') course_content, __ = TestMongoModuleStore.content_store.get_all_content_for_course(location.course_key) assert_true(len(course_content) > 0) # a bit overkill, could just do for content[0] for content in course_content: assert not content.get('locked', False) asset_key = AssetLocation._from_deprecated_son(content['_id'], location.run) assert not TestMongoModuleStore.content_store.get_attr(asset_key, 'locked', False) attrs = TestMongoModuleStore.content_store.get_attrs(asset_key) assert_in('uploadDate', attrs) assert not attrs.get('locked', False) TestMongoModuleStore.content_store.set_attr(asset_key, 'locked', True) assert TestMongoModuleStore.content_store.get_attr(asset_key, 'locked', False) attrs = TestMongoModuleStore.content_store.get_attrs(asset_key) assert_in('locked', attrs) assert attrs['locked'] is True TestMongoModuleStore.content_store.set_attrs(asset_key, {'miscel': 99}) assert_equals(TestMongoModuleStore.content_store.get_attr(asset_key, 'miscel'), 99) asset_key = AssetLocation._from_deprecated_son(course_content[0]['_id'], location.run) assert_raises( AttributeError, TestMongoModuleStore.content_store.set_attr, asset_key, 'md5', 'ff1532598830e3feac91c2449eaa60d6' ) assert_raises( AttributeError, TestMongoModuleStore.content_store.set_attrs, asset_key, {'foo': 9, 'md5': 'ff1532598830e3feac91c2449eaa60d6'} ) assert_raises( NotFoundError, TestMongoModuleStore.content_store.get_attr, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), 'displayname' ) assert_raises( NotFoundError, TestMongoModuleStore.content_store.set_attr, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), 'displayname', 'hello' ) assert_raises( NotFoundError, TestMongoModuleStore.content_store.get_attrs, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus') ) assert_raises( NotFoundError, TestMongoModuleStore.content_store.set_attrs, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), {'displayname': 'hello'} ) assert_raises( NotFoundError, TestMongoModuleStore.content_store.set_attrs, Location('bogus', 'bogus', 'bogus', 'asset', None), {'displayname': 'hello'} )
def test_generate_thumbnail_image(self): contentStore = ContentStore() content = Content( AssetLocation(u'mitX', u'800', u'ignore_run', u'asset', u'monsters__.jpg'), None) (thumbnail_content, thumbnail_file_location) = contentStore.generate_thumbnail(content) self.assertIsNone(thumbnail_content) self.assertEqual( AssetLocation(u'mitX', u'800', u'ignore_run', u'thumbnail', u'monsters__.jpg'), thumbnail_file_location)
def compute_location(course_key, path, revision=None, is_thumbnail=False): """ Constructs a location object for static content. - course_key: the course that this asset belongs to - path: is the name of the static asset - revision: is the object's revision information - is_tumbnail: is whether or not we want the thumbnail version of this asset """ path = path.replace('/', '_') return AssetLocation(course_key.org, course_key.course, course_key.run, 'asset' if not is_thumbnail else 'thumbnail', AssetLocation.clean_keeping_underscores(path), revision)
def export_all_for_course(self, course_key, output_directory, assets_policy_file): """ Export all of this course's assets to the output_directory. Export all of the assets' attributes to the policy file. Args: course_key (CourseKey): the :class:`CourseKey` identifying the course output_directory: the directory under which to put all the asset files assets_policy_file: the filename for the policy file which should be in the same directory as the other policy files. """ policy = {} assets, __ = self.get_all_content_for_course(course_key) for asset in assets: asset_location = AssetLocation._from_deprecated_son( asset['_id'], course_key.run) # pylint: disable=protected-access self.export(asset_location, output_directory) for attr, value in asset.iteritems(): if attr not in [ '_id', 'md5', 'uploadDate', 'length', 'chunkSize' ]: policy.setdefault(asset_location.name, {})[attr] = value with open(assets_policy_file, 'w') as f: json.dump(policy, f)
def test_static_import(self): ''' Stuff in static_import should always be imported into contentstore ''' _, content_store, course = self.load_test_import_course() # make sure we have ONE asset in our contentstore ("should_be_imported.html") all_assets, count = content_store.get_all_content_for_course(course.id) print "len(all_assets)=%d" % len(all_assets) self.assertEqual(len(all_assets), 1) self.assertEqual(count, 1) content = None try: location = AssetLocation.from_deprecated_string( '/c4x/edX/test_import_course/asset/should_be_imported.html' ) content = content_store.find(location) except NotFoundError: pass self.assertIsNotNone(content) # make sure course.static_asset_path is correct print "static_asset_path = {0}".format(course.static_asset_path) self.assertEqual(course.static_asset_path, 'test_import_course')
def test_compute_location(self): # We had a bug that __ got converted into a single _. Make sure that substitution of INVALID_CHARS (like space) # still happen. asset_location = StaticContent.compute_location( SlashSeparatedCourseKey('mitX', '400', 'ignore'), 'subs__1eo_jXvZnE .srt.sjson') self.assertEqual( AssetLocation(u'mitX', u'400', u'ignore', u'asset', u'subs__1eo_jXvZnE_.srt.sjson', None), asset_location)
def _clear_assets(location): """ Clear all assets for location. """ store = contentstore() assets, __ = store.get_all_content_for_course(location.course_key) for asset in assets: asset_location = AssetLocation._from_deprecated_son(asset["_id"], location.course_key.run) del_cached_content(asset_location) mongo_id = asset_location.to_deprecated_son() store.delete(mongo_id)
def compute_location(course_key, path, revision=None, is_thumbnail=False): """ Constructs a location object for static content. - course_key: the course that this asset belongs to - path: is the name of the static asset - revision: is the object's revision information - is_tumbnail: is whether or not we want the thumbnail version of this asset """ path = path.replace('/', '_') return AssetLocation( course_key.org, course_key.course, course_key.run, 'asset' if not is_thumbnail else 'thumbnail', AssetLocation.clean_keeping_underscores(path), revision )
def test_pdf_asset(self): module_store = modulestore('direct') _, course_items = import_from_xml(module_store, 'common/test/data/', ['toy'], static_content_store=contentstore(), verbose=True) course = course_items[0] url = reverse_course_url('assets_handler', course.id) # Test valid contentType for pdf asset (textbook.pdf) resp = self.client.get(url, HTTP_ACCEPT='application/json') self.assertContains(resp, "/c4x/edX/toy/asset/textbook.pdf") asset_location = AssetLocation.from_deprecated_string( '/c4x/edX/toy/asset/textbook.pdf') content = contentstore().find(asset_location) # Check after import textbook.pdf has valid contentType ('application/pdf') # Note: Actual contentType for textbook.pdf in asset.json is 'text/pdf' self.assertEqual(content.content_type, 'application/pdf')
def export_all_for_course(self, course_key, output_directory, assets_policy_file): """ Export all of this course's assets to the output_directory. Export all of the assets' attributes to the policy file. Args: course_key (CourseKey): the :class:`CourseKey` identifying the course output_directory: the directory under which to put all the asset files assets_policy_file: the filename for the policy file which should be in the same directory as the other policy files. """ policy = {} assets, __ = self.get_all_content_for_course(course_key) for asset in assets: asset_location = AssetLocation._from_deprecated_son(asset['_id'], course_key.run) # pylint: disable=protected-access self.export(asset_location, output_directory) for attr, value in asset.iteritems(): if attr not in ['_id', 'md5', 'uploadDate', 'length', 'chunkSize']: policy.setdefault(asset_location.name, {})[attr] = value with open(assets_policy_file, 'w') as f: json.dump(policy, f)
def get_location_from_path(path): """ Generate an AssetKey for the given path (old c4x/org/course/asset/name syntax) """ return AssetLocation.from_deprecated_string(path)
def test_get_location_from_path(self): asset_location = StaticContent.get_location_from_path( u'/c4x/foo/bar/asset/images_course_image.jpg') self.assertEqual( AssetLocation(u'foo', u'bar', None, u'asset', u'images_course_image.jpg', None), asset_location)