def _fill_form_list(form_list, metadata: Metadata, dataset: Dataset, user: MrMapUser): """ Iterates over all forms and applies the metadata changes on the objects Args: form_list: The list of forms metadata: The metadata record dataset: The dataset record user: The performing user Returns: """ function_map = { "DatasetIdentificationForm": DatasetWizard._fill_metadata_dataset_identification_form, "DatasetResponsiblePartyForm": DatasetWizard._fill_metadata_dataset_responsible_party_form, "DatasetClassificationForm": DatasetWizard._fill_metadata_dataset_classification_form, "DatasetSpatialExtentForm": DatasetWizard._fill_metadata_dataset_spatial_extent_form, "DatasetLicenseConstraintsForm": DatasetWizard._fill_metadata_dataset_licence_form, "DatasetQualityForm": DatasetWizard._fill_metadata_dataset_quality_form, } for form in form_list: form_class = type(form).__name__ function_map[form_class](form.cleaned_data, metadata, dataset, user) dataset.save() metadata.is_custom = True metadata.save() try: doc = Document.objects.get( metadata__id=metadata.id, document_type=DocumentEnum.METADATA.value, is_original=False, ) doc.is_active = metadata.is_active DatasetWizard._overwrite_dataset_document(metadata, doc) except ObjectDoesNotExist: DatasetWizard._create_dataset_document(metadata)
def overwrite_metadata(original_md: Metadata, custom_md: Metadata, editor_form): """ Overwrites the original data with the custom date Args: original_md (Metadata): The original Metadata object custom_md (Metadata): The custom Metadata object editor_form: The editor form which holds additional data Returns: nothing """ original_md.title = custom_md.title original_md.abstract = custom_md.abstract original_md.access_constraints = custom_md.access_constraints # we need the metadata_url to reset dataset metadatas # original_md.metadata_url = custom_md.metadata_url original_md.licence = custom_md.licence # get db objects from values # Keyword updating keywords = editor_form.cleaned_data["keywords"] original_md.keywords.clear() for kw in keywords: keyword = Keyword.objects.get_or_create(keyword=kw)[0] original_md.keywords.add(keyword) # Language updating original_md.language_code = editor_form.cleaned_data["language_code"] # Categories updating # Categories are provided as id's to prevent language related conflicts try: categories = editor_form.cleaned_data["categories"] original_md.categories.clear() for category in categories: original_md.categories.add(category) except KeyError: pass # Categories are inherited by subelements subelements = original_md.get_described_element().get_subelements( ).select_related('metadata') for subelement in subelements: subelement.metadata.categories.clear() for category in categories: subelement.metadata.categories.add(category) # change capabilities document so that all sensitive elements (links) are proxied if original_md.use_proxy_uri != custom_md.use_proxy_uri: if custom_md.use_proxy_uri == 'on': original_md.set_proxy(True) else: original_md.set_proxy(False) # save metadata original_md.is_custom = True original_md.save() if original_md.is_dataset_metadata: overwrite_dataset_metadata_document(original_md) else: overwrite_capabilities_document(original_md)
def update_metadata(old: Metadata, new: Metadata, keep_custom_md: bool): """ Overwrites existing metadata (old) with newer content (new). Database related information like id, created_by, and so on is saved before and written back after overwriting. Args: old (Metadata): The existing metadata, that shall be overwritten new (Metadata): The new metadata that is used for overwriting Returns: old (Metadata): The overwritten metadata """ # reset update candidate new.is_update_candidate_for = None # Save important persistance information _id = old.id created_by = old.created_by created_on = old.created activated = old.is_active metadata_type = old.metadata_type metadata_is_custom = old.is_custom # If needed, cache custom metadata custom_md = {} if keep_custom_md: custom_md_fields = MetadataEditorForm._meta.fields for field in custom_md_fields: custom_md[field] = old.__getattribute__(field) del custom_md_fields # Overwrite old information with new one old = deepcopy(new) old.id = _id old.created = created_on old.created_by = created_by old.is_active = activated old.metadata_type = metadata_type # reference systems old.reference_system.clear() old.reference_system.add(*new.reference_system.all()) # Dimensions old.dimensions.clear() old.dimensions.add(*new.dimensions.all()) # formats old.formats.clear() old.formats.add(*new.formats_list) # Restore custom metadata if needed if keep_custom_md: old.is_custom = metadata_is_custom for key, val in custom_md.items(): # ManyRelatedManagers have to be handled differently try: old.__setattr__(key, val) except TypeError: # If the above simple attribute setter fails, we are dealing with a ManyRelatedManager, that has to be # handled differently field = val.prefetch_cache_name old_manager = old.__getattribute__(field) old_manager_elems = old_manager.all() custom_m2m_elements = val.all() for elem in custom_m2m_elements: if elem not in old_manager_elems: old_manager.add(elem) else: # Keywords updating without keeping custom md old.keywords.clear() old.keywords.add(*new.keywords.all()) old.last_modified = timezone.now() return old