Passed
Pull Request — 2.x (#1739)
by Jordi
05:20
created

senaite.core.browser.samples.view   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 701
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 57
eloc 530
dl 0
loc 701
rs 5.04
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
B SamplesView.__init__() 0 365 1
F SamplesView.folderitem() 0 178 31
A SamplesView.is_printing_workflow_enabled() 0 4 1
A SamplesView.before_render() 0 13 2
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
A SamplesView.copy_to_new_allowed() 0 6 2
A SamplesView.purge_columns() 0 10 4
A SamplesView.show_partitions() 0 6 2

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": "cancelled",
327
                "title": _("Cancelled"),
328
                "contentFilter": {
329
                    "review_state": "cancelled",
330
                    "sort_on": "created",
331
                    "sort_order": "descending",
332
                },
333
                "custom_transitions": [],
334
                "columns": self.columns.keys(),
335
            }, {
336
                "id": "invalid",
337
                "title": _("Invalid"),
338
                "contentFilter": {
339
                    "review_state": "invalid",
340
                    "sort_on": "created",
341
                    "sort_order": "descending",
342
                },
343
                "custom_transitions": [print_stickers],
344
                "columns": self.columns.keys(),
345
            }, {
346
                "id": "all",
347
                "title": _("All"),
348
                "contentFilter": {
349
                    "sort_on": "created",
350
                    "sort_order": "descending",
351
                },
352
                "custom_transitions": [print_stickers],
353
                "columns": self.columns.keys(),
354
            }, {
355
                "id": "rejected",
356
                "title": _("Rejected"),
357
                "contentFilter": {
358
                    "review_state": "rejected",
359
                    "sort_on": "created",
360
                    "sort_order": "descending",
361
                },
362
                "custom_transitions": [print_stickers],
363
                "columns": self.columns.keys(),
364
            }, {
365
                "id": "assigned",
366
                "title": get_image("assigned.png",
367
                                   title=t(_("Assigned"))),
368
                "contentFilter": {
369
                    "assigned_state": "assigned",
370
                    "review_state": ("sample_received",),
371
                    "sort_on": "created",
372
                    "sort_order": "descending",
373
                },
374
                "custom_transitions": [print_stickers],
375
                "columns": self.columns.keys(),
376
            }, {
377
                "id": "unassigned",
378
                "title": get_image("unassigned.png",
379
                                   title=t(_("Unsassigned"))),
380
                "contentFilter": {
381
                    "assigned_state": "unassigned",
382
                    "review_state": (
383
                        "sample_received",
384
                    ),
385
                    "sort_on": "created",
386
                    "sort_order": "descending",
387
                },
388
                "custom_transitions": [print_stickers],
389
                "columns": self.columns.keys(),
390
            }, {
391
                "id": "late",
392
                "title": get_image("late.png",
393
                                   title=t(_("Late"))),
394
                "contentFilter": {
395
                    # Query only for unpublished ARs that are late
396
                    "review_state": (
397
                        "sample_received",
398
                        "to_be_verified",
399
                        "verified",
400
                    ),
401
                    "getDueDate": {
402
                        "query": DateTime(),
403
                        "range": "max",
404
                    },
405
                    "sort_on": "created",
406
                    "sort_order": "descending",
407
                },
408
                "custom_transitions": [print_stickers],
409
                "columns": self.columns.keys(),
410
            }
411
        ]
412
413
    def update(self):
414
        """Called before the listing renders
415
        """
416
        super(SamplesView, self).update()
417
418
        setup = api.get_setup()
419
        self.workflow = api.get_tool("portal_workflow")
420
        self.member = self.mtool.getAuthenticatedMember()
421
        self.roles = self.member.getRoles()
422
423
        # Remove unnecessary filters
424
        self.purge_review_states()
425
426
        # Remove unnecessary columns
427
        self.purge_columns()
428
429
        # Additional custom transitions
430
        self.add_custom_transitions()
431
432
    def before_render(self):
433
        """Before template render hook
434
        """
435
        # If the current user is a client contact, display those analysis
436
        # requests that belong to same client only
437
        super(SamplesView, self).before_render()
438
        client = api.get_current_client()
439
        if client:
440
            self.contentFilter['path'] = {
441
                "query": "/".join(client.getPhysicalPath()),
442
                "level": 0}
443
            # No need to display the Client column
444
            self.remove_column('Client')
445
446
    def folderitem(self, obj, item, index):
447
        # Additional info from AnalysisRequest to be added in the item
448
        # generated by default by bikalisting.
449
        # Call the folderitem method from the base class
450
        item = super(SamplesView, self).folderitem(obj, item, index)
451
        if not item:
452
            return None
453
454
        item["Creator"] = self.user_fullname(obj.Creator)
455
        # If we redirect from the folderitems view we should check if the
456
        # user has permissions to medify the element or not.
457
        priority_sort_key = obj.getPrioritySortkey
458
        if not priority_sort_key:
459
            # Default priority is Medium = 3.
460
            # The format of PrioritySortKey is <priority>.<created>
461
            priority_sort_key = "3.%s" % obj.created.ISO8601()
462
        priority = priority_sort_key.split(".")[0]
463
        priority_text = PRIORITIES.getValue(priority)
464
        priority_div = """<div class="priority-ico priority-%s">
465
                          <span class="notext">%s</span><div>
466
                       """
467
        item["replace"]["Priority"] = priority_div % (priority, priority_text)
468
        item["replace"]["getProfilesTitle"] = obj.getProfilesTitleStr
469
470
        analysesnum = obj.getAnalysesNum
471
        if analysesnum:
472
            num_verified = str(analysesnum[0])
473
            num_total = str(analysesnum[1])
474
            item["getAnalysesNum"] = "{0}/{1}".format(num_verified, num_total)
475
        else:
476
            item["getAnalysesNum"] = ""
477
478
        # Progress
479
        progress_perc = obj.getProgress
480
        item["Progress"] = progress_perc
481
        item["replace"]["Progress"] = get_progress_bar_html(progress_perc)
482
483
        item["BatchID"] = obj.getBatchID
484
        if obj.getBatchID:
485
            item['replace']['BatchID'] = "<a href='%s'>%s</a>" % \
486
                                         (obj.getBatchURL, obj.getBatchID)
487
        # TODO: SubGroup ???
488
        # val = obj.Schema().getField('SubGroup').get(obj)
489
        # item['SubGroup'] = val.Title() if val else ''
490
491
        item["SamplingDate"] = self.str_date(obj.getSamplingDate)
492
        item["getDateReceived"] = self.str_date(obj.getDateReceived)
493
        item["getDueDate"] = self.str_date(obj.getDueDate)
494
        item["getDatePublished"] = self.str_date(obj.getDatePublished)
495
        item["getDateVerified"] = self.str_date(obj.getDateVerified)
496
497
        if self.is_printing_workflow_enabled:
498
            item["Printed"] = ""
499
            printed = obj.getPrinted if hasattr(obj, "getPrinted") else "0"
500
            print_icon = ""
501
            if printed == "0":
502
                print_icon = get_image("delete.png",
503
                                       title=t(_("Not printed yet")))
504
            elif printed == "1":
505
                print_icon = get_image("ok.png",
506
                                       title=t(_("Printed")))
507
            elif printed == "2":
508
                print_icon = get_image(
509
                    "exclamation.png",
510
                    title=t(_("Republished after last print")))
511
            item["after"]["Printed"] = print_icon
512
        item["SamplingDeviation"] = obj.getSamplingDeviationTitle
513
514
        item["getStorageLocation"] = obj.getStorageLocationTitle
515
516
        after_icons = ""
517
        if obj.assigned_state == 'assigned':
518
            after_icons += get_image("worksheet.png",
519
                                     title=t(_("All analyses assigned")))
520
        if item["review_state"] == 'invalid':
521
            after_icons += get_image("delete.png",
522
                                     title=t(_("Results have been withdrawn")))
523
524
        due_date = obj.getDueDate
525
        if due_date and due_date < (obj.getDatePublished or DateTime()):
526
            due_date_str = self.ulocalized_time(due_date)
527
            img_title = "{}: {}".format(t(_("Late Analyses")), due_date_str)
528
            after_icons += get_image("late.png", title=img_title)
529
530
        if obj.getSamplingDate and obj.getSamplingDate > DateTime():
531
            after_icons += get_image("calendar.png",
532
                                     title=t(_("Future dated sample")))
533
        if obj.getInvoiceExclude:
534
            after_icons += get_image("invoice_exclude.png",
535
                                     title=t(_("Exclude from invoice")))
536
        if obj.getHazardous:
537
            after_icons += get_image("hazardous.png",
538
                                     title=t(_("Hazardous")))
539
540
        if obj.getInternalUse:
541
            after_icons += get_image("locked.png", title=t(_("Internal use")))
542
543
        if after_icons:
544
            item['after']['getId'] = after_icons
545
546
        item['Created'] = self.ulocalized_time(obj.created, long_format=1)
547
        if obj.getContactUID:
548
            item['ClientContact'] = obj.getContactFullName
549
            item['replace']['ClientContact'] = "<a href='%s'>%s</a>" % \
550
                                               (obj.getContactURL, obj.getContactFullName)
551
        else:
552
            item["ClientContact"] = ""
553
        # TODO-performance: If SamplingWorkflowEnabled, we have to get the
554
        # full object to check the user permissions, so far this is
555
        # a performance hit.
556
        if obj.getSamplingWorkflowEnabled:
557
            # We don't do anything with Sampling Date.
558
            # User can modify Sampling date
559
            # inside AR view. In this listing view,
560
            # we only let the user to edit Date Sampled
561
            # and Sampler if he wants to make 'sample' transaction.
562
            if not obj.getDateSampled:
563
                datesampled = self.ulocalized_time(
564
                    DateTime(), long_format=True)
565
                item["class"]["getDateSampled"] = "provisional"
566
            else:
567
                datesampled = self.ulocalized_time(obj.getDateSampled,
568
                                                   long_format=True)
569
570
            sampler = obj.getSampler
571
            if sampler:
572
                item["replace"]["getSampler"] = obj.getSamplerFullName
573
            if "Sampler" in self.roles and not sampler:
574
                sampler = self.member.id
575
                item["class"]["getSampler"] = "provisional"
576
            # sampling workflow - inline edits for Sampler and Date Sampled
577
            if item["review_state"] == 'to_be_sampled':
578
                # We need to get the full object in order to check
579
                # the permissions
580
                full_object = obj.getObject()
581
                checkPermission = \
582
                    self.context.portal_membership.checkPermission
583
584
                # TODO Do we really need this check?
585
                if checkPermission(TransitionSampleSample, full_object):
586
                    item["required"] = ["getSampler", "getDateSampled"]
587
                    item["allow_edit"] = ["getSampler", "getDateSampled"]
588
                    # TODO-performance: hit performance while getting the
589
                    # sample object...
590
                    # TODO Can LabManagers be a Sampler?!
591
                    samplers = getUsers(full_object, ["Sampler", ])
592
                    username = self.member.getUserName()
593
                    users = [({
594
                        "ResultValue": u,
595
                        "ResultText": samplers.getValue(u)}) for u in samplers]
596
                    item['choices'] = {'getSampler': users}
597
                    Sampler = sampler and sampler or \
598
                              (username in samplers.keys() and username) or ''
599
                    sampler = Sampler
600
                else:
601
                    datesampled = self.ulocalized_time(obj.getDateSampled,
602
                                                       long_format=True)
603
                    sampler = obj.getSamplerFullName if obj.getSampler else ''
604
        else:
605
            datesampled = self.ulocalized_time(obj.getDateSampled,
606
                                               long_format=True)
607
            sampler = ""
608
        item["getDateSampled"] = datesampled
609
        item["getSampler"] = sampler
610
611
        # These don't exist on ARs
612
        # XXX This should be a list of preservers...
613
        item["getPreserver"] = ""
614
        item["getDatePreserved"] = ""
615
616
        # Assign the parent sample of this partition
617
        item["parent"] = obj.getRawParentAnalysisRequest
618
619
        # Assign the partitions of this sample
620
        if self.show_partitions:
621
            item["children"] = obj.getDescendantsUIDs or []
622
623
        return item
624
625
    def purge_review_states(self):
626
        """Purges unnecessary review statuses
627
        """
628
        remove_filters = []
629
        setup = api.get_bika_setup()
630
        if not setup.getSamplingWorkflowEnabled():
631
            remove_filters.append("to_be_sampled")
632
        if not setup.getScheduleSamplingEnabled():
633
            remove_filters.append("scheduled_sampling")
634
        if not setup.getSamplePreservationEnabled():
635
            remove_filters.append("to_be_preserved")
636
        if not setup.getRejectionReasons():
637
            remove_filters.append("rejected")
638
639
        self.review_states = filter(lambda r: r.get("id") not in remove_filters,
640
                                    self.review_states)
641
642
    def purge_columns(self):
643
        """Purges unnecessary columns
644
        """
645
        remove_columns = []
646
        if not self.is_printing_workflow_enabled:
647
            remove_columns.append("Printed")
648
649
        for rv in self.review_states:
650
            cols = rv.get("columns", [])
651
            rv["columns"] = filter(lambda c: c not in remove_columns, cols)
652
653
    def add_custom_transitions(self):
654
        """Adds custom transitions as required
655
        """
656
        custom_transitions = []
657
        if self.is_printing_workflow_enabled:
658
            custom_transitions.append({
659
                "id": "print_sample",
660
                "title": _("Print"),
661
                "url": "{}/workflow_action?action={}".format(
662
                    self.url, "print_sample")
663
            })
664
        if self.copy_to_new_allowed:
665
            custom_transitions.append({
666
                "id": "copy_to_new",
667
                "title": _("Copy to new"),
668
                "url": "{}/workflow_action?action={}".format(
669
                    self.url, "copy_to_new")
670
            })
671
672
        for rv in self.review_states:
673
            rv.setdefault("custom_transitions", []).extend(custom_transitions)
674
675
    @property
676
    def copy_to_new_allowed(self):
677
        mtool = api.get_tool("portal_membership")
678
        if mtool.checkPermission(AddAnalysisRequest, self.context):
679
            return True
680
        return False
681
682
    @property
683
    def is_printing_workflow_enabled(self):
684
        setup = api.get_setup()
685
        return setup.getPrintingWorkflowEnabled()
686
687
    def str_date(self, date, long_format=1, default=""):
688
        if not date:
689
            return default
690
        return self.ulocalized_time(date, long_format=long_format)
691
692
    def getDefaultAddCount(self):
693
        return self.context.bika_setup.getDefaultNumberOfARsToAdd()
694
695
    @property
696
    def show_partitions(self):
697
        if api.get_current_client():
698
            # If current user is a client contact, delegate to ShowPartitions
699
            return api.get_setup().getShowPartitions()
700
        return True
701