Passed
Pull Request — 2.x (#1958)
by Jordi
05:23
created

AnalysisRequestAddView.get_ar()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nop 1
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
import json
22
import six
23
24
from collections import OrderedDict
25
from datetime import datetime
26
27
from bika.lims import POINTS_OF_CAPTURE
28
from bika.lims import api
29
from bika.lims import bikaMessageFactory as _
30
from bika.lims import logger
31
from bika.lims.api.analysisservice import get_calculation_dependencies_for
32
from bika.lims.api.analysisservice import get_service_dependencies_for
33
from bika.lims.interfaces import IAddSampleConfirmation
34
from bika.lims.interfaces import IAddSampleFieldsFlush
35
from bika.lims.interfaces import IAddSampleObjectInfo
36
from bika.lims.interfaces import IAddSampleRecordsValidator
37
from bika.lims.interfaces import IGetDefaultFieldValueARAddHook
38
from bika.lims.utils import tmpID
39
from bika.lims.utils.analysisrequest import create_analysisrequest as crar
40
from bika.lims.workflow import ActionHandlerPool
41
from BTrees.OOBTree import OOBTree
42
from DateTime import DateTime
43
from plone import protect
44
from plone.memoize import view as viewcache
45
from plone.memoize.volatile import DontCache
46
from plone.memoize.volatile import cache
47
from plone.protect.interfaces import IDisableCSRFProtection
48
from Products.CMFPlone.utils import _createObjectByType
49
from Products.CMFPlone.utils import safe_unicode
50
from Products.Five.browser import BrowserView
51
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
52
from senaite.core.p3compat import cmp
53
from zope.annotation.interfaces import IAnnotations
54
from zope.component import getAdapters
55
from zope.component import queryAdapter
56
from zope.i18n.locales import locales
57
from zope.interface import alsoProvides
58
from zope.interface import implements
59
from zope.publisher.interfaces import IPublishTraverse
60
61
AR_CONFIGURATION_STORAGE = "bika.lims.browser.analysisrequest.manage.add"
62
SKIP_FIELD_ON_COPY = ["Sample", "PrimaryAnalysisRequest", "Remarks"]
63
64
65 View Code Duplication
def returns_json(func):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
66
    """Decorator for functions which return JSON
67
    """
68
    def decorator(*args, **kwargs):
69
        instance = args[0]
70
        request = getattr(instance, 'request', None)
71
        request.response.setHeader("Content-Type", "application/json")
72
        result = func(*args, **kwargs)
73
        return json.dumps(result)
74
    return decorator
75
76
77
def cache_key(method, self, obj):
78
    if obj is None:
79
        raise DontCache
80
    return api.get_cache_key(obj)
81
82
83
class AnalysisRequestAddView(BrowserView):
84
    """AR Add view
85
    """
86
    template = ViewPageTemplateFile("templates/ar_add2.pt")
87
88
    def __init__(self, context, request):
89
        super(AnalysisRequestAddView, self).__init__(context, request)
90
        # disable CSRF protection
91
        alsoProvides(request, IDisableCSRFProtection)
92
        self.request = request
93
        self.context = context
94
        self.fieldvalues = {}
95
        self.tmp_ar = None
96
97
    def __call__(self):
98
        self.portal = api.get_portal()
99
        self.portal_url = self.portal.absolute_url()
100
        self.setup = api.get_setup()
101
        self.request.set("disable_plone.rightcolumn", 1)
102
        self.came_from = "add"
103
        self.tmp_ar = self.get_ar()
104
        self.ar_count = self.get_ar_count()
105
        self.fieldvalues = self.generate_fieldvalues(self.ar_count)
106
        self.ShowPrices = self.setup.getShowPrices()
107
        self.theme = api.get_view("senaite_theme")
108
        self.icon = self.theme.icon_url("Sample")
109
        logger.info("*** Prepared data for {} ARs ***".format(self.ar_count))
110
        return self.template()
111
112
    def get_view_url(self):
113
        """Return the current view url including request parameters
114
        """
115
        request = self.request
116
        url = request.getURL()
117
        qs = request.getHeader("query_string")
118
        if not qs:
119
            return url
120
        return "{}?{}".format(url, qs)
121
122
    # N.B.: We are caching here persistent objects!
123
    #       It should be safe to do this but only on the view object,
124
    #       because it get recreated per request (transaction border).
125
    @viewcache.memoize
126
    def get_object_by_uid(self, uid):
127
        """Get the object by UID
128
        """
129
        logger.debug("get_object_by_uid::UID={}".format(uid))
130
        obj = api.get_object_by_uid(uid, None)
131
        if obj is None:
132
            logger.warn("!! No object found for UID #{} !!")
133
        return obj
134
135
    def get_currency(self):
136
        """Returns the configured currency
137
        """
138
        setup = api.get_setup()
139
        currency = setup.getCurrency()
140
        currencies = locales.getLocale('en').numbers.currencies
141
        return currencies[currency]
142
143
    def get_ar_count(self):
144
        """Return the ar_count request paramteter
145
        """
146
        ar_count = 1
147
        try:
148
            ar_count = int(self.request.form.get("ar_count", 1))
149
        except (TypeError, ValueError):
150
            ar_count = 1
151
        return ar_count
152
153
    def get_ar(self):
154
        """Create a temporary AR to fetch the fields from
155
        """
156
        if not self.tmp_ar:
157
            logger.info("*** CREATING TEMPORARY AR ***")
158
            self.tmp_ar = self.context.restrictedTraverse(
159
                "portal_factory/AnalysisRequest/Request new analyses")
160
        return self.tmp_ar
161
162
    def get_ar_schema(self):
163
        """Return the AR schema
164
        """
165
        logger.info("*** GET AR SCHEMA ***")
166
        ar = self.get_ar()
167
        return ar.Schema()
168
169
    def get_ar_fields(self):
170
        """Return the AR schema fields (including extendend fields)
171
        """
172
        logger.info("*** GET AR FIELDS ***")
173
        schema = self.get_ar_schema()
174
        return schema.fields()
175
176
    def get_fieldname(self, field, arnum):
177
        """Generate a new fieldname with a '-<arnum>' suffix
178
        """
179
        name = field.getName()
180
        # ensure we have only *one* suffix
181
        base_name = name.split("-")[0]
182
        suffix = "-{}".format(arnum)
183
        return "{}{}".format(base_name, suffix)
184
185
    def get_input_widget(self, fieldname, arnum=0, **kw):
186
        """Get the field widget of the AR in column <arnum>
187
188
        :param fieldname: The base fieldname
189
        :type fieldname: string
190
        """
191
192
        # temporary AR Context
193
        context = self.get_ar()
194
        # request = self.request
195
        schema = context.Schema()
196
197
        # get original field in the schema from the base_fieldname
198
        base_fieldname = fieldname.split("-")[0]
199
        field = context.getField(base_fieldname)
200
201
        # fieldname with -<arnum> suffix
202
        new_fieldname = self.get_fieldname(field, arnum)
203
        new_field = field.copy(name=new_fieldname)
204
205
        # get the default value for this field
206
        fieldvalues = self.fieldvalues
207
        field_value = fieldvalues.get(new_fieldname)
208
        # request_value = request.form.get(new_fieldname)
209
        # value = request_value or field_value
210
        value = field_value
211
212
        def getAccessor(instance):
213
            def accessor(**kw):
214
                return value
215
            return accessor
216
217
        # inject the new context for the widget renderer
218
        # see: Products.Archetypes.Renderer.render
219
        kw["here"] = context
220
        kw["context"] = context
221
        kw["fieldName"] = new_fieldname
222
223
        # make the field available with this name
224
        # XXX: This is a hack to make the widget available in the template
225
        schema._fields[new_fieldname] = new_field
226
        new_field.getAccessor = getAccessor
227
        new_field.getEditAccessor = getAccessor
228
229
        # set the default value
230
        form = dict()
231
        form[new_fieldname] = value
232
        self.request.form.update(form)
233
        logger.info("get_input_widget: fieldname={} arnum={} "
234
                    "-> new_fieldname={} value={}".format(
235
                        fieldname, arnum, new_fieldname, value))
236
        widget = context.widget(new_fieldname, **kw)
237
        return widget
238
239
    def get_copy_from(self):
240
        """Returns a mapping of UID index -> AR object
241
        """
242
        # Create a mapping of source ARs for copy
243
        copy_from = self.request.form.get("copy_from", "").split(",")
244
        # clean out empty strings
245
        copy_from_uids = filter(lambda x: x, copy_from)
246
        out = dict().fromkeys(range(len(copy_from_uids)))
247
        for n, uid in enumerate(copy_from_uids):
248
            ar = self.get_object_by_uid(uid)
249
            if ar is None:
250
                continue
251
            out[n] = ar
252
        logger.info("get_copy_from: uids={}".format(copy_from_uids))
253
        return out
254
255
    def get_default_value(self, field, context, arnum):
256
        """Get the default value of the field
257
        """
258
        name = field.getName()
259
        default = field.getDefault(context)
260
        if name == "Batch":
261
            batch = self.get_batch()
262
            if batch is not None:
263
                default = batch
264
        if name == "Client":
265
            client = self.get_client()
266
            if client is not None:
267
                default = client
268
        # only set default contact for first column
269
        if name == "Contact" and arnum == 0:
270
            contact = self.get_default_contact()
271
            if contact is not None:
272
                default = contact
273
        if name == "Sample":
274
            sample = self.get_sample()
275
            if sample is not None:
276
                default = sample
277
        # Querying for adapters to get default values from add-ons':
278
        # We don't know which fields the form will render since
279
        # some of them may come from add-ons. In order to obtain the default
280
        # value for those fields we take advantage of adapters. Adapters
281
        # registration should have the following format:
282
        # < adapter
283
        #   factory = ...
284
        #   for = "*"
285
        #   provides = "bika.lims.interfaces.IGetDefaultFieldValueARAddHook"
286
        #   name = "<fieldName>_default_value_hook"
287
        # / >
288
        hook_name = name + '_default_value_hook'
289
        adapter = queryAdapter(
290
            self.request,
291
            name=hook_name,
292
            interface=IGetDefaultFieldValueARAddHook)
293
        if adapter is not None:
294
            default = adapter(self.context)
295
        logger.info("get_default_value: context={} field={} value={} arnum={}"
296
                    .format(context, name, default, arnum))
297
        return default
298
299
    def get_field_value(self, field, context):
300
        """Get the stored value of the field
301
        """
302
        name = field.getName()
303
        value = context.getField(name).get(context)
304
        logger.info("get_field_value: context={} field={} value={}".format(
305
            context, name, value))
306
        return value
307
308
    def get_client(self):
309
        """Returns the Client
310
        """
311
        context = self.context
312
        parent = api.get_parent(context)
313
        if context.portal_type == "Client":
314
            return context
315
        elif parent.portal_type == "Client":
316
            return parent
317
        elif context.portal_type == "Batch":
318
            return context.getClient()
319
        elif parent.portal_type == "Batch":
320
            return context.getClient()
321
        return None
322
323
    def get_sample(self):
324
        """Returns the Sample
325
        """
326
        context = self.context
327
        if context.portal_type == "Sample":
328
            return context
329
        return None
330
331
    def get_batch(self):
332
        """Returns the Batch
333
        """
334
        context = self.context
335
        parent = api.get_parent(context)
336
        if context.portal_type == "Batch":
337
            return context
338
        elif parent.portal_type == "Batch":
339
            return parent
340
        return None
341
342
    def get_parent_ar(self, ar):
343
        """Returns the parent AR
344
        """
345
        parent = ar.getParentAnalysisRequest()
346
347
        # Return immediately if we have no parent
348
        if parent is None:
349
            return None
350
351
        # Walk back the chain until we reach the source AR
352
        while True:
353
            pparent = parent.getParentAnalysisRequest()
354
            if pparent is None:
355
                break
356
            # remember the new parent
357
            parent = pparent
358
359
        return parent
360
361
    def generate_fieldvalues(self, count=1):
362
        """Returns a mapping of '<fieldname>-<count>' to the default value
363
        of the field or the field value of the source AR
364
        """
365
        ar_context = self.get_ar()
366
367
        # mapping of UID index to AR objects {1: <AR1>, 2: <AR2> ...}
368
        copy_from = self.get_copy_from()
369
370
        out = {}
371
        # the original schema fields of an AR (including extended fields)
372
        fields = self.get_ar_fields()
373
374
        # generate fields for all requested ARs
375
        for arnum in range(count):
376
            source = copy_from.get(arnum)
377
            parent = None
378
            if source is not None:
379
                parent = self.get_parent_ar(source)
380
            for field in fields:
381
                value = None
382
                fieldname = field.getName()
383
                if source and fieldname not in SKIP_FIELD_ON_COPY:
384
                    # get the field value stored on the source
385
                    context = parent or source
386
                    value = self.get_field_value(field, context)
387
                else:
388
                    # get the default value of this field
389
                    value = self.get_default_value(
390
                        field, ar_context, arnum=arnum)
391
                # store the value on the new fieldname
392
                new_fieldname = self.get_fieldname(field, arnum)
393
                out[new_fieldname] = value
394
395
        return out
396
397
    def get_default_contact(self, client=None):
398
        """Logic refactored from JavaScript:
399
400
        * If client only has one contact, and the analysis request comes from
401
        * a client, then Auto-complete first Contact field.
402
        * If client only has one contect, and the analysis request comes from
403
        * a batch, then Auto-complete all Contact field.
404
405
        :returns: The default contact for the AR
406
        :rtype: Client object or None
407
        """
408
        catalog = api.get_tool("portal_catalog")
409
        client = client or self.get_client()
410
        path = api.get_path(self.context)
411
        if client:
412
            path = api.get_path(client)
413
        query = {
414
            "portal_type": "Contact",
415
            "path": {
416
                "query": path,
417
                "depth": 1
418
            },
419
            "is_active": True,
420
        }
421
        contacts = catalog(query)
422
        if len(contacts) == 1:
423
            return api.get_object(contacts[0])
424
        elif client == api.get_current_client():
425
            # Current user is a Client contact. Use current contact
426
            current_user = api.get_current_user()
427
            return api.get_user_contact(current_user, contact_types=["Contact"])
428
429
        return None
430
431
    def getMemberDiscountApplies(self):
432
        """Return if the member discount applies for this client
433
434
        :returns: True if member discount applies for the client
435
        :rtype: bool
436
        """
437
        client = self.get_client()
438
        if client is None:
439
            return False
440
        return client.getMemberDiscountApplies()
441
442
    def is_field_visible(self, field):
443
        """Check if the field is visible
444
        """
445
        context = self.context
446
        fieldname = field.getName()
447
448
        # hide the Client field on client and batch contexts
449
        if fieldname == "Client" and context.portal_type in ("Client", ):
450
            return False
451
452
        # hide the Batch field on batch contexts
453
        if fieldname == "Batch" and context.portal_type in ("Batch", ):
454
            return False
455
456
        return True
457
458
    def get_fields_with_visibility(self, visibility, mode="add"):
459
        """Return the AR fields with the current visibility
460
        """
461
        ar = self.get_ar()
462
        mv = api.get_view("ar_add_manage", context=ar)
463
        mv.get_field_order()
464
465
        out = []
466
        for field in mv.get_fields_with_visibility(visibility, mode):
467
            # check custom field condition
468
            visible = self.is_field_visible(field)
469
            if visible is False and visibility != "hidden":
470
                continue
471
            out.append(field)
472
        return out
473
474
    def get_service_categories(self, restricted=True):
475
        """Return all service categories in the right order
476
477
        :param restricted: Client settings restrict categories
478
        :type restricted: bool
479
        :returns: Category catalog results
480
        :rtype: brains
481
        """
482
        bsc = api.get_tool("senaite_catalog_setup")
483
        query = {
484
            "portal_type": "AnalysisCategory",
485
            "is_active": True,
486
            "sort_on": "sortable_title",
487
        }
488
        categories = bsc(query)
489
        client = self.get_client()
490
        if client and restricted:
491
            restricted_categories = client.getRestrictedCategories()
492
            restricted_category_ids = map(
493
                lambda c: c.getId(), restricted_categories)
494
            # keep correct order of categories
495
            if restricted_category_ids:
496
                categories = filter(
497
                    lambda c: c.getId in restricted_category_ids, categories)
0 ignored issues
show
introduced by
The variable restricted_category_ids does not seem to be defined in case client and restricted on line 490 is False. Are you sure this can never be the case?
Loading history...
498
        return categories
499
500
    def get_points_of_capture(self):
501
        items = POINTS_OF_CAPTURE.items()
502
        return OrderedDict(items)
503
504
    def get_services(self, poc="lab"):
505
        """Return all Services
506
507
        :param poc: Point of capture (lab/field)
508
        :type poc: string
509
        :returns: Mapping of category -> list of services
510
        :rtype: dict
511
        """
512
        bsc = api.get_tool("senaite_catalog_setup")
513
        query = {
514
            "portal_type": "AnalysisService",
515
            "point_of_capture": poc,
516
            "is_active": True,
517
            "sort_on": "sortable_title",
518
        }
519
        services = bsc(query)
520
        categories = self.get_service_categories(restricted=False)
521
        analyses = {key: [] for key in map(lambda c: c.Title, categories)}
522
523
        # append the empty category as well
524
        analyses[""] = []
525
526
        for brain in services:
527
            category = brain.getCategoryTitle
528
            if category in analyses:
529
                analyses[category].append(brain)
530
        return analyses
531
532
    @cache(cache_key)
533
    def get_service_uid_from(self, analysis):
534
        """Return the service from the analysis
535
        """
536
        analysis = api.get_object(analysis)
537
        return api.get_uid(analysis.getAnalysisService())
538
539
    def is_service_selected(self, service):
540
        """Checks if the given service is selected by one of the ARs.
541
        This is used to make the whole line visible or not.
542
        """
543
        service_uid = api.get_uid(service)
544
        for arnum in range(self.ar_count):
545
            analyses = self.fieldvalues.get("Analyses-{}".format(arnum))
546
            if not analyses:
547
                continue
548
            service_uids = map(self.get_service_uid_from, analyses)
549
            if service_uid in service_uids:
550
                return True
551
        return False
552
553
554
class AnalysisRequestManageView(BrowserView):
555
    """AR Manage View
556
    """
557
    template = ViewPageTemplateFile("templates/ar_add_manage.pt")
558
559
    def __init__(self, context, request):
560
        # disable CSRF protection
561
        alsoProvides(request, IDisableCSRFProtection)
562
        self.context = context
563
        self.request = request
564
        self.tmp_ar = None
565
566
    def __call__(self):
567
        protect.CheckAuthenticator(self.request.form)
568
        form = self.request.form
569
        if form.get("submitted", False) and form.get("save", False):
570
            order = form.get("order")
571
            self.set_field_order(order)
572
            visibility = form.get("visibility")
573
            self.set_field_visibility(visibility)
574
        if form.get("submitted", False) and form.get("reset", False):
575
            self.flush()
576
        return self.template()
577
578
    def get_ar(self):
579
        if not self.tmp_ar:
580
            self.tmp_ar = self.context.restrictedTraverse(
581
                "portal_factory/AnalysisRequest/Request new analyses")
582
        return self.tmp_ar
583
584
    def get_annotation(self):
585
        setup = api.get_setup()
586
        return IAnnotations(setup)
587
588
    @property
589
    def storage(self):
590
        annotation = self.get_annotation()
591
        if annotation.get(AR_CONFIGURATION_STORAGE) is None:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable AR_CONFIGURATION_STORAGE does not seem to be defined.
Loading history...
592
            annotation[AR_CONFIGURATION_STORAGE] = OOBTree()
593
        return annotation[AR_CONFIGURATION_STORAGE]
594
595
    def flush(self):
596
        annotation = self.get_annotation()
597
        if annotation.get(AR_CONFIGURATION_STORAGE) is not None:
598
            del annotation[AR_CONFIGURATION_STORAGE]
599
600
    def set_field_order(self, order):
601
        self.storage.update({"order": order})
602
603
    def get_field_order(self):
604
        order = self.storage.get("order")
605
        if order is None:
606
            return map(lambda f: f.getName(), self.get_fields())
607
        return order
608
609
    def set_field_visibility(self, visibility):
610
        self.storage.update({"visibility": visibility})
611
612
    def get_field_visibility(self):
613
        return self.storage.get("visibility")
614
615
    def is_field_visible(self, field):
616
        if field.required:
617
            return True
618
        visibility = self.get_field_visibility()
619
        if visibility is None:
620
            return True
621
        return visibility.get(field.getName(), True)
622
623
    def get_field(self, name):
624
        """Get AR field by name
625
        """
626
        ar = self.get_ar()
627
        return ar.getField(name)
628
629
    def get_fields(self):
630
        """Return all AR fields
631
        """
632
        ar = self.get_ar()
633
        return ar.Schema().fields()
634
635
    def get_sorted_fields(self):
636
        """Return the sorted fields
637
        """
638
        inf = float("inf")
639
        order = self.get_field_order()
640
641
        def field_cmp(field1, field2):
642
            _n1 = field1.getName()
643
            _n2 = field2.getName()
644
            _i1 = _n1 in order and order.index(_n1) + 1 or inf
645
            _i2 = _n2 in order and order.index(_n2) + 1 or inf
646
            return cmp(_i1, _i2)
647
648
        return sorted(self.get_fields(), cmp=field_cmp)
649
650
    def get_fields_with_visibility(self, visibility="edit", mode="add"):
651
        """Return the fields with visibility
652
        """
653
        fields = self.get_sorted_fields()
654
655
        out = []
656
657
        for field in fields:
658
            v = field.widget.isVisible(
659
                self.context, mode, default='invisible', field=field)
660
661
            if self.is_field_visible(field) is False:
662
                v = "hidden"
663
664
            visibility_guard = True
665
            # visibility_guard is a widget field defined in the schema in order
666
            # to know the visibility of the widget when the field is related to
667
            # a dynamically changing content such as workflows. For instance
668
            # those fields related to the workflow will be displayed only if
669
            # the workflow is enabled, otherwise they should not be shown.
670
            if 'visibility_guard' in dir(field.widget):
671
                visibility_guard = eval(field.widget.visibility_guard)
672
            if v == visibility and visibility_guard:
673
                out.append(field)
674
675
        return out
676
677
678
class ajaxAnalysisRequestAddView(AnalysisRequestAddView):
679
    """Ajax helpers for the analysis request add form
680
    """
681
    implements(IPublishTraverse)
682
683
    def __init__(self, context, request):
684
        super(ajaxAnalysisRequestAddView, self).__init__(context, request)
685
        self.context = context
686
        self.request = request
687
        self.traverse_subpath = []
688
        # Errors are aggregated here, and returned together to the browser
689
        self.errors = {}
690
691
    def publishTraverse(self, request, name):
692
        """ get's called before __call__ for each path name
693
        """
694
        self.traverse_subpath.append(name)
695
        return self
696
697
    @returns_json
698
    def __call__(self):
699
        """Dispatch the path to a method and return JSON.
700
        """
701
        protect.CheckAuthenticator(self.request.form)
702
        protect.PostOnly(self.request.form)
703
704
        if len(self.traverse_subpath) != 1:
705
            return self.error("Not found", status=404)
706
        func_name = "ajax_{}".format(self.traverse_subpath[0])
707
        func = getattr(self, func_name, None)
708
        if func is None:
709
            return self.error("Invalid function", status=400)
710
        return func()
711
712
    def error(self, message, status=500, **kw):
713
        """Set a JSON error object and a status to the response
714
        """
715
        self.request.response.setStatus(status)
716
        result = {"success": False, "errors": message}
717
        result.update(kw)
718
        return result
719
720
    def to_iso_date(self, dt):
721
        """Return the ISO representation of a date object
722
        """
723
        if dt is None:
724
            return ""
725
        if isinstance(dt, DateTime):
726
            return dt.ISO8601()
727
        if isinstance(dt, datetime):
728
            return dt.isoformat()
729
        raise TypeError("{} is neiter an instance of DateTime nor datetime"
730
                        .format(repr(dt)))
731
732
    def get_records(self):
733
        """Returns a list of AR records
734
735
        Fields coming from `request.form` have a number prefix, e.g. Contact-0.
736
        Fields with the same suffix number are grouped together in a record.
737
        Each record represents the data for one column in the AR Add form and
738
        contains a mapping of the fieldName (w/o prefix) -> value.
739
740
        Example:
741
        [{"Contact": "Rita Mohale", ...}, {Contact: "Neil Standard"} ...]
742
        """
743
        form = self.request.form
744
        ar_count = self.get_ar_count()
745
746
        records = []
747
        # Group belonging AR fields together
748
        for arnum in range(ar_count):
749
            record = {}
750
            s1 = "-{}".format(arnum)
751
            keys = filter(lambda key: s1 in key, form.keys())
0 ignored issues
show
introduced by
The variable s1 does not seem to be defined in case the for loop on line 748 is not entered. Are you sure this can never be the case?
Loading history...
752
            for key in keys:
753
                new_key = key.replace(s1, "")
754
                value = form.get(key)
755
                record[new_key] = value
756
            records.append(record)
757
        return records
758
759
    def get_uids_from_record(self, record, key):
760
        """Returns a list of parsed UIDs from a single form field identified by
761
        the given key.
762
763
        A form field ending with `_uid` can contain an empty value, a
764
        single UID or multiple UIDs separated by a comma.
765
766
        This method parses the UID value and returns a list of non-empty UIDs.
767
        """
768
        value = record.get(key, None)
769
        if value is None:
770
            return []
771
        if isinstance(value, six.string_types):
772
            value = value.split(",")
773
        return filter(lambda uid: uid, value)
774
775
    @cache(cache_key)
776
    def get_base_info(self, obj):
777
        """Returns the base info of an object
778
        """
779
        if obj is None:
780
            return {}
781
782
        info = {
783
            "id": api.get_id(obj),
784
            "uid": api.get_uid(obj),
785
            "title": api.get_title(obj),
786
            "field_values": {},
787
            "filter_queries": {},
788
        }
789
790
        return info
791
792
    @cache(cache_key)
793
    def get_client_info(self, obj):
794
        """Returns the client info of an object
795
        """
796
        info = self.get_base_info(obj)
797
798
        # Set the default contact, but only if empty. The Contact field is
799
        # flushed each time the Client changes, so we can assume that if there
800
        # is a selected contact, it belongs to current client already
801
        default_contact = self.get_default_contact(client=obj)
802
        if default_contact:
803
            contact_info = self.get_contact_info(default_contact)
804
            contact_info.update({"if_empty": True})
805
            info["field_values"].update({
806
                "Contact": contact_info
807
            })
808
809
        # Set default CC Email field
810
        info["field_values"].update({
811
            "CCEmails": {"value": obj.getCCEmails(), "if_empty": True}
812
        })
813
814
        # UID of the client
815
        uid = api.get_uid(obj)
816
817
        # catalog queries for UI field filtering
818
        filter_queries = {
819
            "Contact": {
820
                "path": {
821
                    "query": api.get_path(obj),
822
                    "level": 0
823
                }
824
            },
825
            "CCContact": {
826
                "path": {
827
                    "query": api.get_path(obj),
828
                    "level": 0
829
                }
830
            },
831
            "InvoiceContact": {
832
                "getParentUID": [uid]
833
            },
834
            "SamplePoint": {
835
                "getClientUID": [uid, ""],
836
            },
837
            "Template": {
838
                "getClientUID": [uid, ""],
839
            },
840
            "Profiles": {
841
                "getClientUID": [uid, ""],
842
            },
843
            "Specification": {
844
                "getClientUID": [uid, ""],
845
            },
846
            "Sample": {
847
                "getClientUID": [uid],
848
            },
849
            "Batch": {
850
                "getClientUID": [uid, ""],
851
            }
852
        }
853
        info["filter_queries"] = filter_queries
854
        return info
855
856
    @cache(cache_key)
857
    def get_contact_info(self, obj):
858
        """Returns the client info of an object
859
        """
860
861
        info = self.get_base_info(obj)
862
        fullname = obj.getFullname()
863
        email = obj.getEmailAddress()
864
865
        # Note: It might get a circular dependency when calling:
866
        #       map(self.get_contact_info, obj.getCCContact())
867
        cccontacts = []
868
        for contact in obj.getCCContacts(as_objects=True):
869
            uid = api.get_uid(contact)
870
            fullname = contact.getFullname()
871
            email = contact.getEmail()
872
            cccontacts.append({
873
                "uid": uid,
874
                "title": fullname,
875
                "fullname": fullname,
876
                "email": email
877
            })
878
879
        info.update({
880
            "fullname": fullname,
881
            "email": email,
882
            "field_values": {
883
                "CCContact": cccontacts
884
            },
885
        })
886
887
        return info
888
889
    @cache(cache_key)
890
    def get_service_info(self, obj):
891
        """Returns the info for a Service
892
        """
893
        info = self.get_base_info(obj)
894
895
        info.update({
896
            "short_title": obj.getShortTitle(),
897
            "scientific_name": obj.getScientificName(),
898
            "unit": obj.getUnit(),
899
            "keyword": obj.getKeyword(),
900
            "methods": map(self.get_method_info, obj.getMethods()),
901
            "calculation": self.get_calculation_info(obj.getCalculation()),
902
            "price": obj.getPrice(),
903
            "currency_symbol": self.get_currency().symbol,
904
            "accredited": obj.getAccredited(),
905
            "category": obj.getCategoryTitle(),
906
            "poc": obj.getPointOfCapture(),
907
            "conditions": self.get_conditions_info(obj),
908
        })
909
910
        dependencies = get_calculation_dependencies_for(obj).values()
911
        info["dependencies"] = map(self.get_base_info, dependencies)
912
        return info
913
914
    @cache(cache_key)
915
    def get_template_info(self, obj):
916
        """Returns the info for a Template
917
        """
918
        client = self.get_client()
919
        client_uid = api.get_uid(client) if client else ""
920
921
        profile = obj.getAnalysisProfile()
922
        profile_uid = api.get_uid(profile) if profile else ""
923
        profile_title = profile.Title() if profile else ""
924
925
        sample_type = obj.getSampleType()
926
        sample_type_uid = api.get_uid(sample_type) if sample_type else ""
927
        sample_type_title = sample_type.Title() if sample_type else ""
928
929
        sample_point = obj.getSamplePoint()
930
        sample_point_uid = api.get_uid(sample_point) if sample_point else ""
931
        sample_point_title = sample_point.Title() if sample_point else ""
932
933
        service_uids = []
934
        analyses_partitions = {}
935
        analyses = obj.getAnalyses()
936
937
        for record in analyses:
938
            service_uid = record.get("service_uid")
939
            service_uids.append(service_uid)
940
            analyses_partitions[service_uid] = record.get("partition")
941
942
        info = self.get_base_info(obj)
943
        info.update({
944
            "analyses_partitions": analyses_partitions,
945
            "analysis_profile_title": profile_title,
946
            "analysis_profile_uid": profile_uid,
947
            "client_uid": client_uid,
948
            "composite": obj.getComposite(),
949
            "partitions": obj.getPartitions(),
950
            "remarks": obj.getRemarks(),
951
            "sample_point_title": sample_point_title,
952
            "sample_point_uid": sample_point_uid,
953
            "sample_type_title": sample_type_title,
954
            "sample_type_uid": sample_type_uid,
955
            "service_uids": service_uids,
956
        })
957
        return info
958
959
    @cache(cache_key)
960
    def get_profile_info(self, obj):
961
        """Returns the info for a Profile
962
        """
963
        info = self.get_base_info(obj)
964
        info.update({})
965
        return info
966
967
    @cache(cache_key)
968
    def get_method_info(self, obj):
969
        """Returns the info for a Method
970
        """
971
        info = self.get_base_info(obj)
972
        info.update({})
973
        return info
974
975
    @cache(cache_key)
976
    def get_calculation_info(self, obj):
977
        """Returns the info for a Calculation
978
        """
979
        info = self.get_base_info(obj)
980
        info.update({})
981
        return info
982
983
    @cache(cache_key)
984
    def get_sampletype_info(self, obj):
985
        """Returns the info for a Sample Type
986
        """
987
        info = self.get_base_info(obj)
988
989
        # client
990
        client = self.get_client()
991
        client_uid = client and api.get_uid(client) or ""
992
993
        info.update({
994
            "prefix": obj.getPrefix(),
995
            "minimum_volume": obj.getMinimumVolume(),
996
            "hazardous": obj.getHazardous(),
997
            "retention_period": obj.getRetentionPeriod(),
998
        })
999
1000
        # catalog queries for UI field filtering
1001
        sample_type_uid = api.get_uid(obj)
1002
        filter_queries = {
1003
            # Display Sample Points that have this sample type assigned plus
1004
            # those that do not have a sample type assigned
1005
            "SamplePoint": {
1006
                "sampletype_uid": [sample_type_uid, None],
1007
                "getClientUID": [client_uid, ""],
1008
            },
1009
            # Display Specifications that have this sample type assigned only
1010
            "Specification": {
1011
                "sampletype_uid": sample_type_uid,
1012
                "getClientUID": [client_uid, ""],
1013
            },
1014
            # Display AR Templates that have this sample type assigned plus
1015
            # those that do not have a sample type assigned
1016
            "Template": {
1017
                "sampletype_uid": [sample_type_uid, None],
1018
                "getClientUID": [client_uid, ""],
1019
            }
1020
        }
1021
        info["filter_queries"] = filter_queries
1022
1023
        return info
1024
1025
    @cache(cache_key)
1026
    def get_primaryanalysisrequest_info(self, obj):
1027
        """Returns the info for a Primary Sample
1028
        """
1029
        info = self.get_base_info(obj)
1030
1031
        batch = obj.getBatch()
1032
        client = obj.getClient()
1033
        sample_type = obj.getSampleType()
1034
        sample_condition = obj.getSampleCondition()
1035
        storage_location = obj.getStorageLocation()
1036
        sample_point = obj.getSamplePoint()
1037
        container = obj.getContainer()
1038
        deviation = obj.getSamplingDeviation()
1039
        cccontacts = obj.getCCContact() or []
1040
        contact = obj.getContact()
1041
1042
        info.update({
1043
            "composite": obj.getComposite(),
1044
        })
1045
1046
        # Set the fields for which we want the value to be set automatically
1047
        # when the primary sample is selected
1048
        info["field_values"].update({
1049
            "Client": self.to_field_value(client),
1050
            "Contact": self.to_field_value(contact),
1051
            "CCContact": map(self.to_field_value, cccontacts),
1052
            "CCEmails": obj.getCCEmails(),
1053
            "Batch": self.to_field_value(batch),
1054
            "DateSampled": {"value": self.to_iso_date(obj.getDateSampled())},
1055
            "SamplingDate": {"value": self.to_iso_date(obj.getSamplingDate())},
1056
            "SampleType": self.to_field_value(sample_type),
1057
            "EnvironmentalConditions": {"value": obj.getEnvironmentalConditions()},
1058
            "ClientSampleID": {"value": obj.getClientSampleID()},
1059
            "ClientReference": {"value": obj.getClientReference()},
1060
            "ClientOrderNumber": {"value": obj.getClientOrderNumber()},
1061
            "SampleCondition": self.to_field_value(sample_condition),
1062
            "SamplePoint": self.to_field_value(sample_point),
1063
            "StorageLocation": self.to_field_value(storage_location),
1064
            "Container": self.to_field_value(container),
1065
            "SamplingDeviation": self.to_field_value(deviation),
1066
            "Composite": {"value": obj.getComposite()}
1067
        })
1068
1069
        return info
1070
1071
    @cache(cache_key)
1072
    def get_conditions_info(self, obj):
1073
        conditions = obj.getConditions()
1074
        for condition in conditions:
1075
            choices = condition.get("choices", "")
1076
            options = filter(None, choices.split('|'))
1077
            if options:
1078
                condition.update({"options": options})
1079
        return conditions
1080
1081
    @cache(cache_key)
1082
    def to_field_value(self, obj):
1083
        return {
1084
            "uid": obj and api.get_uid(obj) or "",
1085
            "title": obj and api.get_title(obj) or ""
1086
        }
1087
1088
    def ajax_get_global_settings(self):
1089
        """Returns the global Bika settings
1090
        """
1091
        setup = api.get_setup()
1092
        settings = {
1093
            "show_prices": setup.getShowPrices(),
1094
        }
1095
        return settings
1096
1097
    def ajax_get_flush_settings(self):
1098
        """Returns the settings for fields flush
1099
        """
1100
        flush_settings = {
1101
            "Client": [
1102
                "Contact",
1103
                "CCContact",
1104
                "InvoiceContact",
1105
                "SamplePoint",
1106
                "Template",
1107
                "Profiles",
1108
                "PrimaryAnalysisRequest",
1109
                "Specification",
1110
                "Batch"
1111
            ],
1112
            "Contact": [
1113
                "CCContact"
1114
            ],
1115
            "SampleType": [
1116
                "SamplePoint",
1117
                "Specification",
1118
                "Template",
1119
            ],
1120
            "PrimarySample": [
1121
                "Batch"
1122
                "Client",
1123
                "Contact",
1124
                "CCContact",
1125
                "CCEmails",
1126
                "ClientOrderNumber",
1127
                "ClientReference",
1128
                "ClientSampleID",
1129
                "ContainerType",
1130
                "DateSampled",
1131
                "EnvironmentalConditions",
1132
                "InvoiceContact",
1133
                "Preservation",
1134
                "Profiles",
1135
                "SampleCondition",
1136
                "SamplePoint",
1137
                "SampleType",
1138
                "SamplingDate",
1139
                "SamplingDeviation",
1140
                "StorageLocation",
1141
                "Specification",
1142
                "Template",
1143
            ]
1144
        }
1145
1146
        # Maybe other add-ons have additional fields that require flushing
1147
        for name, ad in getAdapters((self.context,), IAddSampleFieldsFlush):
1148
            logger.info("Additional flush settings from {}".format(name))
1149
            additional_settings = ad.get_flush_settings()
1150
            for key, values in additional_settings.items():
1151
                new_values = flush_settings.get(key, []) + values
1152
                flush_settings[key] = list(set(new_values))
1153
1154
        return flush_settings
1155
1156
    def ajax_get_service(self):
1157
        """Returns the services information
1158
        """
1159
        uid = self.request.form.get("uid", None)
1160
1161
        if uid is None:
1162
            return self.error("Invalid UID", status=400)
1163
1164
        service = self.get_object_by_uid(uid)
1165
        if not service:
1166
            return self.error("Service not found", status=404)
1167
1168
        info = self.get_service_info(service)
1169
        return info
1170
1171
    def ajax_recalculate_records(self):
1172
        out = {}
1173
        records = self.get_records()
1174
        for num_sample, record in enumerate(records):
1175
            # Get reference fields metadata
1176
            metadata = self.get_record_metadata(record)
1177
1178
            # service_to_templates, template_to_services
1179
            templates_additional = self.get_template_additional_info(metadata)
1180
            metadata.update(templates_additional)
1181
1182
            # service_to_profiles, profiles_to_services
1183
            profiles_additional = self.get_profiles_additional_info(metadata)
1184
            metadata.update(profiles_additional)
1185
1186
            # dependencies
1187
            dependencies = self.get_unmet_dependencies_info(metadata)
1188
            metadata.update(dependencies)
1189
1190
            # Set the metadata for current sample number (column)
1191
            out[num_sample] = metadata
1192
1193
        return out
1194
1195
    def get_record_metadata(self, record):
1196
        """Returns the metadata for the record passed in
1197
        """
1198
        metadata = {}
1199
        extra_fields = {}
1200
        for key, value in record.items():
1201
            if not key.endswith("_uid"):
1202
                continue
1203
1204
            # This is a reference field (ends with _uid), so we add the
1205
            # metadata key, even if there is no way to handle objects this
1206
            # field refers to
1207
            metadata_key = key.replace("_uid", "")
1208
            metadata_key = "{}_metadata".format(metadata_key.lower())
1209
            metadata[metadata_key] = {}
1210
1211
            if not value:
1212
                continue
1213
1214
            # Get objects information (metadata)
1215
            objs_info = self.get_objects_info(record, key)
1216
            objs_uids = map(lambda obj: obj["uid"], objs_info)
1217
            metadata[metadata_key] = dict(zip(objs_uids, objs_info))
1218
1219
            # Grab 'field_values' fields to be recalculated too
1220
            for obj_info in objs_info:
1221
                field_values = obj_info.get("field_values", {})
1222
                for field_name, field_value in field_values.items():
1223
                    if not isinstance(field_value, dict):
1224
                        # this is probably a list, e.g. "Profiles" field
1225
                        continue
1226
                    uids = self.get_uids_from_record(field_value, "uid")
1227
                    if len(uids) == 1:
1228
                        extra_fields[field_name] = uids[0]
1229
1230
        # Populate metadata with object info from extra fields (hidden fields)
1231
        for field_name, uid in extra_fields.items():
1232
            key = "{}_metadata".format(field_name.lower())
1233
            if metadata.get(key):
1234
                # This object has been processed already, skip
1235
                continue
1236
            obj = self.get_object_by_uid(uid)
1237
            if not obj:
1238
                continue
1239
            obj_info = self.get_object_info(obj, field_name, record=extra_fields)
1240
            if not obj_info or "uid" not in obj_info:
1241
                continue
1242
            metadata[key] = {obj_info["uid"]: obj_info}
1243
1244
        return metadata
1245
1246
    def get_template_additional_info(self, metadata):
1247
        template_to_services = {}
1248
        service_to_templates = {}
1249
        service_metadata = metadata.get("service_metadata", {})
1250
        profiles_metadata = metadata.get("profiles_metadata", {})
1251
        template = metadata.get("template_metadata", {})
1252
        # We don't expect more than one template, but who knows about future?
1253
        for uid, obj_info in template.items():
1254
            obj = self.get_object_by_uid(uid)
1255
            # profile from the template
1256
            profile = obj.getAnalysisProfile()
1257
            # add the profile to the other profiles
1258
            if profile is not None:
1259
                profile_uid = api.get_uid(profile)
1260
                if profile_uid not in profiles_metadata:
1261
                    profile = self.get_object_by_uid(profile_uid)
1262
                    profile_info = self.get_profile_info(profile)
1263
                    profiles_metadata[profile_uid] = profile_info
1264
1265
            # get the template analyses
1266
            # [{'partition': 'part-1', 'service_uid': '...'},
1267
            # {'partition': 'part-1', 'service_uid': '...'}]
1268
            analyses = obj.getAnalyses() or []
1269
            # get all UIDs of the template records
1270
            service_uids = map(lambda rec: rec.get("service_uid"), analyses)
1271
            # remember a mapping of template uid -> service
1272
            template_to_services[uid] = service_uids
1273
            # remember a mapping of service uid -> templates
1274
            for service_uid in service_uids:
1275
                # remember the template of all services
1276
                if service_uid in service_to_templates:
1277
                    service_to_templates[service_uid].append(uid)
1278
                else:
1279
                    service_to_templates[service_uid] = [uid]
1280
                # remember the service metadata
1281
                if service_uid not in service_metadata:
1282
                    service = self.get_object_by_uid(service_uid)
1283
                    service_info = self.get_service_info(service)
1284
                    service_metadata[service_uid] = service_info
1285
1286
        return {
1287
            "service_to_templates": service_to_templates,
1288
            "template_to_services": template_to_services,
1289
            "service_metadata": service_metadata,
1290
            "profiles_metadata": profiles_metadata,
1291
        }
1292
1293
    def get_profiles_additional_info(self, metadata):
1294
        profile_to_services = {}
1295
        service_to_profiles = metadata.get("service_to_profiles", {})
1296
        service_metadata = metadata.get("service_metadata", {})
1297
        profiles = metadata.get("profiles_metadata", {})
1298
        for uid, obj_info in profiles.items():
1299
            obj = self.get_object_by_uid(uid)
1300
            # get all services of this profile
1301
            services = obj.getService()
1302
            # get all UIDs of the profile services
1303
            service_uids = map(api.get_uid, services)
1304
            # remember all services of this profile
1305
            profile_to_services[uid] = service_uids
1306
            # remember a mapping of service uid -> profiles
1307
            for service in services:
1308
                # get the UID of this service
1309
                service_uid = api.get_uid(service)
1310
                # remember the profiles of this service
1311
                if service_uid in service_to_profiles:
1312
                    service_to_profiles[service_uid].append(uid)
1313
                else:
1314
                    service_to_profiles[service_uid] = [uid]
1315
                # remember the service metadata
1316
                if service_uid not in service_metadata:
1317
                    service_info = self.get_service_info(service)
1318
                    service_metadata[service_uid] = service_info
1319
1320
        return {
1321
            "profile_to_services": profile_to_services,
1322
            "service_to_profiles": service_to_profiles,
1323
            "service_metadata": service_metadata,
1324
        }
1325
1326
    def get_unmet_dependencies_info(self, metadata):
1327
        # mapping of service UID -> unmet service dependency UIDs
1328
        unmet_dependencies = {}
1329
        services = metadata.get("service_metadata", {}).copy()
1330
        for uid, obj_info in services.items():
1331
            obj = self.get_object_by_uid(uid)
1332
            # get the dependencies of this service
1333
            deps = get_service_dependencies_for(obj)
1334
1335
            # check for unmet dependencies
1336
            for dep in deps["dependencies"]:
1337
                # we use the UID to test for equality
1338
                dep_uid = api.get_uid(dep)
1339
                if dep_uid not in services:
1340
                    if uid in unmet_dependencies:
1341
                        unmet_dependencies[uid].append(self.get_base_info(dep))
1342
                    else:
1343
                        unmet_dependencies[uid] = [self.get_base_info(dep)]
1344
            # remember the dependencies in the service metadata
1345
            metadata["service_metadata"][uid].update({
1346
                "dependencies": map(
1347
                    self.get_base_info, deps["dependencies"]),
1348
            })
1349
        return {
1350
            "unmet_dependencies": unmet_dependencies
1351
        }
1352
1353
    def get_objects_info(self, record, key):
1354
        """
1355
        Returns a list with the metadata for the objects the field with
1356
        field_name passed in refers to. Returns empty list if the field is not
1357
        a reference field or the record for this key cannot be handled
1358
        :param record: a record for a single sample (column)
1359
        :param key: The key of the field from the record (e.g. Client_uid)
1360
        :return: list of info objects
1361
        """
1362
        # Get the objects from this record. Returns a list because the field
1363
        # can be multivalued
1364
        uids = self.get_uids_from_record(record, key)
1365
        objects = map(self.get_object_by_uid, uids)
1366
        objects = map(lambda obj: self.get_object_info(
1367
            obj, key, record=record), objects)
1368
        return filter(None, objects)
1369
1370
    def object_info_cache_key(method, self, obj, key, **kw):
1371
        if obj is None or not key:
1372
            raise DontCache
1373
        field_name = key.replace("_uid", "").lower()
1374
        obj_key = api.get_cache_key(obj)
1375
        return "-".join([field_name, obj_key] + kw.keys())
1376
1377
    @cache(object_info_cache_key)
1378
    def get_object_info(self, obj, key, record=None):
1379
        """Returns the object info metadata for the passed in object and key
1380
        :param obj: the object from which extract the info from
1381
        :param key: The key of the field from the record (e.g. Client_uid)
1382
        :return: dict that represents the object
1383
        """
1384
        # Check if there is a function to handle objects for this field
1385
        field_name = key.replace("_uid", "")
1386
        func_name = "get_{}_info".format(field_name.lower())
1387
        func = getattr(self, func_name, None)
1388
1389
        # always ensure we have a record
1390
        if record is None:
1391
            record = {}
1392
1393
        # Get the info for each object
1394
        info = callable(func) and func(obj) or self.get_base_info(obj)
1395
1396
        # Check if there is any adapter to handle objects for this field
1397
        for name, adapter in getAdapters((obj, ), IAddSampleObjectInfo):
1398
            logger.info("adapter for '{}': {}".format(field_name, name))
1399
            ad_info = adapter.get_object_info_with_record(record)
1400
            self.update_object_info(info, ad_info)
1401
1402
        return info
1403
1404
    def update_object_info(self, base_info, additional_info):
1405
        """Updates the dictionaries for keys 'field_values' and 'filter_queries'
1406
        from base_info with those defined in additional_info. If base_info is
1407
        empty or None, updates the whole base_info dict with additional_info
1408
        """
1409
        if not base_info:
1410
            base_info.update(additional_info)
1411
            return
1412
1413
        # Merge field_values info
1414
        field_values = base_info.get("field_values", {})
1415
        field_values.update(additional_info.get("field_values", {}))
1416
        base_info["field_values"] = field_values
1417
1418
        # Merge filter_queries info
1419
        filter_queries = base_info.get("filter_queries", {})
1420
        filter_queries.update(additional_info.get("filter_queries", {}))
1421
        base_info["filter_queries"] = filter_queries
1422
1423
    def show_recalculate_prices(self):
1424
        setup = api.get_setup()
1425
        return setup.getShowPrices()
1426
1427
    def ajax_recalculate_prices(self):
1428
        """Recalculate prices for all ARs
1429
        """
1430
        # When the option "Include and display pricing information" in
1431
        # Bika Setup Accounting tab is not selected
1432
        if not self.show_recalculate_prices():
1433
            return {}
1434
1435
        # The sorted records from the request
1436
        records = self.get_records()
1437
1438
        client = self.get_client()
1439
        setup = api.get_setup()
1440
1441
        member_discount = float(setup.getMemberDiscount())
1442
        member_discount_applies = False
1443
        if client:
1444
            member_discount_applies = client.getMemberDiscountApplies()
1445
1446
        prices = {}
1447
        for n, record in enumerate(records):
1448
            ardiscount_amount = 0.00
1449
            arservices_price = 0.00
1450
            arprofiles_price = 0.00
1451
            arprofiles_vat_amount = 0.00
1452
            arservice_vat_amount = 0.00
1453
            services_from_priced_profile = []
1454
1455
            profile_uids = record.get("Profiles_uid", "").split(",")
1456
            profile_uids = filter(lambda x: x, profile_uids)
1457
            profiles = map(self.get_object_by_uid, profile_uids)
1458
            services = map(self.get_object_by_uid, record.get("Analyses", []))
1459
1460
            # ANALYSIS PROFILES PRICE
1461
            for profile in profiles:
1462
                use_profile_price = profile.getUseAnalysisProfilePrice()
1463
                if not use_profile_price:
1464
                    continue
1465
1466
                profile_price = float(profile.getAnalysisProfilePrice())
1467
                arprofiles_price += profile_price
1468
                arprofiles_vat_amount += profile.getVATAmount()
1469
                profile_services = profile.getService()
1470
                services_from_priced_profile.extend(profile_services)
1471
1472
            # ANALYSIS SERVICES PRICE
1473
            for service in services:
1474
                if service in services_from_priced_profile:
1475
                    continue
1476
                service_price = float(service.getPrice())
1477
                # service_vat = float(service.getVAT())
1478
                service_vat_amount = float(service.getVATAmount())
1479
                arservice_vat_amount += service_vat_amount
1480
                arservices_price += service_price
1481
1482
            base_price = arservices_price + arprofiles_price
1483
1484
            # Calculate the member discount if it applies
1485
            if member_discount and member_discount_applies:
1486
                logger.info("Member discount applies with {}%".format(
1487
                    member_discount))
1488
                ardiscount_amount = base_price * member_discount / 100
1489
1490
            subtotal = base_price - ardiscount_amount
1491
            vat_amount = arprofiles_vat_amount + arservice_vat_amount
1492
            total = subtotal + vat_amount
1493
1494
            prices[n] = {
1495
                "discount": "{0:.2f}".format(ardiscount_amount),
1496
                "subtotal": "{0:.2f}".format(subtotal),
1497
                "vat": "{0:.2f}".format(vat_amount),
1498
                "total": "{0:.2f}".format(total),
1499
            }
1500
            logger.info("Prices for AR {}: Discount={discount} "
1501
                        "VAT={vat} Subtotal={subtotal} total={total}"
1502
                        .format(n, **prices[n]))
1503
1504
        return prices
1505
1506
    def check_confirmation(self):
1507
        """Returns a dict when user confirmation is required for the creation of
1508
        samples. Returns None otherwise
1509
        """
1510
        if self.request.form.get("confirmed") == "1":
1511
            # User pressed the "yes" button in the confirmation pane already
1512
            return None
1513
1514
        # Find out if there is a confirmation adapter available
1515
        adapter = queryAdapter(self.request, IAddSampleConfirmation)
1516
        if not adapter:
1517
            return None
1518
1519
        # Extract records from the request and call the adapter
1520
        records = self.get_records()
1521
        return adapter.check_confirmation(records)
1522
1523
    def ajax_submit(self):
1524
        """Submit & create the ARs
1525
        """
1526
        # Check if there is the need to display a confirmation pane
1527
        confirmation = self.check_confirmation()
1528
        if confirmation:
1529
            return {"confirmation": confirmation}
1530
1531
        # Get AR required fields (including extended fields)
1532
        fields = self.get_ar_fields()
1533
1534
        # extract records from request
1535
        records = self.get_records()
1536
1537
        fielderrors = {}
1538
        errors = {"message": "", "fielderrors": {}}
1539
1540
        attachments = {}
1541
        valid_records = []
1542
1543
        # Validate required fields
1544
        for n, record in enumerate(records):
1545
1546
            # Process UID fields first and set their values to the linked field
1547
            uid_fields = filter(lambda f: f.endswith("_uid"), record)
1548
            for field in uid_fields:
1549
                name = field.replace("_uid", "")
1550
                value = record.get(field)
1551
                if "," in value:
1552
                    value = value.split(",")
1553
                record[name] = value
1554
1555
            # Extract file uploads (fields ending with _file)
1556
            # These files will be added later as attachments
1557
            file_fields = filter(lambda f: f.endswith("_file"), record)
1558
            attachments[n] = map(lambda f: record.pop(f), file_fields)
0 ignored issues
show
introduced by
The variable record does not seem to be defined for all execution paths.
Loading history...
1559
1560
            # Required fields and their values
1561
            required_keys = [field.getName() for field in fields
1562
                             if field.required]
1563
            required_values = [record.get(key) for key in required_keys]
1564
            required_fields = dict(zip(required_keys, required_values))
1565
1566
            # Client field is required but hidden in the AR Add form. We remove
1567
            # it therefore from the list of required fields to let empty
1568
            # columns pass the required check below.
1569
            if record.get("Client", False):
1570
                required_fields.pop('Client', None)
1571
1572
            # Contacts get pre-filled out if only one contact exists.
1573
            # We won't force those columns with only the Contact filled out to
1574
            # be required.
1575
            contact = required_fields.pop("Contact", None)
1576
1577
            # None of the required fields are filled, skip this record
1578
            if not any(required_fields.values()):
1579
                continue
1580
1581
            # Re-add the Contact
1582
            required_fields["Contact"] = contact
1583
1584
            # Check if the contact belongs to the selected client
1585
            contact_obj = api.get_object(contact, None)
1586
            if not contact_obj:
1587
                fielderrors["Contact"] = _("No valid contact")
1588
            else:
1589
                parent_uid = api.get_uid(api.get_parent(contact_obj))
1590
                if parent_uid != record.get("Client"):
1591
                    msg = _("Contact does not belong to the selected client")
1592
                    fielderrors["Contact"] = msg
1593
1594
            # Missing required fields
1595
            missing = [f for f in required_fields if not record.get(f, None)]
1596
1597
            # Handle required fields from Service conditions
1598
            for condition in record.get("ServiceConditions", []):
1599
                if condition.get("required") == "on":
1600
                    if not condition.get("value"):
1601
                        title = condition.get("title")
1602
                        if title not in missing:
1603
                            missing.append(title)
1604
1605
            # If there are required fields missing, flag an error
1606
            for field in missing:
1607
                fieldname = "{}-{}".format(field, n)
1608
                msg = _("Field '{}' is required".format(field))
1609
                fielderrors[fieldname] = msg
1610
1611
            # Process valid record
1612
            valid_record = dict()
1613
            for fieldname, fieldvalue in six.iteritems(record):
1614
                # clean empty
1615
                if fieldvalue in ['', None]:
1616
                    continue
1617
                valid_record[fieldname] = fieldvalue
1618
1619
            # append the valid record to the list of valid records
1620
            valid_records.append(valid_record)
1621
1622
        # return immediately with an error response if some field checks failed
1623
        if fielderrors:
1624
            errors["fielderrors"] = fielderrors
1625
            return {'errors': errors}
1626
1627
        # do a custom validation of records. For instance, we may want to rise
1628
        # an error if a value set to a given field is not consistent with a
1629
        # value set to another field
1630
        validators = getAdapters((self.request, ), IAddSampleRecordsValidator)
1631
        for name, validator in validators:
1632
            validation_err = validator.validate(valid_records)
1633
            if validation_err:
1634
                # Not valid, return immediately with an error response
1635
                return {"errors": validation_err}
1636
1637
        # Process Form
1638
        actions = ActionHandlerPool.get_instance()
1639
        actions.queue_pool()
1640
        ARs = OrderedDict()
1641
        for n, record in enumerate(valid_records):
1642
            client_uid = record.get("Client")
1643
            client = self.get_object_by_uid(client_uid)
1644
1645
            if not client:
1646
                actions.resume()
1647
                raise RuntimeError("No client found")
1648
1649
            # Create the Analysis Request
1650
            try:
1651
                ar = crar(
1652
                    client,
1653
                    self.request,
1654
                    record,
1655
                )
1656
            except Exception as e:
1657
                actions.resume()
1658
                errors["message"] = str(e)
1659
                return {"errors": errors}
1660
            # We keep the title to check if AR is newly created
1661
            # and UID to print stickers
1662
            ARs[ar.Title()] = ar.UID()
1663
            for attachment in attachments.get(n, []):
1664
                if not attachment.filename:
1665
                    continue
1666
                att = _createObjectByType("Attachment", client, tmpID())
1667
                att.setAttachmentFile(attachment)
1668
                att.processForm()
1669
                ar.addAttachment(att)
1670
        actions.resume()
1671
1672
        level = "info"
1673
        if len(ARs) == 0:
1674
            message = _('No Samples could be created.')
1675
            level = "error"
1676
        elif len(ARs) > 1:
1677
            message = _('Samples ${ARs} were successfully created.',
1678
                        mapping={'ARs': safe_unicode(', '.join(ARs.keys()))})
1679
        else:
1680
            message = _('Sample ${AR} was successfully created.',
1681
                        mapping={'AR': safe_unicode(ARs.keys()[0])})
1682
1683
        # Display a portal message
1684
        self.context.plone_utils.addPortalMessage(message, level)
1685
1686
        # Automatic label printing
1687
        setup = api.get_setup()
1688
        auto_print = setup.getAutoPrintStickers()
1689
        if 'register' in auto_print and ARs:
1690
            return {
1691
                'success': message,
1692
                'stickers': ARs.values(),
1693
                'stickertemplate': setup.getAutoStickerTemplate()
1694
            }
1695
        else:
1696
            return {'success': message}
1697