Passed
Push — 2.x ( 65b15a...441526 )
by Jordi
06:22
created

LabAnalysesViewlet.available()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 1
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-2021 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
from bika.lims import senaiteMessageFactory as _
23
from plone.app.layout.viewlets import ViewletBase
24
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
25
from senaite.core.registry import get_registry_record
26
27
28
class LabAnalysesViewlet(ViewletBase):
29
    """Laboratory analyses section viewlet for Sample view
30
    """
31
    index = ViewPageTemplateFile("templates/sampleanalyses.pt")
32
33
    title = _("Analyses")
34
    icon_name = "analysisservice"
35
    capture = "lab"
36
37
    @property
38
    def sample(self):
39
        return self.context
40
41
    def is_collapsed(self):
42
        name = "sampleview_collapse_{}_analysis_table".format(
43
            self.capture)
44
        return get_registry_record(name, default=False)
45
46
    def available(self):
47
        """Returns true if this sample contains at least one analysis for the
48
        point of capture (capture)
49
        """
50
        analyses = self.sample.getAnalyses(getPointOfCapture=self.capture)
51
        return len(analyses) > 0
52
53
    def get_listing_view(self):
54
        request = api.get_request()
55
        view_name = "table_{}_analyses".format(self.capture)
56
        view = api.get_view(view_name, context=self.sample, request=request)
57
        return view
58
59
    def contents_table(self):
60
        view = self.get_listing_view()
61
        view.update()
62
        view.before_render()
63
        return view.ajax_contents_table()
64
65
66
class FieldAnalysesViewlet(LabAnalysesViewlet):
67
    """Field analyses section viewlet for Sample view
68
    """
69
    title = _("Field Analyses")
70
    capture = "field"
71
72
73
class QCAnalysesViewlet(LabAnalysesViewlet):
74
    """QC analyses section viewlet for Sample view
75
    """
76
    title = _("QC Analyses")
77
    capture = "qc"
78
79
    def available(self):
80
        """Returns true if this sample contains at least one qc analysis
81
        """
82
        analyses = self.sample.getQCAnalyses()
83
        return len(analyses) > 0
84