Passed
Push — master ( 70be41...e1228c )
by Ramon
11:12
created

AccreditationView.search()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nop 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-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from Products.CMFPlone.utils import safe_unicode
22
from plone.app.content.browser.interfaces import IFolderContentsView
23
from zope.interface import implements
24
25
from bika.lims import api
26
from bika.lims import bikaMessageFactory as _
27
from bika.lims.controlpanel.bika_analysisservices import AnalysisServicesView
28
from bika.lims.utils import t
29
30
31
class AccreditationView(AnalysisServicesView):
32
    implements(IFolderContentsView)
33
34
    def __init__(self, context, request):
35
        super(AccreditationView, self).__init__(context, request)
36
        self.contentFilter = {
37
            'portal_type': 'AnalysisService',
38
            'sort_on': 'sortable_title',
39
            'is_active': True}
40
        self.context_actions = {}
41
        self.title = self.context.translate(_("Accreditation"))
42
        self.icon = self.portal_url + \
43
                    "/++resource++bika.lims.images/accredited_big.png"
44
45
        lab = context.bika_setup.laboratory
46
        accredited = lab.getLaboratoryAccredited()
47
        self.mapping = {
48
            'lab_is_accredited':
49
                accredited,
50
            'lab_name':
51
                safe_unicode(lab.getName()),
52
            'lab_country':
53
                safe_unicode(lab.getPhysicalAddress().get('country', '')),
54
            'confidence':
55
                safe_unicode(lab.getConfidence()),
56
            'accreditation_body_abbr':
57
                safe_unicode(lab.getAccreditationBody()),
58
            'accreditation_body_name':
59
                safe_unicode(lab.getAccreditationBodyURL()),
60
            'accreditation_standard':
61
                safe_unicode(lab.getAccreditation()),
62
            'accreditation_reference':
63
                safe_unicode(lab.getAccreditationReference())
64
        }
65
        if accredited:
66
            self.description = t(_(
67
                safe_unicode(lab.getAccreditationPageHeader()),
68
                mapping=self.mapping
69
            ))
70
        else:
71
            self.description = t(_(
72
                "The lab is not accredited, or accreditation has not been "
73
                "configured. "
74
            ))
75
        msg = t(_("All Accredited analysis services are listed here."))
76
        self.description = "%s<p><br/>%s</p>" % (self.description, msg)
77
78
        self.show_select_column = False
79
        request.set('disable_border', 1)
80
81
        self.review_states = [
82
            {'id': 'default',
83
             'title': _('All'),
84
             'contentFilter': {},
85
             'transitions': [{'id': 'empty'}, ],  # none
86
             'columns': ['Title',
87
                         'Keyword',
88
                         'Price',
89
                         'MaxTimeAllowed',
90
                         'DuplicateVariation',
91
                         ],
92
             },
93
        ]
94
95
        if not self.context.bika_setup.getShowPrices():
96
            self.review_states[0]['columns'].remove('Price')
97
98
    def selected_cats(self, items):
99
        """return a list of all categories with accredited services
100
        """
101
        cats = []
102
        for item in items:
103
            if 'category' in item and item['category'] not in cats:
104
                cats.append(item['category'])
105
        return cats
106
107
    def search(self, searchterm="", ignorecase=True):
108
        # Boil out those that are not accredited
109
        # There is no need to keep a `getAccredited` index in the catalog only
110
        # for this view. Since we don't expect a lot of items from this content
111
        # type (AnalysisService), is fine to wake-up them
112
        brains = super(AccreditationView, self).search(searchterm, ignorecase)
113
114
        def is_accredited(brain):
115
            obj = api.get_object(brain)
116
            return obj.getAccredited()
117
118
        return filter(is_accredited, brains)
119
120