Skip to content

Commit

Permalink
separate the filtering from the get_value method
Browse files Browse the repository at this point in the history
  • Loading branch information
Paurikova2 committed Nov 13, 2024
1 parent 045dabd commit 3795cbc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
13 changes: 5 additions & 8 deletions src/pump/_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class items:
}],
]

ignored_fields = ["bitstream.redirectToURL"]
ignored_fields = ["dc.bitstream.redirectToURL"]

def __init__(self,
item_file_str: str,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
20 changes: 6 additions & 14 deletions src/pump/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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 {}
Expand Down

0 comments on commit 3795cbc

Please sign in to comment.