From cf917593d604a9cf94b20f1f8cb2bb6864365a01 Mon Sep 17 00:00:00 2001 From: Lunga Baliwe Date: Mon, 2 Dec 2024 15:08:40 +0200 Subject: [PATCH] Subgroups sort by sort_key --- .../core/catalog/indexer/configure.zcml | 3 ++ src/senaite/core/catalog/indexer/subgroup.py | 32 +++++++++++++++++++ src/senaite/core/content/subgroup.py | 28 ++++++++++++++-- .../core/profiles/default/metadata.xml | 2 +- src/senaite/core/upgrade/v02_06_000.py | 10 ++++++ src/senaite/core/upgrade/v02_06_000.zcml | 8 +++++ 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/senaite/core/catalog/indexer/subgroup.py diff --git a/src/senaite/core/catalog/indexer/configure.zcml b/src/senaite/core/catalog/indexer/configure.zcml index 46ebfd713f..5edbe1a891 100644 --- a/src/senaite/core/catalog/indexer/configure.zcml +++ b/src/senaite/core/catalog/indexer/configure.zcml @@ -30,6 +30,9 @@ + + + diff --git a/src/senaite/core/catalog/indexer/subgroup.py b/src/senaite/core/catalog/indexer/subgroup.py new file mode 100644 index 0000000000..2c79f4b428 --- /dev/null +++ b/src/senaite/core/catalog/indexer/subgroup.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# +# This file is part of SENAITE.CORE. +# +# SENAITE.CORE is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation, version 2. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 51 +# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Copyright 2018-2024 by it's authors. +# Some rights reserved, see README and LICENSE. + +from plone.indexer import indexer +from Products.CMFPlone.utils import safe_callable +from senaite.core.catalog.utils import sortable_sortkey_title +from senaite.core.interfaces import ISubGroup + + +@indexer(ISubGroup) +def sortable_title(instance): + title = sortable_sortkey_title(instance) + if safe_callable(title): + title = title() + return title diff --git a/src/senaite/core/content/subgroup.py b/src/senaite/core/content/subgroup.py index b99e0c3324..823ba75f3c 100644 --- a/src/senaite/core/content/subgroup.py +++ b/src/senaite/core/content/subgroup.py @@ -27,7 +27,9 @@ from senaite.core.content.base import Container from senaite.core.interfaces import ISubGroup from zope import schema +from zope.interface import Invalid from zope.interface import implementer +from zope.interface import invariant class ISubGroupSchema(model.Schema): @@ -50,14 +52,36 @@ class ISubGroupSchema(model.Schema): required=False, ) - sort_key = schema.TextLine( + sort_key = schema.Float( title=_( - "title_subgroup_sortkey", + "title_subgroup_sort_key", default="Sort Key" ), + description=_( + u"description_subgroup_sort_key", + default=u"Float value from 0.0 - 1000.0 indicating the sort order." + " Duplicate values are ordered alphabetically.", required=False, ) + @invariant + def validate_sort_key(data): + """Checks sort_key field for float value if exist + """ + sort_key = getattr(data, "sort_key", None) + if sort_key is None: + return + + try: + value = float(data.sort_key) + except Exception: + msg = _("Validation failed: value must be float") + raise Invalid(msg) + + if value < 0 or value > 1000: + msg = _("Validation failed: value must be between 0 and 1000") + raise Invalid(msg) + @implementer(ISubGroup, ISubGroupSchema, IDeactivable) class SubGroup(Container): diff --git a/src/senaite/core/profiles/default/metadata.xml b/src/senaite/core/profiles/default/metadata.xml index 4165d6b115..8db0af2159 100644 --- a/src/senaite/core/profiles/default/metadata.xml +++ b/src/senaite/core/profiles/default/metadata.xml @@ -1,6 +1,6 @@ - 2655 + 2656 profile-Products.ATContentTypes:base profile-Products.CMFEditions:CMFEditions diff --git a/src/senaite/core/upgrade/v02_06_000.py b/src/senaite/core/upgrade/v02_06_000.py index 133854fa15..e02c51200a 100644 --- a/src/senaite/core/upgrade/v02_06_000.py +++ b/src/senaite/core/upgrade/v02_06_000.py @@ -2725,3 +2725,13 @@ def reindex_specs(tool): logger.info("Reindex analysis spec: %r" % obj) obj.reindexObject(idxs=["sampletype_uid", "sampletype_title"]) logger.info("Reindexing analysis specifications [DONE]") + + +def reindex_sub_groups(tool): + logger.info("Reindexing sub group ...") + cat = api.get_tool(SETUP_CATALOG) + for brain in cat(portal_type="SubGroup"): + obj = brain.getObject() + logger.info("Reindex sub group: %r" % obj) + obj.reindexObject(idxs=["sortable_title"], update_metadata=False) + logger.info("Reindexing sub groups [DONE]") diff --git a/src/senaite/core/upgrade/v02_06_000.zcml b/src/senaite/core/upgrade/v02_06_000.zcml index bd99dfef63..9365758f56 100644 --- a/src/senaite/core/upgrade/v02_06_000.zcml +++ b/src/senaite/core/upgrade/v02_06_000.zcml @@ -3,6 +3,14 @@ xmlns:genericsetup="http://namespaces.zope.org/genericsetup" i18n_domain="senaite.core"> + +