From 6d42ea288552ff593a008b8bc45661a21375d3ae Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Thu, 12 Sep 2019 14:48:46 +0200 Subject: [PATCH 1/5] use uninedexObject instead of reindexObject --- bika/lims/subscribers/auditlog.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bika/lims/subscribers/auditlog.py b/bika/lims/subscribers/auditlog.py index 7182e5e43b..c8e42ba4af 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): From e1e1e30fee2cf9d4a798374867439d1313f745e4 Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Thu, 12 Sep 2019 14:49:03 +0200 Subject: [PATCH 2/5] Do not perform manual unindexing --- bika/lims/subscribers/auditlog.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bika/lims/subscribers/auditlog.py b/bika/lims/subscribers/auditlog.py index c8e42ba4af..ccb4be9649 100644 --- a/bika/lims/subscribers/auditlog.py +++ b/bika/lims/subscribers/auditlog.py @@ -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) From e1b9aec833ecbb41e079a69ffad6e44fe64d094a Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Thu, 12 Sep 2019 14:49:37 +0200 Subject: [PATCH 3/5] Added upgrade step --- bika/lims/upgrade/v01_03_002.py | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/bika/lims/upgrade/v01_03_002.py b/bika/lims/upgrade/v01_03_002.py index b38a6ad73d..7c3cb020c5 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") + analyses = ac({"portal_type": types_to_check}) + total = len(analyses) + + logger.info("Checking %s brains in auditlog_catalog" % total) + + for num, brain in enumerate(analyses): + 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 From 1679ac5a8bcfd281fea5b5afb2d259858380c0eb Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Thu, 12 Sep 2019 14:55:55 +0200 Subject: [PATCH 4/5] Changelog updated --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) 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 From 8ef67cf98a2ae6224200f3cc9cb5742baaebabd4 Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Thu, 12 Sep 2019 14:58:15 +0200 Subject: [PATCH 5/5] Naming changed --- bika/lims/upgrade/v01_03_002.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bika/lims/upgrade/v01_03_002.py b/bika/lims/upgrade/v01_03_002.py index 7c3cb020c5..4a493fab51 100644 --- a/bika/lims/upgrade/v01_03_002.py +++ b/bika/lims/upgrade/v01_03_002.py @@ -73,12 +73,12 @@ def unindex_orphaned_brains_in_auditlog_catalog(portal): orphaned = [] types_to_check = ["Analysis", "Attachment"] ac = api.get_tool("auditlog_catalog") - analyses = ac({"portal_type": types_to_check}) - total = len(analyses) + brains = ac({"portal_type": types_to_check}) + total = len(brains) logger.info("Checking %s brains in auditlog_catalog" % total) - for num, brain in enumerate(analyses): + for num, brain in enumerate(brains): if num % 100 == 0: logger.info("Checked %s/%s brains in auditlog_catalog" % (num, total))