bika.health.ajax.client   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 52
dl 0
loc 99
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B ClientAjaxHandler.getPublicationSettings() 0 45 5
A ajaxGetClientInfoFromCurrentUser.__call__() 0 12 3
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.HEALTH.
4
#
5
# SENAITE.HEALTH is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by the Free
7
# Software Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
import json
22
23
import plone
24
from Products.CMFCore.utils import getToolByName
25
from bika.health.ajax.ajaxhandler import AjaxHandler
26
from bika.lims import api
27
from bika.lims import bikaMessageFactory as _
28
from bika.lims.browser import BrowserView
29
from bika.lims.interfaces import IClient
30
31
32
class ClientAjaxHandler(AjaxHandler):
33
34
    def getPublicationSettings(self, params):
35
        """ Returns the settings for the publication of results to patients.
36
            The first output param is a dictionary with the following
37
            key/values:
38
            - AllowResultsDistributionToPatients: true/false
39
            - PatientPublicationPreferences: array of strings (email, etc.)
40
            - PatientPublicationAttachmentsPermitted: true/false
41
            If client not found, first param values are those from the default
42
            BikaSetup settings.
43
            The first output param is Null if there was an error.
44
            The second output param is an error message if there was an error
45
            invoking the method. Null if there was no error.
46
        """
47
        uid = params.get('uid', '')
48
        if not uid:
49
            error = _("Parameter '%s' is missing") % 'uid'
50
            return None, error
51
52
        DEF = 'DefaultResultsDistributionToPatients'
53
        ALLOW = 'AllowResultsDistributionToPatients'
54
        PREF = 'PatientPublicationPreferences'
55
        ATTACH = 'PatientPublicationAttachmentsPermitted'
56
        res = {}
57
58
        bc = getToolByName(self.context, 'portal_catalog')
59
        proxies = bc(UID=uid)
60
61
        defsettings = True
62
        if len(proxies) == 1:
63
            client = proxies[0].getObject()
64
            sch = client.Schema()
65
            defsettings = sch[DEF].get(client)
66
            if defsettings == False:
67
                res = {ALLOW: sch[ALLOW].get(client),
68
                       PREF: sch[PREF].get(client),
69
                       ATTACH: sch[ATTACH].get(client)}
70
        if defsettings:
71
            # Retrieve from Bika Setup
72
            bs = self.context.bika_setup
73
            sch = bs.Schema()
74
            res = {ALLOW: sch[ALLOW].get(bs),
75
                   PREF: sch[PREF].get(bs),
76
                   ATTACH: sch[ATTACH].get(bs)}
77
78
        return res, None
79
80
81
class ajaxGetClientInfoFromCurrentUser(BrowserView):
82
    """Returns the client information associated to the current user (if the
83
    current user has a contact associated that it's parent is a Client).
84
    Otherwise, returns an empty dict
85
    """
86
87
    def __call__(self):
88
        plone.protect.CheckAuthenticator(self.request)
89
        curr_user = api.get_current_user()
90
        contact = api.get_user_contact(curr_user, contact_types=['Contact'])
91
        parent = contact and contact.getParent() or None
92
        if parent and not IClient.providedBy(parent):
93
            parent = None
94
        ret = {'ClientTitle': parent and parent.Title() or '',
95
               'ClientID': parent and parent.getClientID() or '',
96
               'ClientSysID': parent and parent.id or '',
97
               'ClientUID': parent and parent.UID() or '',}
98
        return json.dumps(ret)
99