From d8a0d5d047b1f32721b983f09ef07930b7ac0664 Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Thu, 8 Aug 2019 12:57:56 +0200 Subject: [PATCH 1/2] Cache allowed transitions for analyses in request This improves the rendering performance of worksheets containing a huge amount of analyses with dependencies. --- bika/lims/workflow/analysis/guards.py | 42 +++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/bika/lims/workflow/analysis/guards.py b/bika/lims/workflow/analysis/guards.py index 0c70653aa3..72947d6587 100644 --- a/bika/lims/workflow/analysis/guards.py +++ b/bika/lims/workflow/analysis/guards.py @@ -19,9 +19,13 @@ # Some rights reserved, see README and LICENSE. from bika.lims import api -from bika.lims.interfaces import IWorksheet, IVerified, ISubmitted -from bika.lims.interfaces.analysis import IRequestAnalysis +from bika.lims import logger from bika.lims import workflow as wf +from bika.lims.interfaces import ISubmitted +from bika.lims.interfaces import IVerified +from bika.lims.interfaces import IWorksheet +from bika.lims.interfaces.analysis import IRequestAnalysis +from plone.memoize.request import cache def is_worksheet_context(): @@ -311,11 +315,43 @@ def is_transition_allowed(analyses, transition_id): if not isinstance(analyses, list): return is_transition_allowed([analyses], transition_id) for analysis in analyses: - if not wf.isTransitionAllowed(analysis, transition_id): + if not cached_is_transition_allowed(analysis, transition_id): return False return True +def _transition_cache_key(fun, obj, action): + """Cache key generator for the request cache + + This function generates cache keys like this: + >>> from bika.lims import api + >>> from zope.annotation.interfaces import IAnnotations + >>> request = api.get_request() + >>> IAnnotations(request) + # noqa: E501 + {'bika.lims.workflow.analysis.guards.check_analysis_allows_transition:3ff02762c70f4a56b1b30c1b74d32bf6-retract': True, + 'bika.lims.workflow.analysis.guards.check_analysis_allows_transition:0390c16ddec14a04b87ff8408e2aa229-retract': True, + ... + } + """ + return "%s-%s" % (api.get_uid(obj), action) + + +@cache(get_key=_transition_cache_key, get_request="analysis.REQUEST") +def cached_is_transition_allowed(analysis, transition_id): + """Check if the transition is allowed for the given analysis and cache the + value on the request. + + Note: The request is obtained by the given expression from the `locals()`, + which includes the given arguments. + """ + logger.debug("cached_is_transition_allowed: analyis=%r transition=%s" + % (analysis, transition_id)) + if wf.isTransitionAllowed(analysis, transition_id): + return True + return False + + def is_submitted_or_submittable(analysis): """Returns whether the analysis is submittable or has already been submitted """ From fba96ec18af14c817745f4d8d8f96973131a2893 Mon Sep 17 00:00:00 2001 From: Ramon Bartl Date: Thu, 8 Aug 2019 13:32:19 +0200 Subject: [PATCH 2/2] Changelog updated --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index f1bef17961..1d2417ba64 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -11,6 +11,7 @@ Changelog **Changed** +- #1417 Cache allowed transitions for analyses on the request - #1413 Improved Email Publication