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

Apply category sort order on analysis specifications #2191

Open
wants to merge 7 commits into
base: 2.x
Choose a base branch
from
Open
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 @@ -4,6 +4,7 @@ Changelog
2.6.0 (unreleased)
------------------

- #2191 Apply category sort order on analysis specifications
- #2434 Add string support for interim fields
- #2417 Use dtime.to_DT instead of api.to_date
- #2411 Multiselection with duplicates support for interim fields
Expand Down
28 changes: 24 additions & 4 deletions src/bika/lims/browser/widgets/analysisspecificationwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from plone.memoize import view
from Products.Archetypes.Registry import registerWidget
from Products.Archetypes.Widget import TypesWidget
from Products.CMFCore.utils import getToolByName



class AnalysisSpecificationView(BikaListingView):
Expand All @@ -43,7 +45,6 @@ class AnalysisSpecificationView(BikaListingView):

def __init__(self, context, request):
super(AnalysisSpecificationView, self).__init__(context, request)

self.catalog = "senaite_catalog_setup"
self.contentFilter = {
"portal_type": "AnalysisService",
Expand Down Expand Up @@ -188,8 +189,25 @@ def get_dynamic_analysisspecs(self):
return self.dynamic_spec.get_by_keyword()

def folderitems(self):
"""Sort by Categories
"""

bsc = getToolByName(self.context, "senaite_catalog_setup")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use simply catalog as variable, because this catalog is no longer named bika_setup_catalog. You might also consider to use api.get_tool instead of getToolByName

self.an_cats = bsc(
portal_type="AnalysisCategory",
sort_on="sortable_title")
self.an_cats_order = dict([
(b.Title, "{:04}".format(a))
for a, b in enumerate(self.an_cats)])

items = super(AnalysisSpecificationView, self).folderitems()
self.categories.sort()


if self.show_categories_enabled():
self.categories = map(lambda x: x[0],
sorted(self.categories, key=lambda x: x[1]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, above you have already all categories in the right order. It would make more sense to simply filter out those that are not found instead of sorting the found ones afterwards.
Use therefore the built in filter function of Python, e.g. like this:

from bika.lims import api
from senaite.core.catalog import SETUP_CATALOG

catalog = api.get_tool(SETUP_CATALOG)
all_categories = catalog({"portal_type": "AnalysisCategory", "sort_on: "sortable_title"})
sorted_categories = filter(lambda cat: api.get_title(cat) in self.found_categories, all_categories)
self.categories = map(api.get_object, sorted_categories)

Note: This code is not tested;)

else:
self.categories.sort()
return items

def folderitem(self, obj, item, index):
Expand All @@ -207,6 +225,8 @@ def folderitem(self, obj, item, index):
url = api.get_url(obj)
title = api.get_title(obj)
keyword = obj.getKeyword()
cat = obj.getCategoryTitle()
cat_order = self.an_cats_order.get(cat)

# dynamic analysisspecs
dspecs = self.get_dynamic_analysisspecs()
Expand All @@ -221,8 +241,8 @@ def folderitem(self, obj, item, index):
# get the category
if self.show_categories_enabled():
category = obj.getCategoryTitle()
if category not in self.categories:
self.categories.append(category)
if (category,cat_order) not in self.categories:
self.categories.append((category,cat_order))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please simply collect here the category title in a instance variable, e.g.:

if category not in self.found_categories:
    self.found_categories.append(category)

This one is used later to filter out the already sorted categories.

item["category"] = category

item["Title"] = title
Expand Down