Skip to content

Commit

Permalink
Subgroups sort by sort_key
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunga001 committed Dec 2, 2024
1 parent dd5aab4 commit cf91759
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/senaite/core/catalog/indexer/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<!-- AnalysisCategory Indexer -->
<adapter name="sortable_title" factory=".analysiscategory.sortable_title"/>

<!-- SubGroup Indexer -->
<adapter name="sortable_title" factory=".subgroup.sortable_title"/>

<!-- ARReport Indexer -->
<adapter name="sample_uid" factory=".arreport.sample_uid"/>
<adapter name="arreport_searchable_text" factory=".arreport.arreport_searchable_text"/>
Expand Down
32 changes: 32 additions & 0 deletions src/senaite/core/catalog/indexer/subgroup.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 26 additions & 2 deletions src/senaite/core/content/subgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/senaite/core/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<metadata>
<version>2655</version>
<version>2656</version>
<dependencies>
<dependency>profile-Products.ATContentTypes:base</dependency>
<dependency>profile-Products.CMFEditions:CMFEditions</dependency>
Expand Down
10 changes: 10 additions & 0 deletions src/senaite/core/upgrade/v02_06_000.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
8 changes: 8 additions & 0 deletions src/senaite/core/upgrade/v02_06_000.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
i18n_domain="senaite.core">

<genericsetup:upgradeStep
title="SENAITE.CORE 2.6.0: Fix subgroups not sorted by sort key"
description="Reindex sortable_title for all sub groups"
source="2655"
destination="2656"
handler=".v02_06_000.reindex_sub_groups"
profile="senaite.core:default"/>

<genericsetup:upgradeStep
title="SENAITE.CORE 2.6.0: Fix sampletype-related indexes for specs"
description="SampleType-specific indexes Analysis Specs are not indexed"
Expand Down

0 comments on commit cf91759

Please sign in to comment.