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

Cache allowed transitions for analyses on the request #1417

Merged
merged 2 commits into from
Aug 9, 2019
Merged
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 @@ -11,6 +11,7 @@ Changelog

**Changed**

- #1417 Cache allowed transitions for analyses on the request
- #1413 Improved Email Publication


Expand Down
42 changes: 39 additions & 3 deletions bika/lims/workflow/analysis/guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
"""
Expand Down