Passed
Push — 2.x ( 44675d...5e1af5 )
by Jordi
09:20
created

senaite.core.browser.controlpanel.interpretationtemplates.view   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 68
dl 0
loc 128
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A InterpretationTemplatesView.update() 0 4 1
A InterpretationTemplatesView.before_render() 0 4 1
B InterpretationTemplatesView.__init__() 0 52 1
A InterpretationTemplatesView.folderitem() 0 28 3
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-2024 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import collections
22
23
from bika.lims import _
24
from bika.lims import api
25
from bika.lims.catalog import SETUP_CATALOG
26
from bika.lims.utils import get_link_for
27
from plone.app.textfield import RichTextValue
28
from senaite.app.listing import ListingView
29
30
31
class InterpretationTemplatesView(ListingView):
32
    """Results Interpretation Templates listing view
33
    """
34
35
    def __init__(self, context, request):
36
        super(InterpretationTemplatesView, self).__init__(context, request)
37
38
        self.catalog = SETUP_CATALOG
39
40
        self.contentFilter = {
41
            "portal_type": "InterpretationTemplate",
42
            "sort_on": "created",
43
            "sort_order": "descending",
44
        }
45
46
        self.context_actions = {
47
            _("Add"): {
48
                "url": "++add++InterpretationTemplate",
49
                "icon": "add.png"}
50
            }
51
52
        self.show_select_column = True
53
54
        self.columns = collections.OrderedDict((
55
            ("Title", {
56
                "title": _("Title"),
57
                "index": "sortable_title"}),
58
            ("Description", {
59
                "title": _("Description"),
60
                "index": "Description"}),
61
            ("SampleTypes", {
62
                "title": _("Sample Types"),
63
                "sortable": False}),
64
            ("Text", {
65
                "title": _("Text"),
66
                "sortable": False}),
67
        ))
68
69
        self.review_states = [
70
            {
71
                "id": "default",
72
                "title": _("Active"),
73
                "contentFilter": {"is_active": True},
74
                "transitions": [],
75
                "columns": self.columns.keys(),
76
            }, {
77
                "id": "inactive",
78
                "title": _("Inactive"),
79
                "contentFilter": {'is_active': False},
80
                "transitions": [],
81
                "columns": self.columns.keys(),
82
            }, {
83
                "id": "all",
84
                "title": _("All"),
85
                "contentFilter": {},
86
                "columns": self.columns.keys(),
87
            },
88
        ]
89
90
    def update(self):
91
        """Update hook
92
        """
93
        super(InterpretationTemplatesView, self).update()
94
95
    def before_render(self):
96
        """Before template render hook
97
        """
98
        super(InterpretationTemplatesView, self).before_render()
99
100
    def folderitem(self, obj, item, index):
101
        """Service triggered each time an item is iterated in folderitems.
102
        The use of this service prevents the extra-loops in child objects.
103
        :obj: the instance of the class to be foldered
104
        :item: dict containing the properties of the object to be used by
105
            the template
106
        :index: current index of the item
107
        """
108
        obj = api.get_object(obj)
109
110
        item["replace"]["Title"] = get_link_for(obj)
111
112
        # List all linked sampletypes
113
        sampletypes = obj.getSampleTypes()
114
        if sampletypes:
115
            titles = map(api.get_title, sampletypes)
116
            item["SampleTypes"] = ", ".join(titles)
117
            item["replace"]["SampleTypes"] = ", ".join(
118
                map(get_link_for, sampletypes))
119
120
        text = obj.text
121
        if isinstance(text, RichTextValue):
122
            # convert output to plain text
123
            text._outputMimeType = "text/plain"
124
            text = text.output
125
        item["Text"] = text
126
127
        return item
128