def test_export_with_None_value(self): """ Export and import a single AssetMetadata to XML with a None created_by field, without causing an exception. """ asset_md = AssetMetadata(self.course_id.make_asset_key("asset", "none_value"), created_by=None) asset = etree.Element("asset") asset_md.to_xml(asset) asset_md.from_xml(asset)
def test_export_with_None_value(self): """ Export and import a single AssetMetadata to XML with a None created_by field, without causing an exception. """ asset_md = AssetMetadata( self.course_id.make_asset_key('asset', 'none_value'), created_by=None, ) asset = etree.Element("asset") asset_md.to_xml(asset) asset_md.from_xml(asset)
def _import_course_asset_metadata(store, data_dir, course_id, raise_on_failure): """ Read in assets XML file, parse it, and add all asset metadata to the modulestore. """ asset_dir = path(data_dir) / AssetMetadata.EXPORTED_ASSET_DIR assets_filename = AssetMetadata.EXPORTED_ASSET_FILENAME asset_xml_file = asset_dir / assets_filename def make_asset_id(course_id, asset_xml): """ Construct an asset ID out of a complete asset XML section. """ asset_type = None asset_name = None for child in asset_xml.iterchildren(): if child.tag == AssetMetadata.ASSET_TYPE_ATTR: asset_type = child.text elif child.tag == AssetMetadata.ASSET_BASENAME_ATTR: asset_name = child.text return course_id.make_asset_key(asset_type, asset_name) all_assets = [] try: xml_data = etree.parse(asset_xml_file).getroot() assert (xml_data.tag == AssetMetadata.ALL_ASSETS_XML_TAG) for asset in xml_data.iterchildren(): if asset.tag == AssetMetadata.ASSET_XML_TAG: # Construct the asset key. asset_key = make_asset_id(course_id, asset) asset_md = AssetMetadata(asset_key) asset_md.from_xml(asset) all_assets.append(asset_md) except IOError: logging.info('No {} file is present with asset metadata.'.format( assets_filename)) return except Exception: # pylint: disable=W0703 logging.exception('Error while parsing asset xml.') if raise_on_failure: raise else: return # Now add all asset metadata to the modulestore. if len(all_assets) > 0: store.save_asset_metadata_list(all_assets, all_assets[0].edited_by, import_only=True)
def import_asset_metadata(self, data_dir, course_id): """ Read in assets XML file, parse it, and add all asset metadata to the modulestore. """ asset_dir = path(data_dir) / AssetMetadata.EXPORTED_ASSET_DIR assets_filename = AssetMetadata.EXPORTED_ASSET_FILENAME asset_xml_file = asset_dir / assets_filename def make_asset_id(course_id, asset_xml): """ Construct an asset ID out of a complete asset XML section. """ asset_type = None asset_name = None for child in asset_xml.iterchildren(): if child.tag == AssetMetadata.ASSET_TYPE_ATTR: asset_type = child.text elif child.tag == AssetMetadata.ASSET_BASENAME_ATTR: asset_name = child.text return course_id.make_asset_key(asset_type, asset_name) all_assets = [] try: xml_data = etree.parse(asset_xml_file).getroot() # pylint: disable=no-member assert xml_data.tag == AssetMetadata.ALL_ASSETS_XML_TAG for asset in xml_data.iterchildren(): if asset.tag == AssetMetadata.ASSET_XML_TAG: # Construct the asset key. asset_key = make_asset_id(course_id, asset) asset_md = AssetMetadata(asset_key) asset_md.from_xml(asset) all_assets.append(asset_md) except IOError: logging.info('No %s file is present with asset metadata.', assets_filename) return except Exception: # pylint: disable=W0703 logging.exception('Error while parsing asset xml.') if self.raise_on_failure: raise else: return # Now add all asset metadata to the modulestore. if len(all_assets) > 0: self.store.save_asset_metadata_list(all_assets, all_assets[0].edited_by, import_only=True)
def test_export_single_asset_to_from_xml(self): """ Export a single AssetMetadata to XML and verify the structure and fields. """ asset_md = self.course_assets[0] root = etree.Element("assets") asset = etree.SubElement(root, "asset") asset_md.to_xml(asset) # If this line does *not* raise, the XML is valid. etree.fromstring(etree.tostring(root), self.xmlparser) new_asset_key = self.course_id.make_asset_key('tmp', 'tmp') new_asset_md = AssetMetadata(new_asset_key) new_asset_md.from_xml(asset) # Compare asset_md to new_asset_md. for attr in AssetMetadata.ALL_ATTRS: orig_value = getattr(asset_md, attr) new_value = getattr(new_asset_md, attr) self.assertEqual(orig_value, new_value)