Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global Auditlog when Analyses/Attachments were removed #1439

Merged
merged 5 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions bika/lims/subscribers/auditlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
41 changes: 41 additions & 0 deletions bika/lims/upgrade/v01_03_002.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down