Passed
Push — master ( d79aa2...26548a )
by Jordi
11:12
created

AnalysisReportInfoView.get_sendlog()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
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-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
from bika.lims.browser import BrowserView
23
from bika.lims.utils import get_image
24
from plone.memoize import view
25
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
26
from ZODB.POSException import POSKeyError
27
28
29
class AnalysisReportInfoView(BrowserView):
30
    """Show details of the Analysis Report
31
    """
32
    template = ViewPageTemplateFile("templates/analysisreport_info.pt")
33
34
    def __init__(self, context, request):
35
        super(AnalysisReportInfoView, self).__init__(context, request)
36
37
    def __call__(self):
38
        # disable the editable border
39
        self.request.set("disable_border", 1)
40
        return self.template()
41
42
    @view.memoize
43
    def get_report(self):
44
        report_uid = self.request.form.get("report_uid")
45
        return api.get_object_by_uid(report_uid, None)
46
47
    @view.memoize
48
    def get_primary_sample(self):
49
        report = self.get_report()
50
        return report.getAnalysisRequest()
51
52
    @view.memoize
53
    def get_metadata(self):
54
        report = self.get_report()
55
        return report.getMetadata()
56
57
    @view.memoize
58
    def get_sendlog(self):
59
        report = self.get_report()
60
        records = report.getSendLog()
61
        return list(reversed(records))
62
63
    @view.memoize
64
    def get_contained_samples(self):
65
        metadata = self.get_metadata()
66
        samples = metadata.get("contained_requests", [])
67
        sample_uids = filter(api.is_uid, samples)
68
        return map(api.get_object_by_uid, sample_uids)
69
70
    def get_icon_for(self, typename):
71
        """Returns the big image icon by its (type-)name
72
        """
73
        image = "{}_big.png".format(typename)
74
        return get_image(
75
            image, width="20px", style="vertical-align: baseline;")
76
77
    def get_filesize(self, f):
78
        """Return the filesize of the PDF as a float
79
        """
80
        try:
81
            filesize = float(f.get_size())
82
            return float("%.2f" % (filesize / 1024))
83
        except (POSKeyError, TypeError, AttributeError):
84
            return 0.0
85
86
    @view.memoize
87
    def get_attachment_data_by_uid(self, uid):
88
        """Retrieve attachment data by UID
89
        """
90
        attachment = api.get_object_by_uid(uid, default=None)
91
        # Attachment file not found/deleted
92
        if attachment is None:
93
            return {}
94
        f = attachment.getAttachmentFile()
95
        attachment_type = attachment.getAttachmentType()
96
        attachment_keys = attachment.getAttachmentKeys()
97
        filename = f.filename
98
        filesize = self.get_filesize(f)
99
        mimetype = f.getContentType()
100
        report_option = attachment.getReportOption()
101
102
        return {
103
            "obj": attachment,
104
            "attachment_type": attachment_type,
105
            "attachment_keys": attachment_keys,
106
            "file": f,
107
            "uid": uid,
108
            "filesize": filesize,
109
            "filename": filename,
110
            "mimetype": mimetype,
111
            "report_option": report_option,
112
        }
113