Skip to content

Commit f5138b8

Browse files
xisparamonski
authored andcommitted
Analyses sorted by priority in Add Analyses view and preserve AR order when adding into Worksheet (#338)
* Sort Add Analyses View by priority + analyses sort key * Preserve the order of creation of ARs when adding analyses in Worksheets * Remove code complexity
1 parent 5c96019 commit f5138b8

File tree

6 files changed

+81
-14
lines changed

6 files changed

+81
-14
lines changed

bika/lims/browser/worksheet/views/add_analyses.py

+2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ def __init__(self, context, request):
3838
self.context_actions = {}
3939
# initial review state for first form display of the worksheet
4040
# add_analyses search view - first batch of analyses, latest first.
41+
self.sort_on = 'Priority'
4142
self.contentFilter = {'portal_type': 'Analysis',
4243
'review_state':'sample_received',
4344
'worksheetanalysis_review_state':'unassigned',
45+
'sort_on': 'getPrioritySortkey',
4446
'cancellation_state':'active'}
4547
self.base_url = self.context.absolute_url()
4648
self.view_url = self.base_url + "/add_analyses"

bika/lims/browser/worksheet/workflow.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
from AccessControl import getSecurityManager
99
from bika.lims import bikaMessageFactory as _
1010
from bika.lims import PMF
11+
from bika.lims.api import get_tool
1112
from bika.lims.browser.bika_listing import WorkflowAction
1213
from bika.lims.browser.referenceanalysis import AnalysesRetractedListReport
14+
from bika.lims.catalog.analysis_catalog import CATALOG_ANALYSIS_LISTING
1315
from bika.lims.permissions import EditResults, EditWorksheet, ManageWorksheets
1416
from bika.lims.subscribers import doActionFor
1517
from bika.lims.subscribers import skip
@@ -105,16 +107,17 @@ def __call__(self):
105107
self.request.response.redirect(self.context.absolute_url())
106108
return
107109

108-
selected_analyses = WorkflowAction._get_selected_items(self)
109-
selected_analysis_uids = selected_analyses.keys()
110-
if selected_analyses:
111-
for uid in selected_analysis_uids:
112-
analysis = rc.lookupObject(uid)
113-
# Double-check the state first
114-
if (workflow.getInfoFor(analysis, 'worksheetanalysis_review_state') == 'unassigned'
115-
and workflow.getInfoFor(analysis, 'review_state') == 'sample_received'
116-
and workflow.getInfoFor(analysis, 'cancellation_state') == 'active'):
117-
self.context.addAnalysis(analysis)
110+
analysis_uids = form.get("uids", [])
111+
if analysis_uids:
112+
# We retrieve the analyses from the database sorted by AR ID
113+
# ascending, so the positions of the ARs inside the WS are
114+
# consistent with the order of the ARs
115+
catalog = get_tool(CATALOG_ANALYSIS_LISTING)
116+
brains = catalog({'UID': analysis_uids,
117+
'sort_on': 'getRequestID'})
118+
for brain in brains:
119+
analysis = brain.getObject()
120+
self.context.addAnalysis(analysis)
118121

119122
self.destination_url = self.context.absolute_url()
120123
self.request.response.redirect(self.destination_url)

bika/lims/content/abstractroutineanalysis.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from bika.lims.browser.fields import HistoryAwareReferenceField, \
1717
InterimFieldsField, UIDReferenceField
1818
from bika.lims.browser.widgets import DecimalWidget, RecordsWidget
19+
from bika.lims.catalog.indexers.baseanalysis import sortable_title
1920
from bika.lims.content.abstractanalysis import AbstractAnalysis
2021
from bika.lims.content.abstractanalysis import schema
2122
from bika.lims.content.reflexrule import doReflexRuleAction
@@ -477,13 +478,20 @@ def getDependencies(self):
477478
@security.public
478479
def getPrioritySortkey(self):
479480
"""
480-
Returns the key that will be used to sort the current Analysis
481-
Delegates to getPrioritySortKey function from the AnalysisRequest
481+
Returns the key that will be used to sort the current Analysis, from
482+
most prioritary to less prioritary.
482483
:return: string used for sorting
483484
"""
484485
analysis_request = self.getRequest()
485-
if analysis_request:
486-
return analysis_request.getPrioritySortkey()
486+
if analysis_request is None:
487+
return None
488+
ar_sort_key = analysis_request.getPrioritySortkey()
489+
ar_id = analysis_request.getId().lower()
490+
title = sortable_title(self)
491+
if callable(title):
492+
title = title()
493+
return '{}.{}.{}'.format(ar_sort_key, ar_id, title)
494+
487495

488496
@security.public
489497
def getHidden(self):

bika/lims/upgrade/configure.zcml

+7
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@
2424
handler="bika.lims.upgrade.v01_01_000.upgrade"
2525
profile="bika.lims:default"/>
2626

27+
<genericsetup:upgradeStep
28+
title="Upgrade to Bika LIMS Evo 1.1.1"
29+
source="1.1.0"
30+
destination="1.1.1"
31+
handler="bika.lims.upgrade.v01_01_001.upgrade"
32+
profile="bika.lims:default"/>
33+
2734
</configure>

bika/lims/upgrade/utils.py

+10
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,16 @@ def addColumn(self, catalog, column):
286286
self.refreshcatalog.append(cat.id)
287287
transaction.commit()
288288

289+
def reindexIndex(self, catalog, index):
290+
cat = self._getCatalog(catalog)
291+
if index not in cat.indexes():
292+
logger.warn("Index {} not found in {}".format(index, catalog))
293+
return
294+
indexes = self.reindexcatalog.get(cat.id, [])
295+
if index not in indexes:
296+
indexes.append(index)
297+
self.reindexcatalog[cat.id] = indexes
298+
289299
def refreshCatalogs(self):
290300
"""
291301
It reindexes the modified catalogs but, while cleanAndRebuildCatalogs

bika/lims/upgrade/v01_01_001.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from Acquisition import aq_inner
2+
from Acquisition import aq_parent
3+
from bika.lims import logger
4+
from bika.lims.catalog import CATALOG_ANALYSIS_LISTING
5+
from bika.lims.config import PROJECTNAME as product
6+
from bika.lims.upgrade import upgradestep
7+
from bika.lims.upgrade.utils import UpgradeUtils
8+
9+
version = '1.1.1'
10+
profile = 'profile-{0}:default'.format(product)
11+
12+
13+
@upgradestep(product, version)
14+
def upgrade(tool):
15+
portal = aq_parent(aq_inner(tool))
16+
setup = portal.portal_setup
17+
ut = UpgradeUtils(portal)
18+
ver_from = ut.getInstalledVersion(product)
19+
20+
if ut.isOlderVersion(product, version):
21+
logger.info("Skipping upgrade of {0}: {1} > {2}".format(
22+
product, ver_from, version))
23+
# The currently installed version is more recent than the target
24+
# version of this upgradestep
25+
return True
26+
27+
logger.info("Upgrading {0}: {1} -> {2}".format(product, ver_from, version))
28+
29+
# Reindex getPrioritySortkey index. The priority sort key from analyses
30+
# takes also into account analyses have to be sorted by their sortkey, so
31+
# two analyses with same priority, same AR, but different sort key values
32+
# don't get mixed.
33+
ut.reindexIndex(CATALOG_ANALYSIS_LISTING, 'getPrioritySortkey')
34+
ut.refreshCatalogs()
35+
36+
logger.info("{0} upgraded to version {1}".format(product, version))
37+
return True

0 commit comments

Comments
 (0)