Passed
Pull Request — 2.x (#1935)
by Jordi
05:06
created

senaite.core.browser.modals.analysis   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 56
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A SetAnalysisConditionsView.get_analysis_name() 0 3 1
A SetAnalysisConditionsView.get_conditions() 0 8 2
A SetAnalysisConditionsView.get_uid_from_request() 0 10 3
A SetAnalysisConditionsView.redirect() 0 7 2
A SetAnalysisConditionsView.get_analysis() 0 7 2
A SetAnalysisConditionsView.handle_submit() 0 12 2
A SetAnalysisConditionsView.__call__() 0 4 2
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2022 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import copy
22
23
from bika.lims import api
24
from bika.lims import FieldEditAnalysisConditions
25
from bika.lims import senaiteMessageFactory as _
26
from bika.lims.api.security import check_permission
27
from bika.lims.interfaces.analysis import IRequestAnalysis
28
from Products.CMFPlone.utils import safe_unicode
29
from Products.Five.browser import BrowserView
30
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
31
32
33
class SetAnalysisConditionsView(BrowserView):
34
    """View for the update of analysis conditions
35
    """
36
    template = ViewPageTemplateFile("templates/set_analysis_conditions.pt")
37
38
    def __call__(self):
39
        if self.request.form.get("submitted", False):
40
            return self.handle_submit()
41
        return self.template()
42
43
    def redirect(self, message=None, level="info"):
44
        """Redirect with a message
45
        """
46
        redirect_url = api.get_url(self.context)
47
        if message is not None:
48
            self.context.plone_utils.addPortalMessage(message, level)
49
        return self.request.response.redirect(redirect_url)
50
51
    def get_analysis(self):
52
        uid = self.get_uid_from_request()
53
        obj = api.get_object_by_uid(uid)
54
        if IRequestAnalysis.providedBy(obj):
55
            # Only analyses that implement IRequestAnalysis support conditions
56
            return obj
57
        return None
58
59
    def get_uid_from_request(self):
60
        """Returns the uid from the request, if any
61
        """
62
        uid = self.request.form.get("uid", "")
63
        if api.is_uid(uid):
64
            return uid
65
        uid = self.request.get("uid", "")
66
        if api.is_uid(uid):
67
            return uid
68
        return None
69
70
    def get_analysis_name(self):
71
        analysis = self.get_analysis()
72
        return api.get_title(analysis)
73
74
    def get_conditions(self):
75
        conditions = self.get_analysis().getConditions()
76
        conditions = copy.deepcopy(conditions)
77
        for condition in conditions:
78
            choices = condition.get("choices", "")
79
            options = filter(None, choices.split("|"))
80
            condition.update({"options": options})
81
        return conditions
82
83
    def handle_submit(self):
84
        analysis = self.get_analysis()
85
        title = safe_unicode(api.get_title(analysis))
86
        if not check_permission(FieldEditAnalysisConditions, analysis):
87
            message = _("Not allowed to update conditions: {}").format(title)
88
            return self.redirect(message=message, level="error")
89
90
        # Update the conditions
91
        conditions = self.request.form.get("conditions", [])
92
        analysis.setConditions(conditions)
93
        message = _("Analysis conditions updated: {}").format(title)
94
        return self.redirect(message=message)
95