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

SelectionMacrosView.select_analyst()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
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.CMFCore.utils import getToolByName
22
from zope.i18n import translate
23
from bika.lims.browser import BrowserView
24
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
25
from bika.lims.utils import getUsers
26
from bika.lims import bikaMessageFactory as _
27
from bika.lims import PMF
28
from plone.memoize import ram
29
from time import time
30
31
32
def update_timer():
33
    """
34
    This function sets the time between updates of cached values
35
    """
36
    return time() // (24 *60 * 60)
37
38
39
def _cache_key_select_state(method, self, workflow_id, field_id, field_title):
40
    """
41
    This function returns the key used to decide if select_state has to be recomputed
42
    """
43
    key = update_timer(), workflow_id, field_id, field_title
44
    return key
45
46
47
def _cache_key_select_analysiscategory(method, self, style=None):
48
    """
49
    This function returns the key used to decide if method select_analysiscategory has to be recomputed
50
    """
51
    key = update_timer(), style
52
    return key
53
54
55
def _cache_key_select_analysisservice(method, self, allow_blank,
56
                                      multiselect, style=None):
57
    """
58
    This function returns the key used to decide if method select_analysisservice has to be recomputed
59
    """
60
    key = update_timer(), allow_blank, multiselect, style
61
    return key
62
63
64
def _cache_key_select_analysisspecification(method, self, style=None):
65
    """
66
    This function returns the key used to decide if method select_analysisspecification has to be recomputed
67
    """
68
    key = update_timer(), style
69
    return key
70
71
72
def _cache_key_select_analyst(method, self, allow_blank=False, style=None):
73
    """
74
    This function returns the key used to decide if method select_analyst has to be recomputed
75
    """
76
    key = update_timer(),allow_blank, style
77
    return key
78
79
80
def _cache_key_select_user(method, self, allow_blank=True, style=None):
81
    """
82
    This function returns the key used to decide if method select_user has to be recomputed
83
    """
84
    key = update_timer(), allow_blank, style
85
    return key
86
87
88
def _cache_key_select_client(method, self, style=None):
89
    """
90
    This function returns the key used to decide if method select_client has to be recomputed
91
    """
92
    key = update_timer(), style
93
    return key
94
95
96
def _cache_key_select_contact(method, self, style=None):
97
    """
98
    This function returns the key used to decide if method select_contact has to be recomputed
99
    """
100
    key = update_timer(), style
101
    return key
102
103
104
def _cache_key_select_daterange(method, self, field_id, field_title, style=None):
105
    """
106
    This function returns the key used to decide if method select_daterange has to be recomputed
107
    """
108
    key = update_timer(), field_id, field_title, style
109
    return key
110
111
112
def _cache_key_select_instrument(method, self, style=None):
113
    """
114
    This function returns the key used to decide if method select_instrument has to be recomputed
115
    """
116
    key = update_timer(), style
117
    return key
118
119
120
def _cache_key_select_period(method, self, style=None):
121
    """
122
    This function returns the key used to decide if method select_period has to be recomputed
123
    """
124
    key = update_timer(), style
125
    return key
126
127
128
def _cache_key_select_profile(method, self, style=None):
129
    """
130
    This function returns the key used to decide if method select_profile has to be recomputed
131
    """
132
    key = update_timer(), style
133
    return key
134
135
136
def _cache_key_select_supplier(method, self, style=None):
137
    """
138
    This function returns the key used to decide if method select_supplier has to be recomputed
139
    """
140
    key = update_timer(), style
141
    return key
142
143
def _cache_key_select_reference_sample(method, self, style=None):
144
    """
145
    This function returns the key used to decide if method select_reference_sample has to be recomputed
146
    """
147
    key = update_timer(), style
148
    return key
149
150
151
def _cache_key_select_reference_service(method, self, style=None):
152
    """
153
    This function returns the key used to decide if method select_reference_service has to be recomputed
154
    """
155
    key = update_timer(), style
156
    return key
157
158
159
def _cache_key_select_sample_type(method, self, allow_blank=True, multiselect=False, style=None):
160
    """
161
    This function returns the key used to decide if method select_sample_type has to be recomputed
162
    """
163
    key = update_timer(), allow_blank, multiselect, style
164
    return key
165
166
167
def _cache_key_select_groupingperiod(method, self, allow_blank=True, multiselect=False, style=None):
168
    """
169
    This function returns the key used to decide if method select_groupingperiod has to be recomputed
170
    """
171
    key = update_timer(), allow_blank, multiselect, style
172
    return key
173
174
175
def _cache_key_select_output_format(method, self, style=None):
176
    """
177
    This function returns the key used to decide if method select_output_format has to be recomputed
178
    """
179
    key = update_timer(), style
180
    return key
181
182
183
class SelectionMacrosView(BrowserView):
184
    """ Display snippets for the query form, and
185
        parse their results to contentFilter
186
187
    These methods are called directlly from tal:
188
189
        context/@@selection_macros/analysts
190
191
    To parse form values in reports:
192
193
        python:view.selection_macros.parse_analysisservice(allow_blank=False)
194
195
    The parse_ functions return {'contentFilter': (k,v),
196
                                 'parms': (k,v),
197
                                 'title': string
198
                                 }
199
200
    """
201
202
    def __init__(self, context, request):
203
        super(SelectionMacrosView, self).__init__(context, request)
204
        self.bc = self.bika_catalog
205
        self.bac = self.bika_analysis_catalog
206
        self.bsc = self.bika_setup_catalog
207
        self.pc = self.portal_catalog
208
        self.rc = self.reference_catalog
209
210
    select_analysiscategory_pt = ViewPageTemplateFile(
211
        "select_analysiscategory.pt")
212
213
    @ram.cache(_cache_key_select_analysiscategory)
214
    def select_analysiscategory(self, style=None):
215
        self.style = style
216
        self.analysiscategories = self.bsc(portal_type='AnalysisCategory',
217
                                           sort_on='sortable_title')
218
        return self.select_analysiscategory_pt()
219
220
    select_analysisservice_pt = ViewPageTemplateFile("select_analysisservice.pt")
221
222
    @ram.cache(_cache_key_select_analysisservice)
223
    def select_analysisservice(self, allow_blank=True, multiselect=False,
224
                               style=None):
225
        self.style = style
226
        self.allow_blank = allow_blank
227
        self.multiselect = multiselect
228
        self.analysisservices = self.bsc(portal_type='AnalysisService',
229
                                         sort_on='sortable_title')
230
        return self.select_analysisservice_pt()
231
232
    def parse_analysisservice(self, request):
233
        val = request.form.get("ServiceUID", "")
234
        if val:
235
            if not type(val) in (list, tuple):
236
                val = (val,)  # Single service
237
            val = [self.rc.lookupObject(s) for s in val]
238
            uids = [o.UID() for o in val]
239
            titles = [o.Title() for o in val]
240
            res = {}
241
            res['contentFilter'] = ('getServiceUID', uids)
242
            res['parms'] = {'title': _("Services"), 'value': ','.join(titles)}
243
            res['titles'] = ','.join(titles)
244
            return res
245
246
    select_analysisspecification_pt = ViewPageTemplateFile(
247
        "select_analysisspecification.pt")
248
249
    @ram.cache(_cache_key_select_analysisspecification)
250
    def select_analysisspecification(self, style=None):
251
        self.style = style
252
        res = []
253
        bsc = getToolByName(self.context, "bika_setup_catalog")
254
        for s in bsc(portal_type='AnalysisSpec'):
255
            res.append({'uid': s.UID, 'title': s.Title})
256
        self.specs = res
257
        return self.select_analysisspecification_pt()
258
259
    select_analyst_pt = ViewPageTemplateFile("select_analyst.pt")
260
261
    @ram.cache(_cache_key_select_analyst)
262
    def select_analyst(self, allow_blank=False, style=None):
263
        self.style = style
264
        self.analysts = getUsers(self.context,
265
                                 ['Manager', 'Analyst', 'LabManager'],
266
                                 allow_blank)
267
        return self.select_analyst_pt()
268
269
    select_user_pt = ViewPageTemplateFile("select_user.pt")
270
271
    @ram.cache(_cache_key_select_user)
272
    def select_user(self, allow_blank=True, style=None):
273
        self.style = style
274
        self.allow_blank = allow_blank
275
        self.users = getUsers(self.context, None, allow_blank)
276
        return self.select_user_pt()
277
278
    select_client_pt = ViewPageTemplateFile("select_client.pt")
279
280
    @ram.cache(_cache_key_select_client)
281
    def select_client(self, style=None):
282
        self.style = style
283
        self.clients = self.pc(portal_type='Client',
284
                               sort_on='sortable_title')
285
        return self.select_client_pt()
286
287
    def parse_client(self, request):
288
        val = request.form.get("ClientUID", "")
289
        if val:
290
            obj = val and self.rc.lookupObject(val)
291
            title = obj.Title()
292
            res = {}
293
            res['contentFilter'] = ('getClientUID', val)
294
            res['parms'] = {'title': _("Client"), 'value': title}
295
            res['titles'] = title
296
            return res
297
298
    select_contact_pt = ViewPageTemplateFile("select_contact.pt")
299
300
    @ram.cache(_cache_key_select_contact)
301
    def select_contact(self, style=None):
302
        self.style = style
303
        self.contacts = self.pc(portal_type='Contact', is_active=True,
304
                                sort_on='sortable_title')
305
        return self.select_contact_pt()
306
307
    select_daterange_pt = ViewPageTemplateFile("select_daterange.pt")
308
309
    def _select_daterange(self, field_id, field_title, style=None):
310
        self.style = style
311
        self.field_id = field_id
312
        self.field_title = _(field_title)
313
        return self.select_daterange_pt()
314
315
    @ram.cache(_cache_key_select_daterange)
316
    def select_daterange(self, field_id, field_title, style=None):
317
        return self._select_daterange(field_id, field_title, style)
318
319
    @ram.cache(_cache_key_select_daterange)
320
    def select_daterange_requested(self, field_id, field_title, style=None):
321
        return self._select_daterange(field_id, field_title, style)
322
323
    @ram.cache(_cache_key_select_daterange)
324
    def select_daterange_created(self, field_id, field_title, style=None):
325
        return self._select_daterange(field_id, field_title, style)
326
327
    @ram.cache(_cache_key_select_daterange)
328
    def select_daterange_received(self, field_id, field_title, style=None):
329
        return self._select_daterange(field_id, field_title, style)
330
331
    @ram.cache(_cache_key_select_daterange)
332
    def select_daterange_published(self, field_id, field_title, style=None):
333
        return self._select_daterange(field_id, field_title, style)
334
335
    @ram.cache(_cache_key_select_daterange)
336
    def select_daterange_loaded(self, field_id, field_title, style=None):
337
        return self._select_daterange(field_id, field_title, style)
338
339
    def parse_daterange(self, request, field_id, field_title):
340
        from_date = request.get('%s_fromdate' % field_id, None)
341
        from_date = from_date and from_date + ' 00:00' or None
342
        to_date = request.get('%s_todate' % field_id, None)
343
        to_date = to_date and to_date + ' 23:59' or None
344
        if from_date and to_date:
345
            query = {'query': [from_date, to_date], 'range': 'min:max'}
346
        elif from_date or to_date:
347
            query = {'query': from_date or to_date,
348
                     'range': from_date and 'min' or 'max'}
349
        else:
350
            return None
351
352
        if from_date and to_date:
353
            parms = translate(_("From ${start_date} to ${end_date}",
354
                               mapping={"start_date":from_date, "end_date":to_date}))
355
        elif from_date:
356
            parms = translate(_("Before ${start_date}",
357
                               mapping={"start_date":from_date}))
358
        elif to_date:
359
            parms = translate(_("After ${end_date}",
360
                               mapping={"end_date":to_date}))
361
362
        res = {}
363
        res['contentFilter'] = (field_id, query)
364
        res['parms'] = {'title': field_title, 'value': parms}
0 ignored issues
show
introduced by
The variable parms does not seem to be defined for all execution paths.
Loading history...
365
        res['titles'] = parms
366
        return res
367
368
    select_instrument_pt = ViewPageTemplateFile("select_instrument.pt")
369
370
    @ram.cache(_cache_key_select_instrument)
371
    def select_instrument(self, style=None):
372
        self.style = style
373
        self.instruments = self.bsc(portal_type='Instrument',
374
                                    is_active=True,
375
                                    sort_on='sortable_title')
376
        return self.select_instrument_pt()
377
378
    select_period_pt = ViewPageTemplateFile("select_period.pt")
379
380
    @ram.cache(_cache_key_select_period)
381
    def select_period(self, style=None):
382
        self.style = style
383
        return self.select_period_pt()
384
385
    select_profile_pt = ViewPageTemplateFile("select_profile.pt")
386
387
    @ram.cache(_cache_key_select_profile)
388
    def select_profile(self, style=None):
389
        self.style = style
390
        self.analysisprofiles = self.bsc(portal_type='AnalysisProfile',
391
                                         is_active=True,
392
                                         sort_on='sortable_title')
393
        return self.select_profile_pt()
394
395
    select_supplier_pt = ViewPageTemplateFile("select_supplier.pt")
396
397
    @ram.cache(_cache_key_select_supplier)
398
    def select_supplier(self, style=None):
399
        self.style = style
400
        self.suppliers = self.bsc(portal_type='Supplier', is_active=True,
401
                                  sort_on='sortable_title')
402
        return self.select_supplier_pt()
403
404
    select_reference_sample_pt = ViewPageTemplateFile(
405
        "select_reference_sample.pt")
406
407
    @ram.cache(_cache_key_select_reference_sample)
408
    def select_reference_sample(self, style=None):
409
        self.style = style
410
        return self.select_reference_sample_pt()
411
412
    select_reference_service_pt = ViewPageTemplateFile(
413
        "select_reference_service.pt")
414
415
    @ram.cache(_cache_key_select_reference_service)
416
    def select_reference_service(self, style=None):
417
        self.style = style
418
        return self.select_reference_service_pt()
419
420
    select_state_pt = ViewPageTemplateFile("select_state.pt")
421
422
    def _select_state(self, workflow_id, field_id, field_title, style=None):
423
        self.style = style
424
        self.field_id = field_id
425
        self.field_title = field_title
426
        states = self.portal_workflow[workflow_id].states
427
        self.states = []
428
        for state_id in states:
429
            state = states[state_id]
430
            self.states.append({'id': state.getId(), 'title': state.title})
431
        return self.select_state_pt()
432
433
    @ram.cache(_cache_key_select_state)
434
    def select_state(self, workflow_id, field_id, field_title, style=None):
435
        return self._select_state(workflow_id, field_title, field_title, style)
436
437
    @ram.cache(_cache_key_select_state)
438
    def select_state_analysis(self, workflow_id, field_id, field_title, style=None):
439
        return self._select_state(workflow_id, field_title, field_title, style)
440
441
    def parse_state(self, request, workflow_id, field_id, field_title):
442
        val = request.form.get(field_id, "")
443
        states = self.portal_workflow[workflow_id].states
444
        if val in states:
445
            state_title = states[val].title
446
            res = {}
447
            res['contentFilter'] = (field_id, val)
448
            res['parms'] = {'title': _('State'), 'value': state_title}
449
            res['titles'] = state_title
450
            return res
451
452
    select_sampletype_pt = ViewPageTemplateFile("select_sampletype.pt")
453
454
    @ram.cache(_cache_key_select_sample_type)
455
    def select_sampletype(self, allow_blank=True, multiselect=False, style=None):
456
        self.style = style
457
        self.allow_blank = allow_blank
458
        self.multiselect = multiselect
459
        self.sampletypes = self.bsc(portal_type='SampleType',
460
                                    is_active=True,
461
                                    sort_on='sortable_title')
462
        return self.select_sampletype_pt()
463
464
    def parse_sampletype(self, request):
465
        val = request.form.get("SampleTypeUID", "")
466
        if val:
467
            obj = val and self.rc.lookupObject(val)
468
            title = obj.Title()
469
            res = {}
470
            res['contentFilter'] = ('getSampleTypeUID', val)
471
            res['parms'] = {'title': _("Sample Type"), 'value': title}
472
            res['titles'] = title
473
            return res
474
475
    select_groupingperiod_pt = ViewPageTemplateFile("select_groupingperiod.pt")
476
477
    @ram.cache(_cache_key_select_groupingperiod)
478
    def select_groupingperiod(self, allow_blank=True, multiselect=False,
479
                              style=None):
480
        self.style = style
481
        self.allow_blank = allow_blank
482
        return self.select_groupingperiod_pt()
483
484
    select_output_format_pt = ViewPageTemplateFile("select_output_format.pt")
485
486
    @ram.cache(_cache_key_select_output_format)
487
    def select_output_format(self, style=None):
488
        self.style = style
489
        return self.select_output_format_pt()
490