Passed
Push — master ( e3f582...5930d4 )
by Ramon
03:43
created

StorageLocationsView.folderitem()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
cc 2
nop 4
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-2020 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import json
22
23
import plone
24
from Products.ATContentTypes.content import schemata
25
from Products.Archetypes import PloneMessageFactory as _p
26
from Products.Archetypes import atapi
27
from Products.CMFCore.utils import getToolByName
28
from Products.CMFPlone.utils import safe_unicode
29
from bika.lims import api
30
from bika.lims import bikaMessageFactory as _
31
from bika.lims.browser import BrowserView
32
from bika.lims.browser.bika_listing import BikaListingView
33
from bika.lims.config import PROJECTNAME
34
from bika.lims.interfaces import IClient
35
from bika.lims.interfaces import IStorageLocations
36
from bika.lims.permissions import AddStorageLocation
37
from bika.lims.utils import get_link
38
from plone.app.folder.folder import ATFolder
39
from plone.app.folder.folder import ATFolderSchema
40
from zope.interface.declarations import implements
41
42
from bika.lims.utils import get_link
43
44
45
class StorageLocationsView(BikaListingView):
46
47
    def __init__(self, context, request):
48
        super(StorageLocationsView, self).__init__(context, request)
49
        self.catalog = 'bika_setup_catalog'
50
        self.contentFilter = {'portal_type': 'StorageLocation',
51
                              'sort_on': 'sortable_title'}
52
        self.context_actions = {_('Add'):
53
                            {'url': 'createObject?type_name=StorageLocation',
54
                             'permission': AddStorageLocation,
55
                             'icon': '++resource++bika.lims.images/add.png'}}
56
        self.title = self.context.translate(_("Storage Locations"))
57
        self.icon = self.portal_url + "/++resource++bika.lims.images/storagelocation_big.png"
58
        self.description = ""
59
60
        self.show_select_row = False
61
        self.show_select_column = True
62
        self.pagesize = 25
63
64
        self.columns = {
65
            'Title': {'title': _('Storage Location'),
66
                      'index':'sortable_title'},
67
            'Description': {'title': _('Description'),
68
                            'index': 'description',
69
                            'toggle': True},
70
            'SiteTitle': {'title': _p('Site Title'),
71
                      'toggle': True},
72
            'SiteCode': {'title': _p('Site Code'),
73
                      'toggle': True},
74
            'LocationTitle': {'title': _p('Location Title'),
75
                      'toggle': True},
76
            'LocationCode': {'title': _p('Location Code'),
77
                      'toggle': True},
78
            'ShelfTitle': {'title': _p('Shelf Title'),
79
                      'toggle': True},
80
            'ShelfCode': {'title': _p('Shelf Code'),
81
                      'toggle': True},
82
            'Owner': {'title': _p('Owner'),
83
                      'toggle': True},
84
        }
85
86
        self.review_states = [
87
            {'id':'default',
88
             'title': _('Active'),
89
             'contentFilter': {'is_active': True},
90
             'transitions': [{'id':'deactivate'}, ],
91
             'columns': ['Title', 'Description', 'Owner',  'SiteTitle', 'SiteCode', 'LocationTitle', 'LocationCode', 'ShelfTitle', 'ShelfCode']},
92
            {'id':'inactive',
93
             'title': _('Inactive'),
94
             'contentFilter': {'is_active': False},
95
             'transitions': [{'id':'activate'}, ],
96
             'columns': ['Title', 'Description', 'Owner', 'SiteTitle', 'SiteCodeShelfCode' ]},
97
            {'id':'all',
98
             'title': _('All'),
99
             'contentFilter':{},
100
             'columns': ['Title', 'Description', 'Owner', 'SiteTitle', 'SiteCodeShelfCode' ]},
101
        ]
102
103
    def before_render(self):
104
        """Before template render hook
105
        """
106
        # Don't allow any context actions
107
        self.request.set("disable_border", 1)
108
109
    def folderitem(self, obj, item, index):
110
        obj = api.get_object(obj)
111
        item["Description"] = obj.Description()
112
        item["replace"]["Title"] = get_link(item["url"], item["Title"])
113
114
        parent = api.get_parent(obj)
115
        if IClient.providedBy(parent):
116
            item["Owner"] = api.get_title(parent)
117
        else:
118
            item["Owner"] = self.context.bika_setup.laboratory.Title()
119
        return item
120
121
122
schema = ATFolderSchema.copy()
123
124
125
class StorageLocations(ATFolder):
126
    implements(IStorageLocations)
127
    displayContentsTab = False
128
    schema = schema
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable schema does not seem to be defined.
Loading history...
129
130
schemata.finalizeATCTSchema(schema, folderish = True, moveDiscussion = False)
131
atapi.registerType(StorageLocations, PROJECTNAME)
132
133
134
class ajax_StorageLocations(BrowserView):
135
    """ The autocomplete data source for storage location selection widgets.
136
        Returns a JSON list of storage location titles.
137
138
        Request parameters:
139
140
        - term: the string which will be searched against all Storage Location
141
          titles.
142
143
        - _authenticator: The plone.protect authenticator.
144
145
    """
146
147 View Code Duplication
    def filter_list(self, items, searchterm):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
148
        if searchterm and len(searchterm) < 3:
149
            # Items that start with A or AA
150
            res = [s.getObject()
151
                     for s in items
152
                     if s.title.lower().startswith(searchterm)]
153
            if not res:
154
                # or, items that contain A or AA
155
                res = [s.getObject()
156
                         for s in items
157
                         if s.title.lower().find(searchterm) > -1]
158
        else:
159
            # or, items that contain searchterm.
160
            res = [s.getObject()
161
                     for s in items
162
                     if s.title.lower().find(searchterm) > -1]
163
        return res
164
165
    def __call__(self):
166
        plone.protect.CheckAuthenticator(self.request)
167
        bsc = getToolByName(self.context, 'bika_setup_catalog')
168
        term = safe_unicode(self.request.get('term', '')).lower()
169
        if not term:
170
            return json.dumps([])
171
172
        client_items = lab_items = []
173
174
        # User (client) storage locations
175
        if self.context.portal_type == 'Client':
176
            client_path = self.context.getPhysicalPath()
177
            client_items = list(
178
                bsc(portal_type = "StorageLocation",
179
                    path = {"query": "/".join(client_path), "level" : 0 },
180
                    is_active = True,
181
                    sort_on='sortable_title'))
182
183
        # Global (lab) storage locations
184
        lab_path = \
185
                self.context.bika_setup.bika_storagelocations.getPhysicalPath()
186
        lab_items = list(
187
            bsc(portal_type = "StorageLocation",
188
                path = {"query": "/".join(lab_path), "level" : 0 },
189
                is_active = True,
190
                sort_on='sortable_title'))
191
192
        client_items = [callable(s.Title) and s.Title() or s.title
193
                 for s in self.filter_list(client_items, term)]
194
        lab_items = [callable(s.Title) and s.Title() or s.title
195
                 for s in self.filter_list(lab_items, term)]
196
        lab_items = ["%s: %s" % (_("Lab"), safe_unicode(i))
197
                     for i in lab_items]
198
199
        items = client_items + lab_items
200
201
        return json.dumps(items)
202