From 4abe8a5bae4bfb9e8fdc40c9b5df1b4d6959a9f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Puiggen=C3=A9?= Date: Tue, 27 Sep 2022 11:36:12 +0200 Subject: [PATCH 1/2] Fix laboratory content type is not properly indexed --- CHANGES.rst | 1 + src/senaite/core/upgrade/v02_03_000.py | 52 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 9e511eb8b6..303e6e82e7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,7 @@ Changelog 2.3.0 (unreleased) ------------------ +- #2146 Fix "No object found for UID: " in report preview - #2143 Fix Traceback when using readonly decorator for objects w/o __name__ - #2140 Allow to enable/disable analysis categories for samples - #2137 Dynamic Workflow Menu diff --git a/src/senaite/core/upgrade/v02_03_000.py b/src/senaite/core/upgrade/v02_03_000.py index ab09941180..b8eab2c66f 100644 --- a/src/senaite/core/upgrade/v02_03_000.py +++ b/src/senaite/core/upgrade/v02_03_000.py @@ -22,6 +22,7 @@ from Products.Archetypes.config import REFERENCE_CATALOG from senaite.core import logger from senaite.core.catalog import ANALYSIS_CATALOG +from senaite.core.catalog import AUDITLOG_CATALOG from senaite.core.catalog import REPORT_CATALOG from senaite.core.catalog import SAMPLE_CATALOG from senaite.core.catalog import SETUP_CATALOG @@ -76,9 +77,13 @@ def upgrade(tool): # Ensure the catalog mappings for Analyses and Samples is correct # https://github.com/senaite/senaite.core/pull/2130 setup_catalog_mappings(portal, catalog_mappings=CATALOG_MAPPINGS) + # remap auditlog catalog setup_auditlog_catalog_mappings(portal) + # ensure the catalogs assigned to types are sorted correctly + setup_catalogs_order(portal) + # Add new setup folder to portal add_senaite_setup(portal) @@ -91,6 +96,7 @@ def upgrade(tool): move_arreports_to_report_catalog(portal) migrate_analysis_services_fields(portal) migrate_analyses_fields(portal) + reindex_laboratory(portal) logger.info("{0} upgraded to version {1}".format(product, version)) return True @@ -420,3 +426,49 @@ def fixed_point_value_to_string(value, precision): str_value = template % (sign, front, fra) # strip off trailing zeros and possible dot return str_value.rstrip("0").rstrip(".") + + +def setup_catalogs_order(portal): + """Ensures the order of catalogs portal types are bound to is correct + This is required because senaite.app.supermodel uses the first catalog + the portal type is associated with when retrieving brains + """ + logger.info("Setup Catalogs order ...") + + def sort_catalogs(id1, id2): + if id1 == id2: + return 0 + + # Catalogs sorted, senaite_* always first + senaite = map(lambda cat_id: cat_id.startswith("senaite_"), [id1, id2]) + if not all(senaite) and any(senaite): + # Item starting with senaite always gets max priority + if id1.startswith("senaite_"): + return -1 + return 1 + + # Auditlog catalog is always the last! + if id1 == AUDITLOG_CATALOG: + return 1 + if id2 == AUDITLOG_CATALOG: + return -1 + + if id1 < id2: + return -1 + return 1 + + at = api.get_tool("archetype_tool") + for portal_type, catalogs in at.listCatalogs().items(): + sorted_catalogs = sorted(catalogs, cmp=sort_catalogs) + at.setCatalogsByType(portal_type, sorted_catalogs) + + logger.info("Setup Catalogs order [DONE]") + + +def reindex_laboratory(portal): + """Forces the reindex of laboratory content type + """ + logger.info("Reindexing laboratory content type ...") + setup = api.get_setup() + setup.laboratory.reindexObject() + logger.info("Reindexing laboratory content type [DONE]") From 70b363d487e34653e137c3013f7ced38cc822cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Puiggen=C3=A9?= Date: Tue, 27 Sep 2022 12:52:55 +0200 Subject: [PATCH 2/2] Move setup_catalogs_order to setuphandlers --- src/senaite/core/setuphandlers.py | 38 +++++++++++++++++++++++++ src/senaite/core/upgrade/v02_03_000.py | 39 +------------------------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/senaite/core/setuphandlers.py b/src/senaite/core/setuphandlers.py index 69377f3ddb..95d384d35c 100644 --- a/src/senaite/core/setuphandlers.py +++ b/src/senaite/core/setuphandlers.py @@ -226,6 +226,7 @@ def install(context): setup_other_catalogs(portal) setup_catalog_mappings(portal) setup_auditlog_catalog_mappings(portal) + setup_catalogs_order(portal) setup_content_structure(portal) add_senaite_setup(portal) add_dexterity_portal_items(portal) @@ -480,6 +481,43 @@ def setup_auditlog_catalog_mappings(portal): AUDITLOG_CATALOG, portal_type)) +def setup_catalogs_order(portal): + """Ensures the order of catalogs portal types are bound to is correct + This is required because senaite.app.supermodel uses the first catalog + the portal type is associated with when retrieving brains + """ + logger.info("Setup Catalogs order ...") + + def sort_catalogs(id1, id2): + if id1 == id2: + return 0 + + # Audit-log catalog is always the last! + if id1 == AUDITLOG_CATALOG: + return 1 + if id2 == AUDITLOG_CATALOG: + return -1 + + # Catalogs sorted, senaite_* always first + senaite = map(lambda cat_id: cat_id.startswith("senaite_"), [id1, id2]) + if not all(senaite) and any(senaite): + # Item starting with senaite always gets max priority + if id1.startswith("senaite_"): + return -1 + return 1 + + if id1 < id2: + return -1 + return 1 + + at = api.get_tool("archetype_tool") + for portal_type, catalogs in at.listCatalogs().items(): + sorted_catalogs = sorted(catalogs, cmp=sort_catalogs) + at.setCatalogsByType(portal_type, sorted_catalogs) + + logger.info("Setup Catalogs order [DONE]") + + def remove_default_content(portal): """Remove default Plone contents """ diff --git a/src/senaite/core/upgrade/v02_03_000.py b/src/senaite/core/upgrade/v02_03_000.py index b8eab2c66f..d23a0aaa11 100644 --- a/src/senaite/core/upgrade/v02_03_000.py +++ b/src/senaite/core/upgrade/v02_03_000.py @@ -22,7 +22,6 @@ from Products.Archetypes.config import REFERENCE_CATALOG from senaite.core import logger from senaite.core.catalog import ANALYSIS_CATALOG -from senaite.core.catalog import AUDITLOG_CATALOG from senaite.core.catalog import REPORT_CATALOG from senaite.core.catalog import SAMPLE_CATALOG from senaite.core.catalog import SETUP_CATALOG @@ -33,6 +32,7 @@ from senaite.core.setuphandlers import add_senaite_setup from senaite.core.setuphandlers import setup_auditlog_catalog_mappings from senaite.core.setuphandlers import setup_catalog_mappings +from senaite.core.setuphandlers import setup_catalogs_order from senaite.core.setuphandlers import setup_core_catalogs from senaite.core.upgrade import upgradestep from senaite.core.upgrade.utils import UpgradeUtils @@ -428,43 +428,6 @@ def fixed_point_value_to_string(value, precision): return str_value.rstrip("0").rstrip(".") -def setup_catalogs_order(portal): - """Ensures the order of catalogs portal types are bound to is correct - This is required because senaite.app.supermodel uses the first catalog - the portal type is associated with when retrieving brains - """ - logger.info("Setup Catalogs order ...") - - def sort_catalogs(id1, id2): - if id1 == id2: - return 0 - - # Catalogs sorted, senaite_* always first - senaite = map(lambda cat_id: cat_id.startswith("senaite_"), [id1, id2]) - if not all(senaite) and any(senaite): - # Item starting with senaite always gets max priority - if id1.startswith("senaite_"): - return -1 - return 1 - - # Auditlog catalog is always the last! - if id1 == AUDITLOG_CATALOG: - return 1 - if id2 == AUDITLOG_CATALOG: - return -1 - - if id1 < id2: - return -1 - return 1 - - at = api.get_tool("archetype_tool") - for portal_type, catalogs in at.listCatalogs().items(): - sorted_catalogs = sorted(catalogs, cmp=sort_catalogs) - at.setCatalogsByType(portal_type, sorted_catalogs) - - logger.info("Setup Catalogs order [DONE]") - - def reindex_laboratory(portal): """Forces the reindex of laboratory content type """