From 3795cbc05d78553ce0d32d1f47affc6ee303b3e4 Mon Sep 17 00:00:00 2001 From: Paurikova2 Date: Wed, 13 Nov 2024 11:03:02 +0100 Subject: [PATCH] separate the filtering from the get_value method --- src/pump/_item.py | 13 +++++-------- src/pump/_metadata.py | 20 ++++++-------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/pump/_item.py b/src/pump/_item.py index 3147ed4..4f33436 100644 --- a/src/pump/_item.py +++ b/src/pump/_item.py @@ -31,7 +31,7 @@ class items: }], ] - ignored_fields = ["bitstream.redirectToURL"] + ignored_fields = ["dc.bitstream.redirectToURL"] def __init__(self, item_file_str: str, @@ -122,9 +122,6 @@ def import_to(self, cache_file, dspace, handles, metadatas, epersons, collection metadata, handle """ - # Convert ignored fields from text to id from v5 - self._ignored_fields_id = metadatas.get_field_id_from_text(self.ignored_fields) - if "ws" in self._done: _logger.info("Skipping workspace import") else: @@ -164,8 +161,8 @@ def _import_item(self, dspace, generic_item_d, item, handles, metadatas, eperson 'lastModified': item['last_modified'], 'withdrawn': item['withdrawn'] } - i_meta = (metadatas. - value(items.TYPE, i_id, None, True, self._ignored_fields_id)) + i_meta = metadatas.filter_res_d(metadatas.value( + items.TYPE, i_id, None, True), self.ignored_fields) if i_meta is not None: data['metadata'] = i_meta @@ -283,8 +280,8 @@ def _item_import_to(self, dspace, handles, metadatas, epersons, collections): 'withdrawn': item['withdrawn'] } - i_meta = metadatas.value(items.TYPE, i_id, None, - True, self._ignored_fields_id) + i_meta = metadatas.filter_res_d(metadatas.value( + items.TYPE, i_id, None, True, self.ignored_fields)) if i_meta: data['metadata'] = i_meta diff --git a/src/pump/_metadata.py b/src/pump/_metadata.py index bb50460..de9e3c0 100644 --- a/src/pump/_metadata.py +++ b/src/pump/_metadata.py @@ -536,18 +536,13 @@ def _get_key_v2(self, val): key += '.' + field_js['qualifier'] return key - def get_field_id_from_text(self, fields: list): + def filter_res_d(self, res_d, ignored_mtd_fields): """ - Check if filtered fields exist + Filter the resulting res_d dictionary based on custom conditions. """ - return [ - field_id - for field in fields - if (field_id := self._v5_fields_name2id.get(field)) is not None - and self.exists_field(field_id) - ] - - def value(self, res_type_id: int, res_id: int, text_for_field_id: int = None, log_missing: bool = True, ignored_mtd_fields: list = None): + return {key: val for key, val in res_d.items() if key not in ignored_mtd_fields} + + def value(self, res_type_id: int, res_id: int, text_for_field_id: int = None, log_missing: bool = True): """ Get metadata value for dspace object. """ @@ -564,10 +559,7 @@ def value(self, res_type_id: int, res_id: int, text_for_field_id: int = None, lo return None vals = tp_values[res_id] - - vals = [x for x in vals - if self.exists_field(x['metadata_field_id']) - and not (ignored_mtd_fields and any(x['metadata_field_id'] == field for field in ignored_mtd_fields))] + vals = [x for x in vals if self.exists_field(x['metadata_field_id'])] if len(vals) == 0: return {}