diff --git a/CHANGES.rst b/CHANGES.rst index f8d5064d33..85127e4d42 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -25,6 +25,7 @@ Changelog **Fixed** +- #1439 Fix global Auditlog when Analyses/Attachments were removed - #1426 Render HTML Texts in Info Popups correctly - #1423 Use the value set for ui_item property when displaying ReferenceWidget - #1425 Fix adapter priority for widget visibility diff --git a/bika/lims/subscribers/auditlog.py b/bika/lims/subscribers/auditlog.py index 7182e5e43b..ccb4be9649 100644 --- a/bika/lims/subscribers/auditlog.py +++ b/bika/lims/subscribers/auditlog.py @@ -30,7 +30,7 @@ def unindex_object(obj): """Unindex the object in the `auditlog_catalog` catalog """ auditlog_catalog = api.get_tool("auditlog_catalog") - auditlog_catalog.reindexObject(obj) + auditlog_catalog.unindexObject(obj) def ObjectTransitionedEventHandler(obj, event): @@ -102,8 +102,9 @@ def ObjectRemovedEventHandler(obj, event): if not supports_snapshots(obj): return - # unindex the object - unindex_object(obj) + # NOTE: It seems like the object is already unindexed and no further manual + # actions are needed here. + # unindex_object(obj) # freeze the object alsoProvides(obj, IDoNotSupportSnapshots) diff --git a/bika/lims/upgrade/v01_03_002.py b/bika/lims/upgrade/v01_03_002.py index b38a6ad73d..4a493fab51 100644 --- a/bika/lims/upgrade/v01_03_002.py +++ b/bika/lims/upgrade/v01_03_002.py @@ -18,6 +18,7 @@ # Copyright 2018-2019 by it's authors. # Some rights reserved, see README and LICENSE. +import transaction from bika.lims import api from bika.lims import logger from bika.lims.catalog.analysisrequest_catalog import \ @@ -56,9 +57,49 @@ def upgrade(tool): # Allow to detach a partition from its primary sample (#1420) update_partitions_role_mappings(portal) + # Unindex stale catalog brains from the auditlog_catalog + # https://github.com/senaite/senaite.core/issues/1438 + unindex_orphaned_brains_in_auditlog_catalog(portal) + logger.info("{0} upgraded to version {1}".format(product, version)) return True + +def unindex_orphaned_brains_in_auditlog_catalog(portal): + """Fetch deletable types from the auditlog_catalog and check if the objects + still exist. If the checkd analysis brains are orphaned, e.g. moved to a + partition, the brain will be unindexed. + """ + orphaned = [] + types_to_check = ["Analysis", "Attachment"] + ac = api.get_tool("auditlog_catalog") + brains = ac({"portal_type": types_to_check}) + total = len(brains) + + logger.info("Checking %s brains in auditlog_catalog" % total) + + for num, brain in enumerate(brains): + if num % 100 == 0: + logger.info("Checked %s/%s brains in auditlog_catalog" + % (num, total)) + try: + obj = brain.getObject() + obj._p_deactivate() + except AttributeError: + orphaned.append(brain) + + if orphaned: + logger.info("Unindexing %s orphaned brains in auditlog_catalog..." + % len(orphaned)) + + for num, brain in enumerate(orphaned): + logger.info("Unindexing %s/%s broken catalog brain" + % (num + 1, len(orphaned))) + ac.uncatalog_object(brain.getPath()) + + transaction.commit() + + def update_partitions_role_mappings(portal): """Updates the rolemappings for existing partitions that are in a suitable state, so they can be detached from the primary sample they belong to