Passed
Pull Request — 2.x (#1772)
by Jordi
04:57
created

senaite.core.browser.samples.view   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 721
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 60
eloc 547
dl 0
loc 721
rs 3.6
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
F SamplesView.folderitem() 0 176 31
A SamplesView.is_printing_workflow_enabled() 0 4 1
A SamplesView.flat_listing() 0 3 1
A SamplesView.before_render() 0 17 3
A SamplesView.str_date() 0 4 2
A SamplesView.getDefaultAddCount() 0 2 1
A SamplesView.add_custom_transitions() 0 21 4
A SamplesView.update() 0 18 1
B SamplesView.purge_review_states() 0 16 6
B SamplesView.__init__() 0 377 1
A SamplesView.copy_to_new_allowed() 0 6 2
A SamplesView.purge_columns() 0 10 4
A SamplesView.show_partitions() 0 8 3

How to fix   Complexity   

Complexity

Complex classes like senaite.core.browser.samples.view often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
22
import collections
23
24
from bika.lims import _
25
from bika.lims import api
26
from bika.lims.catalog import CATALOG_ANALYSIS_REQUEST_LISTING
27
from bika.lims.config import PRIORITIES
28
from bika.lims.permissions import AddAnalysisRequest
29
from bika.lims.permissions import TransitionSampleSample
30
from bika.lims.utils import get_image
31
from bika.lims.utils import get_progress_bar_html
32
from bika.lims.utils import getUsers
33
from bika.lims.utils import t
34
from DateTime import DateTime
35
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
36
from senaite.app.listing import ListingView
37
38
39
class SamplesView(ListingView):
40
    """Listing View for Samples (AnalysisRequest content type) in the System
41
    """
42
43
    template = ViewPageTemplateFile("templates/samples.pt")
44
45
    def __init__(self, context, request):
46
        super(SamplesView, self).__init__(context, request)
47
48
        self.catalog = CATALOG_ANALYSIS_REQUEST_LISTING
49
        self.contentFilter = {
50
            "sort_on": "created",
51
            "sort_order": "descending",
52
            "isRootAncestor": True,  # only root ancestors
53
        }
54
55
        self.title = self.context.translate(_("Samples"))
56
        self.description = ""
57
58
        self.show_select_column = True
59
        self.form_id = "samples"
60
        self.context_actions = {}
61
62
        self.url = api.get_url(self.context)
63
64
        # Toggle some columns if the sampling workflow is enabled
65
        sampling_enabled = api.get_setup().getSamplingWorkflowEnabled()
66
67
        self.columns = collections.OrderedDict((
68
            ("Priority", {
69
                "title": "",
70
                "index": "getPrioritySortkey",
71
                "sortable": True, }),
72
            ("Progress", {
73
                "title": "Progress",
74
                "index": "getProgress",
75
                "sortable": True,
76
                "toggle": True}),
77
            ("getId", {
78
                "title": _("Sample ID"),
79
                "attr": "getId",
80
                "replace_url": "getURL",
81
                "index": "getId"}),
82
            ("getClientOrderNumber", {
83
                "title": _("Client Order"),
84
                "sortable": True,
85
                "toggle": False}),
86
            ("Creator", {
87
                "title": _("Creator"),
88
                "index": "getCreatorFullName",
89
                "sortable": True,
90
                "toggle": True}),
91
            ("Created", {
92
                "title": _("Date Registered"),
93
                "index": "created",
94
                "toggle": False}),
95
            ("SamplingDate", {
96
                "title": _("Expected Sampling Date"),
97
                "index": "getSamplingDate",
98
                "toggle": sampling_enabled}),
99
            ("getDateSampled", {
100
                "title": _("Date Sampled"),
101
                "toggle": True,
102
                "input_class": "datetimepicker_nofuture",
103
                "input_width": "10"}),
104
            ("getDatePreserved", {
105
                "title": _("Date Preserved"),
106
                "toggle": False,
107
                "input_class": "datetimepicker_nofuture",
108
                "input_width": "10",
109
                "sortable": False}),  # no datesort without index
110
            ("getDateReceived", {
111
                "title": _("Date Received"),
112
                "toggle": False}),
113
            ("getDueDate", {
114
                "title": _("Due Date"),
115
                "toggle": False}),
116
            ("getDateVerified", {
117
                "title": _("Date Verified"),
118
                "input_width": "10",
119
                "toggle": False}),
120
            ("getDatePublished", {
121
                "title": _("Date Published"),
122
                "toggle": False}),
123
            ("BatchID", {
124
                "title": _("Batch ID"),
125
                "index": "getBatchID",
126
                "sortable": True,
127
                "toggle": False}),
128
            ("Client", {
129
                "title": _("Client"),
130
                "index": "getClientTitle",
131
                "attr": "getClientTitle",
132
                "replace_url": "getClientURL",
133
                "toggle": True}),
134
            ("ClientID", {
135
                "title": _("Client ID"),
136
                "index": "getClientID",
137
                "attr": "getClientID",
138
                "replace_url": "getClientURL",
139
                "toggle": True}),
140
            ("Province", {
141
                "title": _("Province"),
142
                "sortable": True,
143
                "index": "getProvince",
144
                "attr": "getProvince",
145
                "toggle": False}),
146
            ("District", {
147
                "title": _("District"),
148
                "sortable": True,
149
                "index": "getDistrict",
150
                "attr": "getDistrict",
151
                "toggle": False}),
152
            ("getClientReference", {
153
                "title": _("Client Ref"),
154
                "sortable": True,
155
                "index": "getClientReference",
156
                "toggle": False}),
157
            ("getClientSampleID", {
158
                "title": _("Client SID"),
159
                "toggle": False}),
160
            ("ClientContact", {
161
                "title": _("Contact"),
162
                "sortable": True,
163
                "index": "getContactFullName",
164
                "toggle": False}),
165
            ("getSampleTypeTitle", {
166
                "title": _("Sample Type"),
167
                "sortable": True,
168
                "toggle": True}),
169
            ("getSamplePointTitle", {
170
                "title": _("Sample Point"),
171
                "sortable": True,
172
                "index": "getSamplePointTitle",
173
                "toggle": False}),
174
            ("getStorageLocation", {
175
                "title": _("Storage Location"),
176
                "sortable": True,
177
                "index": "getStorageLocationTitle",
178
                "toggle": False}),
179
            ("SamplingDeviation", {
180
                "title": _("Sampling Deviation"),
181
                "sortable": True,
182
                "index": "getSamplingDeviationTitle",
183
                "toggle": False}),
184
            ("getSampler", {
185
                "title": _("Sampler"),
186
                "toggle": sampling_enabled}),
187
            ("getPreserver", {
188
                "title": _("Preserver"),
189
                "sortable": False,
190
                "toggle": False}),
191
            ("getProfilesTitle", {
192
                "title": _("Profile"),
193
                "sortable": True,
194
                "index": "getProfilesTitle",
195
                "toggle": False}),
196
            ("getAnalysesNum", {
197
                "title": _("Number of Analyses"),
198
                "sortable": True,
199
                "index": "getAnalysesNum",
200
                "toggle": False}),
201
            ("getTemplateTitle", {
202
                "title": _("Template"),
203
                "sortable": True,
204
                "index": "getTemplateTitle",
205
                "toggle": False}),
206
            ("Printed", {
207
                "title": _("Printed"),
208
                "sortable": False,
209
                "index": "getPrinted",
210
                "toggle": False}),
211
            ("state_title", {
212
                "title": _("State"),
213
                "sortable": True,
214
                "index": "review_state"}),
215
        ))
216
217
        # custom print transition
218
        print_stickers = {
219
            "id": "print_stickers",
220
            "title": _("Print stickers"),
221
            "url": "{}/workflow_action?action=print_stickers".format(self.url)
222
        }
223
224
        self.review_states = [
225
            {
226
                "id": "default",
227
                "title": _("Active"),
228
                "contentFilter": {
229
                    "review_state": (
230
                        "sample_registered",
231
                        "scheduled_sampling",
232
                        "to_be_sampled",
233
                        "sample_due",
234
                        "sample_received",
235
                        "to_be_preserved",
236
                        "to_be_verified",
237
                        "verified",
238
                    ),
239
                    "sort_on": "created",
240
                    "sort_order": "descending",
241
                },
242
                "custom_transitions": [print_stickers],
243
                "columns": self.columns.keys(),
244
            }, {
245
                "id": "to_be_sampled",
246
                "title": _("To Be Sampled"),
247
                "contentFilter": {
248
                    "review_state": ("to_be_sampled",),
249
                    "sort_on": "created",
250
                    "sort_order": "descending"},
251
                "custom_transitions": [print_stickers],
252
                "columns": self.columns.keys()
253
            }, {
254
                "id": "to_be_preserved",
255
                "title": _("To Be Preserved"),
256
                "contentFilter": {
257
                    "review_state": ("to_be_preserved",),
258
                    "sort_on": "created",
259
                    "sort_order": "descending",
260
                },
261
                "custom_transitions": [print_stickers],
262
                "columns": self.columns.keys(),
263
            }, {
264
                "id": "scheduled_sampling",
265
                "title": _("Scheduled sampling"),
266
                "contentFilter": {
267
                    "review_state": ("scheduled_sampling",),
268
                    "sort_on": "created",
269
                    "sort_order": "descending",
270
                },
271
                "custom_transitions": [print_stickers],
272
                "columns": self.columns.keys(),
273
            }, {
274
                "id": "sample_due",
275
                "title": _("Due"),
276
                "contentFilter": {
277
                    "review_state": (
278
                        "to_be_sampled",
279
                        "to_be_preserved",
280
                        "sample_due"),
281
                    "sort_on": "created",
282
                    "sort_order": "descending"},
283
                "custom_transitions": [print_stickers],
284
                "columns": self.columns.keys(),
285
            }, {
286
                "id": "sample_received",
287
                "title": _("Received"),
288
                "contentFilter": {
289
                    "review_state": "sample_received",
290
                    "sort_on": "created",
291
                    "sort_order": "descending",
292
                },
293
                "custom_transitions": [print_stickers],
294
                "columns": self.columns.keys(),
295
            }, {
296
                "id": "to_be_verified",
297
                "title": _("To be verified"),
298
                "contentFilter": {
299
                    "review_state": "to_be_verified",
300
                    "sort_on": "created",
301
                    "sort_order": "descending",
302
                },
303
                "custom_transitions": [print_stickers],
304
                "columns": self.columns.keys(),
305
            }, {
306
                "id": "verified",
307
                "title": _("Verified"),
308
                "contentFilter": {
309
                    "review_state": "verified",
310
                    "sort_on": "created",
311
                    "sort_order": "descending",
312
                },
313
                "custom_transitions": [print_stickers],
314
                "columns": self.columns.keys(),
315
            }, {
316
                "id": "published",
317
                "title": _("Published"),
318
                "contentFilter": {
319
                    "review_state": ("published"),
320
                    "sort_on": "created",
321
                    "sort_order": "descending",
322
                },
323
                "custom_transitions": [],
324
                "columns": self.columns.keys(),
325
            }, {
326
                "id": "dispatched",
327
                "title": _("Dispatched"),
328
                "flat_listing": True,
329
                "confirm_transitions": ["restore"],
330
                "contentFilter": {
331
                    "review_state": ("dispatched"),
332
                    "sort_on": "created",
333
                    "sort_order": "descending",
334
                },
335
                "custom_transitions": [],
336
                "columns": self.columns.keys(),
337
            }, {
338
                "id": "cancelled",
339
                "title": _("Cancelled"),
340
                "contentFilter": {
341
                    "review_state": "cancelled",
342
                    "sort_on": "created",
343
                    "sort_order": "descending",
344
                },
345
                "custom_transitions": [],
346
                "columns": self.columns.keys(),
347
            }, {
348
                "id": "invalid",
349
                "title": _("Invalid"),
350
                "contentFilter": {
351
                    "review_state": "invalid",
352
                    "sort_on": "created",
353
                    "sort_order": "descending",
354
                },
355
                "custom_transitions": [print_stickers],
356
                "columns": self.columns.keys(),
357
            }, {
358
                "id": "all",
359
                "title": _("All"),
360
                "contentFilter": {
361
                    "sort_on": "created",
362
                    "sort_order": "descending",
363
                },
364
                "custom_transitions": [print_stickers],
365
                "columns": self.columns.keys(),
366
            }, {
367
                "id": "rejected",
368
                "title": _("Rejected"),
369
                "contentFilter": {
370
                    "review_state": "rejected",
371
                    "sort_on": "created",
372
                    "sort_order": "descending",
373
                },
374
                "custom_transitions": [print_stickers],
375
                "columns": self.columns.keys(),
376
            }, {
377
                "id": "assigned",
378
                "title": get_image("assigned.png",
379
                                   title=t(_("Assigned"))),
380
                "contentFilter": {
381
                    "assigned_state": "assigned",
382
                    "review_state": ("sample_received",),
383
                    "sort_on": "created",
384
                    "sort_order": "descending",
385
                },
386
                "custom_transitions": [print_stickers],
387
                "columns": self.columns.keys(),
388
            }, {
389
                "id": "unassigned",
390
                "title": get_image("unassigned.png",
391
                                   title=t(_("Unsassigned"))),
392
                "contentFilter": {
393
                    "assigned_state": "unassigned",
394
                    "review_state": (
395
                        "sample_received",
396
                    ),
397
                    "sort_on": "created",
398
                    "sort_order": "descending",
399
                },
400
                "custom_transitions": [print_stickers],
401
                "columns": self.columns.keys(),
402
            }, {
403
                "id": "late",
404
                "title": get_image("late.png",
405
                                   title=t(_("Late"))),
406
                "contentFilter": {
407
                    # Query only for unpublished ARs that are late
408
                    "review_state": (
409
                        "sample_received",
410
                        "to_be_verified",
411
                        "verified",
412
                    ),
413
                    "getDueDate": {
414
                        "query": DateTime(),
415
                        "range": "max",
416
                    },
417
                    "sort_on": "created",
418
                    "sort_order": "descending",
419
                },
420
                "custom_transitions": [print_stickers],
421
                "columns": self.columns.keys(),
422
            }
423
        ]
424
425
    def update(self):
426
        """Called before the listing renders
427
        """
428
        super(SamplesView, self).update()
429
430
        setup = api.get_setup()
431
        self.workflow = api.get_tool("portal_workflow")
432
        self.member = self.mtool.getAuthenticatedMember()
433
        self.roles = self.member.getRoles()
434
435
        # Remove unnecessary filters
436
        self.purge_review_states()
437
438
        # Remove unnecessary columns
439
        self.purge_columns()
440
441
        # Additional custom transitions
442
        self.add_custom_transitions()
443
444
    def before_render(self):
445
        """Before template render hook
446
        """
447
        # If the current user is a client contact, display those analysis
448
        # requests that belong to same client only
449
        super(SamplesView, self).before_render()
450
        client = api.get_current_client()
451
        if client:
452
            self.contentFilter['path'] = {
453
                "query": "/".join(client.getPhysicalPath()),
454
                "level": 0}
455
            # No need to display the Client column
456
            self.remove_column('Client')
457
458
        # remove query filter for root samples when listing is flat
459
        if self.flat_listing:
460
            self.contentFilter.pop("isRootAncestor", None)
461
462
    def folderitem(self, obj, item, index):
463
        # Additional info from AnalysisRequest to be added in the item
464
        # generated by default by bikalisting.
465
        # Call the folderitem method from the base class
466
        item = super(SamplesView, self).folderitem(obj, item, index)
467
        if not item:
468
            return None
469
470
        item["Creator"] = self.user_fullname(obj.Creator)
471
        # If we redirect from the folderitems view we should check if the
472
        # user has permissions to medify the element or not.
473
        priority_sort_key = obj.getPrioritySortkey
474
        if not priority_sort_key:
475
            # Default priority is Medium = 3.
476
            # The format of PrioritySortKey is <priority>.<created>
477
            priority_sort_key = "3.%s" % obj.created.ISO8601()
478
        priority = priority_sort_key.split(".")[0]
479
        priority_text = PRIORITIES.getValue(priority)
480
        priority_div = """<div class="priority-ico priority-%s">
481
                          <span class="notext">%s</span><div>
482
                       """
483
        item["replace"]["Priority"] = priority_div % (priority, priority_text)
484
        item["replace"]["getProfilesTitle"] = obj.getProfilesTitleStr
485
486
        analysesnum = obj.getAnalysesNum
487
        if analysesnum:
488
            num_verified = str(analysesnum[0])
489
            num_total = str(analysesnum[1])
490
            item["getAnalysesNum"] = "{0}/{1}".format(num_verified, num_total)
491
        else:
492
            item["getAnalysesNum"] = ""
493
494
        # Progress
495
        progress_perc = obj.getProgress
496
        item["Progress"] = progress_perc
497
        item["replace"]["Progress"] = get_progress_bar_html(progress_perc)
498
499
        item["BatchID"] = obj.getBatchID
500
        if obj.getBatchID:
501
            item['replace']['BatchID'] = "<a href='%s'>%s</a>" % \
502
                                         (obj.getBatchURL, obj.getBatchID)
503
        # TODO: SubGroup ???
504
        # val = obj.Schema().getField('SubGroup').get(obj)
505
        # item['SubGroup'] = val.Title() if val else ''
506
507
        item["SamplingDate"] = self.str_date(obj.getSamplingDate)
508
        item["getDateReceived"] = self.str_date(obj.getDateReceived)
509
        item["getDueDate"] = self.str_date(obj.getDueDate)
510
        item["getDatePublished"] = self.str_date(obj.getDatePublished)
511
        item["getDateVerified"] = self.str_date(obj.getDateVerified)
512
513
        if self.is_printing_workflow_enabled:
514
            item["Printed"] = ""
515
            printed = obj.getPrinted if hasattr(obj, "getPrinted") else "0"
516
            print_icon = ""
517
            if printed == "0":
518
                print_icon = get_image("delete.png",
519
                                       title=t(_("Not printed yet")))
520
            elif printed == "1":
521
                print_icon = get_image("ok.png",
522
                                       title=t(_("Printed")))
523
            elif printed == "2":
524
                print_icon = get_image(
525
                    "exclamation.png",
526
                    title=t(_("Republished after last print")))
527
            item["after"]["Printed"] = print_icon
528
        item["SamplingDeviation"] = obj.getSamplingDeviationTitle
529
530
        item["getStorageLocation"] = obj.getStorageLocationTitle
531
532
        after_icons = ""
533
        if obj.assigned_state == 'assigned':
534
            after_icons += get_image("worksheet.png",
535
                                     title=t(_("All analyses assigned")))
536
        if item["review_state"] == 'invalid':
537
            after_icons += get_image("delete.png",
538
                                     title=t(_("Results have been withdrawn")))
539
540
        due_date = obj.getDueDate
541
        if due_date and due_date < (obj.getDatePublished or DateTime()):
542
            due_date_str = self.ulocalized_time(due_date)
543
            img_title = "{}: {}".format(t(_("Late Analyses")), due_date_str)
544
            after_icons += get_image("late.png", title=img_title)
545
546
        if obj.getSamplingDate and obj.getSamplingDate > DateTime():
547
            after_icons += get_image("calendar.png",
548
                                     title=t(_("Future dated sample")))
549
        if obj.getInvoiceExclude:
550
            after_icons += get_image("invoice_exclude.png",
551
                                     title=t(_("Exclude from invoice")))
552
        if obj.getHazardous:
553
            after_icons += get_image("hazardous.png",
554
                                     title=t(_("Hazardous")))
555
556
        if obj.getInternalUse:
557
            after_icons += get_image("locked.png", title=t(_("Internal use")))
558
559
        if after_icons:
560
            item['after']['getId'] = after_icons
561
562
        item['Created'] = self.ulocalized_time(obj.created, long_format=1)
563
        if obj.getContactUID:
564
            item['ClientContact'] = obj.getContactFullName
565
            item['replace']['ClientContact'] = "<a href='%s'>%s</a>" % \
566
                                               (obj.getContactURL, obj.getContactFullName)
567
        else:
568
            item["ClientContact"] = ""
569
        # TODO-performance: If SamplingWorkflowEnabled, we have to get the
570
        # full object to check the user permissions, so far this is
571
        # a performance hit.
572
        if obj.getSamplingWorkflowEnabled:
573
            # We don't do anything with Sampling Date.
574
            # User can modify Sampling date
575
            # inside AR view. In this listing view,
576
            # we only let the user to edit Date Sampled
577
            # and Sampler if he wants to make 'sample' transaction.
578
            if not obj.getDateSampled:
579
                datesampled = self.ulocalized_time(
580
                    DateTime(), long_format=True)
581
                item["class"]["getDateSampled"] = "provisional"
582
            else:
583
                datesampled = self.ulocalized_time(obj.getDateSampled,
584
                                                   long_format=True)
585
586
            sampler = obj.getSampler
587
            if sampler:
588
                item["replace"]["getSampler"] = obj.getSamplerFullName
589
            if "Sampler" in self.roles and not sampler:
590
                sampler = self.member.id
591
                item["class"]["getSampler"] = "provisional"
592
            # sampling workflow - inline edits for Sampler and Date Sampled
593
            if item["review_state"] == 'to_be_sampled':
594
                # We need to get the full object in order to check
595
                # the permissions
596
                full_object = obj.getObject()
597
                checkPermission = \
598
                    self.context.portal_membership.checkPermission
599
600
                # TODO Do we really need this check?
601
                if checkPermission(TransitionSampleSample, full_object):
602
                    item["required"] = ["getSampler", "getDateSampled"]
603
                    item["allow_edit"] = ["getSampler", "getDateSampled"]
604
                    # TODO-performance: hit performance while getting the
605
                    # sample object...
606
                    # TODO Can LabManagers be a Sampler?!
607
                    samplers = getUsers(full_object, ["Sampler", ])
608
                    username = self.member.getUserName()
609
                    users = [({
610
                        "ResultValue": u,
611
                        "ResultText": samplers.getValue(u)}) for u in samplers]
612
                    item['choices'] = {'getSampler': users}
613
                    Sampler = sampler and sampler or \
614
                              (username in samplers.keys() and username) or ''
615
                    sampler = Sampler
616
                else:
617
                    datesampled = self.ulocalized_time(obj.getDateSampled,
618
                                                       long_format=True)
619
                    sampler = obj.getSamplerFullName if obj.getSampler else ''
620
        else:
621
            datesampled = self.ulocalized_time(obj.getDateSampled,
622
                                               long_format=True)
623
            sampler = ""
624
        item["getDateSampled"] = datesampled
625
        item["getSampler"] = sampler
626
627
        # These don't exist on ARs
628
        # XXX This should be a list of preservers...
629
        item["getPreserver"] = ""
630
        item["getDatePreserved"] = ""
631
632
        # Assign parent and children partitions of this sample
633
        if self.show_partitions:
634
            item["parent"] = obj.getRawParentAnalysisRequest
635
            item["children"] = obj.getDescendantsUIDs or []
636
637
        return item
638
639
    def purge_review_states(self):
640
        """Purges unnecessary review statuses
641
        """
642
        remove_filters = []
643
        setup = api.get_bika_setup()
644
        if not setup.getSamplingWorkflowEnabled():
645
            remove_filters.append("to_be_sampled")
646
        if not setup.getScheduleSamplingEnabled():
647
            remove_filters.append("scheduled_sampling")
648
        if not setup.getSamplePreservationEnabled():
649
            remove_filters.append("to_be_preserved")
650
        if not setup.getRejectionReasons():
651
            remove_filters.append("rejected")
652
653
        self.review_states = filter(lambda r: r.get("id") not in remove_filters,
654
                                    self.review_states)
655
656
    def purge_columns(self):
657
        """Purges unnecessary columns
658
        """
659
        remove_columns = []
660
        if not self.is_printing_workflow_enabled:
661
            remove_columns.append("Printed")
662
663
        for rv in self.review_states:
664
            cols = rv.get("columns", [])
665
            rv["columns"] = filter(lambda c: c not in remove_columns, cols)
666
667
    def add_custom_transitions(self):
668
        """Adds custom transitions as required
669
        """
670
        custom_transitions = []
671
        if self.is_printing_workflow_enabled:
672
            custom_transitions.append({
673
                "id": "print_sample",
674
                "title": _("Print"),
675
                "url": "{}/workflow_action?action={}".format(
676
                    self.url, "print_sample")
677
            })
678
        if self.copy_to_new_allowed:
679
            custom_transitions.append({
680
                "id": "copy_to_new",
681
                "title": _("Copy to new"),
682
                "url": "{}/workflow_action?action={}".format(
683
                    self.url, "copy_to_new")
684
            })
685
686
        for rv in self.review_states:
687
            rv.setdefault("custom_transitions", []).extend(custom_transitions)
688
689
    @property
690
    def copy_to_new_allowed(self):
691
        mtool = api.get_tool("portal_membership")
692
        if mtool.checkPermission(AddAnalysisRequest, self.context):
693
            return True
694
        return False
695
696
    @property
697
    def is_printing_workflow_enabled(self):
698
        setup = api.get_setup()
699
        return setup.getPrintingWorkflowEnabled()
700
701
    def str_date(self, date, long_format=1, default=""):
702
        if not date:
703
            return default
704
        return self.ulocalized_time(date, long_format=long_format)
705
706
    def getDefaultAddCount(self):
707
        return self.context.bika_setup.getDefaultNumberOfARsToAdd()
708
709
    @property
710
    def show_partitions(self):
711
        if self.flat_listing:
712
            return False
713
        if api.get_current_client():
714
            # If current user is a client contact, delegate to ShowPartitions
715
            return api.get_setup().getShowPartitions()
716
        return True
717
718
    @property
719
    def flat_listing(self):
720
        return self.review_state.get("flat_listing", False)
721