From 70d77ba284f1ffcd424e2fdc256e8a7d2bb709e0 Mon Sep 17 00:00:00 2001 From: Peter Weber Date: Tue, 5 Sep 2023 08:01:37 +0200 Subject: [PATCH] entity: better replace identifiedBy Co-Authored-by: Peter Weber --- .../entities/remote_entities/replace.py | 90 +++++++++++-------- 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/rero_ils/modules/entities/remote_entities/replace.py b/rero_ils/modules/entities/remote_entities/replace.py index 419ad3ad93..b27efb5120 100644 --- a/rero_ils/modules/entities/remote_entities/replace.py +++ b/rero_ils/modules/entities/remote_entities/replace.py @@ -18,7 +18,7 @@ """Replace identifiedBy with $ref from MEF.""" - +import contextlib from copy import deepcopy from datetime import datetime, timezone @@ -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}"' ) @@ -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 @@ -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."""