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_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 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 _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 _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 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)