Skip to content

Commit

Permalink
entity: better replace identifiedBy
Browse files Browse the repository at this point in the history
Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep committed Oct 2, 2023
1 parent 6106294 commit 70d77ba
Showing 1 changed file with 53 additions and 37 deletions.
90 changes: 53 additions & 37 deletions rero_ils/modules/entities/remote_entities/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

"""Replace identifiedBy with $ref from MEF."""


import contextlib
from copy import deepcopy
from datetime import datetime, timezone

Expand Down Expand Up @@ -177,31 +177,46 @@ def _do_entity(self, entity, doc_pid):
new_source, new_source_pid = self._find_other_source(
source, source_pid, mef_data)
if new_source:
self._create_entity(mef_type, mef_data)
authorized_access_point = entity[
"entity"]["authorized_access_point"]
mef_authorized_access_point = mef_data[
new_source]["authorized_access_point"]
self.logger.info(
f'Replace document:{doc_pid} '
f'{self.field} "{authorized_access_point}" - '
f'({mef_type}) {new_source}:{new_source_pid} '
f'"{mef_authorized_access_point}"'
)
entity['entity'] = {
'$ref': (
f'{self._get_base_url(mef_type)}'
f'/{new_source}/{new_source_pid}'
),
'pid': mef_data['pid']
}
changed = True
mef_entity_type = mef_data.get('type')
# verify local and MEF type are the same
if mef_entity_type == doc_entity_type:
self._create_entity(mef_type, mef_data)
authorized_access_point = entity[
"entity"]["authorized_access_point"]
mef_authorized_access_point = mef_data[
new_source]["authorized_access_point"]
self.logger.info(
f'Replace document:{doc_pid} '
f'{self.field} "{authorized_access_point}" - '
f'({mef_type}) {new_source}:{new_source_pid} '
f'"{mef_authorized_access_point}"'
)
entity['entity'] = {
'$ref': (
f'{self._get_base_url(mef_type)}'
f'/{new_source}/{new_source_pid}'
),
'pid': mef_data['pid']
}
changed = True
else:
authorized_access_point = mef_data.get(
source, {}).get('authorized_access_point')
info = (
f'{doc_entity_type} != {mef_entity_type} '
f': "{authorized_access_point}"'
)
self.rero_only[identifier] = info
self.logger.warning(
f'Type differ:{doc_pid} '
f'{self.field} - ({mef_type}) {identifier} {info}'
)
else:
authorized_access_point = mef_data.get(
source, {}).get('authorized_access_point')
info = f'{doc_entity_type}: {authorized_access_point}'
self.rero_only[identifier] = info
self.logger.warning(
self.logger.info(
f'No other source found for document:{doc_pid} '
f'{self.field} - ({mef_type}) {identifier} "{info}"'
)
Expand All @@ -210,9 +225,9 @@ def _do_entity(self, entity, doc_pid):
'entity']['authorized_access_point']
info = f'{doc_entity_type}: {authorized_access_point}'
self.not_found[identifier] = info
self.logger.warning(
self.logger.info(
f'No MEF found for document:{doc_pid} '
f'{self.parent} - ({mef_type}) {identifier} "{info}"'
f' - ({mef_type}) {identifier} "{info}"'
)
return changed

Expand All @@ -222,20 +237,21 @@ def _replace_entities_in_document(self, doc_id):
:param doc_id: (string) document id
"""
changed = False
doc = Document.get_record(doc_id)
entities_to_update = filter(
lambda c: c.get('entity', {}).get('identifiedBy'),
doc.get(self.field, {})
)
for entity in entities_to_update:
try:
changed = self._do_entity(entity, doc.pid)
except Exception as err:
self.logger.error(
f'Error document:{doc.pid} {entity} {err}"'
)
if changed:
return doc
with contextlib.suppress(Exception):
doc = Document.get_record(doc_id)
entities_to_update = filter(
lambda c: c.get('entity', {}).get('identifiedBy'),
doc.get(self.field, {})
)
for entity in entities_to_update:
try:
changed = self._do_entity(entity, doc.pid) or changed
except Exception as err:
self.logger.error(
f'Error document:{doc.pid} {entity} {err}"'
)
if changed:
return doc

def run(self):
"""Replace identifiedBy with $ref."""
Expand Down

0 comments on commit 70d77ba

Please sign in to comment.