Passed
Push — 2.x ( 42cd40...702531 )
by Ramon
07:36
created

ResultsInterpretationViewlet.update()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nop 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 get_panels(self):
33
        """Returns the objects for which an specific free text area has to be
34
        rendered for the introduction of results interpretations
35
        """
36
        return self.context.getDepartments()
37
38
    def handle_form_submit(self):
39
        """Handle form submission
40
        """
41
        protect.CheckAuthenticator(self.request)
42
        logger.info("Handle ResultsInterpration Submit")
43
        # Save the results interpretation
44
        res = self.request.form.get("ResultsInterpretationDepts", [])
45
        self.context.setResultsInterpretationDepts(res)
46
        self.add_status_message(_("Changes Saved"), level="info")
47
        # reindex the object after save to update all catalog metadata
48
        self.context.reindexObject()
49
        # notify object edited event
50
        event.notify(ObjectEditedEvent(self.context))
51
        return self.request.response.redirect(api.get_url(self.context))
52
53
    def add_status_message(self, message, level="info"):
54
        """Set a portal status message
55
        """
56
        return self.context.plone_utils.addPortalMessage(message, level)
57
58
    def is_edit_allowed(self):
59
        """Check if edit is allowed
60
        """
61
        checkPermission = self.context.portal_membership.checkPermission
62
        return checkPermission(FieldEditResultsInterpretation, self.context)
63
64
    def get_text(self, department, mode="raw"):
65
        """Returns the text saved for the selected department
66
        """
67
        row = self.context.getResultsInterpretationByDepartment(department)
68
        rt = RichTextValue(row.get("richtext", ""), "text/plain", "text/html")
69
        if mode == "output":
70
            return rt.output
71
        return rt.raw
72
73
    def get_interpretation_templates(self):
74
        """Return a list of datainfo dicts representing interpretation templates
75
        """
76
        query = {"portal_type": "InterpretationTemplate",
77
                 "review_state": "active"}
78
79
        def get_data_info(item):
80
            return {
81
                "uid": api.get_uid(item),
82
                "title": api.get_title(item)
83
            }
84
85
        brains = api.search(query, SETUP_CATALOG)
86
        return map(get_data_info, brains)
87