Passed
Pull Request — 2.x (#1938)
by Jordi
05:41
created

senaite.core.browser.viewlets.resultsinterpretation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 51
dl 0
loc 81
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A ResultsInterpretationViewlet.update() 0 4 2
A ResultsInterpretationViewlet.add_status_message() 0 4 1
A ResultsInterpretationViewlet.get_text() 0 8 2
A ResultsInterpretationViewlet.get_interpretation_templates() 0 14 1
A ResultsInterpretationViewlet.available() 0 2 1
A ResultsInterpretationViewlet.handle_form_submit() 0 14 1
A ResultsInterpretationViewlet.is_edit_allowed() 0 5 1
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
from bika.lims import logger
5
from bika.lims import senaiteMessageFactory as _
6
from bika.lims.catalog import SETUP_CATALOG
7
from bika.lims.permissions import FieldEditResultsInterpretation
8
from plone import protect
9
from plone.app.layout.viewlets import ViewletBase
10
from plone.app.textfield import RichTextValue
11
from Products.Archetypes.event import ObjectEditedEvent
12
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
13
from zope import event
14
15
16
class ResultsInterpretationViewlet(ViewletBase):
17
    """Viewlet for results interpretation field
18
    """
19
    index = ViewPageTemplateFile("templates/resultsinterpretation.pt")
20
21
    title = _("Results interpretation")
22
    icon_name = "resultsinterpretation"
23
24
    def available(self):
25
        return True
26
27
    def update(self):
28
        if self.request.form.get("submitted", False):
29
            return self.handle_form_submit()
30
        return self.index()
31
32
    def handle_form_submit(self):
33
        """Handle form submission
34
        """
35
        protect.CheckAuthenticator(self.request)
36
        logger.info("Handle ResultsInterpration Submit")
37
        # Save the results interpretation
38
        res = self.request.form.get("ResultsInterpretationDepts", [])
39
        self.context.setResultsInterpretationDepts(res)
40
        self.add_status_message(_("Changes Saved"), level="info")
41
        # reindex the object after save to update all catalog metadata
42
        self.context.reindexObject()
43
        # notify object edited event
44
        event.notify(ObjectEditedEvent(self.context))
45
        return self.request.response.redirect(api.get_url(self.context))
46
47
    def add_status_message(self, message, level="info"):
48
        """Set a portal status message
49
        """
50
        return self.context.plone_utils.addPortalMessage(message, level)
51
52
    def is_edit_allowed(self):
53
        """Check if edit is allowed
54
        """
55
        checkPermission = self.context.portal_membership.checkPermission
56
        return checkPermission(FieldEditResultsInterpretation, self.context)
57
58
    def get_text(self, department, mode="raw"):
59
        """Returns the text saved for the selected department
60
        """
61
        row = self.context.getResultsInterpretationByDepartment(department)
62
        rt = RichTextValue(row.get("richtext", ""), "text/plain", "text/html")
63
        if mode == "output":
64
            return rt.output
65
        return rt.raw
66
67
    def get_interpretation_templates(self):
68
        """Return a list of datainfo dicts representing interpretation templates
69
        """
70
        query = {"portal_type": "InterpretationTemplate",
71
                 "review_state": "active"}
72
73
        def get_data_info(item):
74
            return {
75
                "uid": api.get_uid(item),
76
                "title": api.get_title(item)
77
            }
78
79
        brains = api.search(query, SETUP_CATALOG)
80
        return map(get_data_info, brains)
81