PatientMultifileView.get_file()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.HEALTH.
4
#
5
# SENAITE.HEALTH is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by the Free
7
# Software 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 ZODB.POSException import POSKeyError
22
from bika.lims import api
23
from bika.lims import bikaMessageFactory as _
24
from bika.lims.browser.bika_listing import BikaListingView
25
from bika.lims.utils import get_link
26
from plone.app.content.browser.interfaces import IFolderContentsView
27
from plone.app.layout.globals.interfaces import IViewView
28
from zope.interface import implements
29
30
31
class PatientMultifileView(BikaListingView):
32
    implements(IFolderContentsView, IViewView)
33
34
    def __init__(self, context, request):
35
        super(PatientMultifileView, self).__init__(context, request)
36
37
        self.catalog = "bika_setup_catalog"
38
        self.contentFilter = {
39
            "portal_type": "Multifile",
40
            "path": {
41
                "query": api.get_path(context),
42
                "depth": 1  # searching just inside the specified folder
43
            },
44
            "sort_on": "created",
45
            "sort_order": "descending",
46
        }
47
48
        self.form_id = "patientfiles"
49
        self.title = self.context.translate(_("Patient Files"))
50
        self.icon = "{}/{}".format(
51
            self.portal_url,
52
            "++resource++bika.lims.images/instrumentcertification_big.png"
53
        )
54
        self.context_actions = {
55
            _("Add"): {
56
                "url": "createObject?type_name=Multifile",
57
                "icon": "++resource++bika.lims.images/add.png"
58
            }
59
        }
60
61
        self.allow_edit = False
62
        self.show_select_column = False
63
        self.show_workflow_action_buttons = True
64
        self.pagesize = 30
65
66
        self.columns = {
67
            "DocumentID": {"title": _("Document ID"),
68
                           "index": "sortable_title"},
69
            "DocumentVersion": {"title": _("Document Version"),
70
                                "index": "sortable_title"},
71
            "DocumentLocation": {"title": _("Document Location"),
72
                                 "index": "sortable_title"},
73
            "DocumentType": {"title": _("Document Type"),
74
                             "index": "sortable_title"},
75
            "FileDownload": {"title": _("File")}
76
        }
77
78
        self.review_states = [
79
            {
80
                "id": "default",
81
                "title": _("All"),
82
                "contentFilter": {},
83
                "columns": [
84
                    "DocumentID",
85
                    "DocumentVersion",
86
                    "DocumentLocation",
87
                    "DocumentType",
88
                    "FileDownload"
89
                ]
90
            },
91
        ]
92
93
    def get_file(self, obj):
94
        """Return the file of the given object
95
        """
96
        try:
97
            return obj.getFile()
98
        except POSKeyError:  # POSKeyError: "No blob file"
99
            # XXX When does this happen?
100
            return None
101
102
    def folderitem(self, obj, item, index):
103
        """Augment folder listing item with additional data
104
        """
105
        url = item.get("url")
106
        title = item.get("DocumentID")
107
108
        item["replace"]["DocumentID"] = get_link(url, title)
109
110
        item["FileDownload"] = ""
111
        item["replace"]["FileDownload"] = ""
112
        file = self.get_file(obj)
113
        if file and file.get_size() > 0:
114
            filename = file.filename
115
            download_url = "{}/at_download/File".format(url)
116
            anchor = get_link(download_url, filename)
117
            item["FileDownload"] = filename
118
            item["replace"]["FileDownload"] = anchor
119
120
        item["DocumentVersion"] = obj.getDocumentVersion()
121
        item["DocumentLocation"] = obj.getDocumentLocation()
122
        item["DocumentType"] = obj.getDocumentType()
123
        return item
124