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, __ = self.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.get('content_son', content['_id']), location.run) assert not self.content_store.get_attr(asset_key, 'locked', False) attrs = self.content_store.get_attrs(asset_key) assert_in('uploadDate', attrs) assert not attrs.get('locked', False) self.content_store.set_attr(asset_key, 'locked', True) assert self.content_store.get_attr(asset_key, 'locked', False) attrs = self.content_store.get_attrs(asset_key) assert_in('locked', attrs) assert attrs['locked'] is True self.content_store.set_attrs(asset_key, {'miscel': 99}) assert_equals(self.content_store.get_attr(asset_key, 'miscel'), 99) asset_key = AssetLocation._from_deprecated_son( course_content[0].get('content_son', course_content[0]['_id']), location.run ) assert_raises( AttributeError, self.content_store.set_attr, asset_key, 'md5', 'ff1532598830e3feac91c2449eaa60d6' ) assert_raises( AttributeError, self.content_store.set_attrs, asset_key, {'foo': 9, 'md5': 'ff1532598830e3feac91c2449eaa60d6'} ) assert_raises( NotFoundError, self.content_store.get_attr, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), 'displayname' ) assert_raises( NotFoundError, self.content_store.set_attr, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), 'displayname', 'hello' ) assert_raises( NotFoundError, self.content_store.get_attrs, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus') ) assert_raises( NotFoundError, self.content_store.set_attrs, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), {'displayname': 'hello'} ) assert_raises( NotFoundError, self.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, __ = self.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.get('content_son', content['_id']), location.run) assert not self.content_store.get_attr(asset_key, 'locked', False) attrs = self.content_store.get_attrs(asset_key) assert_in('uploadDate', attrs) assert not attrs.get('locked', False) self.content_store.set_attr(asset_key, 'locked', True) assert self.content_store.get_attr(asset_key, 'locked', False) attrs = self.content_store.get_attrs(asset_key) assert_in('locked', attrs) assert attrs['locked'] is True self.content_store.set_attrs(asset_key, {'miscel': 99}) assert_equals(self.content_store.get_attr(asset_key, 'miscel'), 99) asset_key = AssetLocation._from_deprecated_son( course_content[0].get('content_son', course_content[0]['_id']), location.run ) assert_raises( AttributeError, self.content_store.set_attr, asset_key, 'md5', 'ff1532598830e3feac91c2449eaa60d6' ) assert_raises( AttributeError, self.content_store.set_attrs, asset_key, {'foo': 9, 'md5': 'ff1532598830e3feac91c2449eaa60d6'} ) assert_raises( NotFoundError, self.content_store.get_attr, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), 'displayname' ) assert_raises( NotFoundError, self.content_store.set_attr, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), 'displayname', 'hello' ) assert_raises( NotFoundError, self.content_store.get_attrs, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus') ) assert_raises( NotFoundError, self.content_store.set_attrs, Location('bogus', 'bogus', 'bogus', 'asset', 'bogus'), {'displayname': 'hello'} ) assert_raises( NotFoundError, self.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 # TODO: On 6/19/14, I had to put a try/except around this # to export a course. The course failed on JSON files in # the /static/ directory placed in it with an import. # # If this hasn't been looked at in a while, remove this comment. # # When debugging course exports, this might be a good place # to look. -- pmitros 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 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) store.delete(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 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_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, __ = self.content_store.get_all_content_for_course(location.course_key) assert_true(len(course_content) > 0) filter_params = _build_requested_filter("Images") filtered_course_content, __ = self.content_store.get_all_content_for_course( location.course_key, filter_params=filter_params ) assert_true(len(filtered_course_content) < len(course_content)) # 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.get("content_son", content["_id"]), location.run) assert not self.content_store.get_attr(asset_key, "locked", False) attrs = self.content_store.get_attrs(asset_key) assert_in("uploadDate", attrs) assert not attrs.get("locked", False) self.content_store.set_attr(asset_key, "locked", True) assert self.content_store.get_attr(asset_key, "locked", False) attrs = self.content_store.get_attrs(asset_key) assert_in("locked", attrs) assert attrs["locked"] is True self.content_store.set_attrs(asset_key, {"miscel": 99}) assert_equals(self.content_store.get_attr(asset_key, "miscel"), 99) asset_key = AssetLocation._from_deprecated_son( course_content[0].get("content_son", course_content[0]["_id"]), location.run ) assert_raises(AttributeError, self.content_store.set_attr, asset_key, "md5", "ff1532598830e3feac91c2449eaa60d6") assert_raises( AttributeError, self.content_store.set_attrs, asset_key, {"foo": 9, "md5": "ff1532598830e3feac91c2449eaa60d6"}, ) assert_raises( NotFoundError, self.content_store.get_attr, Location("bogus", "bogus", "bogus", "asset", "bogus"), "displayname", ) assert_raises( NotFoundError, self.content_store.set_attr, Location("bogus", "bogus", "bogus", "asset", "bogus"), "displayname", "hello", ) assert_raises( NotFoundError, self.content_store.get_attrs, Location("bogus", "bogus", "bogus", "asset", "bogus") ) assert_raises( NotFoundError, self.content_store.set_attrs, Location("bogus", "bogus", "bogus", "asset", "bogus"), {"displayname": "hello"}, ) assert_raises( NotFoundError, self.content_store.set_attrs, Location("bogus", "bogus", "bogus", "asset", None), {"displayname": "hello"}, )