Passed
Push — master ( bcb3c3...740c75 )
by Pau
04:01
created

r()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 2
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
from bika.lims import api
22
from bika.lims.api import security
23
24
25
def ObjectModifiedEventHandler(patient, event):
26
    """Actions to be done when a patient is modified. Moves the Patient to
27
    Client folder if assigned
28
    """
29
    # If client is assigned, move the Patient to the Client's folder
30
    # Note here we get the Client directly from the Schema, cause
31
    # getPrimaryReferrer is overriden in Patient content type to always look to
32
    # aq_parent in order to prevent inconsistencies (the PrimaryReferrer schema
33
    # field is only used to allow the user to assign a Client to the Patient).
34
    client = patient.getField("PrimaryReferrer").get(patient)
35
36
    # Check if the Patient is being created inside the Client
37
    if client and client.UID() != patient.aq_parent.UID():
38
        # Move the Patient inside the client
39
        cp = patient.aq_parent.manage_cutObjects(patient.id)
40
        client.manage_pasteObjects(cp)
41
42
# TODO: This is no longer needed!
43
def assign_owners_for(patient):
44
    """Assign the role "Owner" to the contacts of the client assigned to the
45
    patient passed in, if any
46
    """
47
    client = patient.getClient()
48
    if not client:
49
        return False
50
51
    contacts = client.objectValues("Contact")
52
    users = map(lambda contact: contact.getUser(), contacts)
53
    users = filter(None, users)
54
    for user in users:
55
        security.grant_local_roles_for(patient, roles=["Owner"], user=user)
56
    patient.reindexObjectSecurity()
57
    return True
58
59
60
def purge_owners_for(patient):
61
    """Remove role "Owner" from all those client contacts that do not belong to
62
    the same Client the patient is assigned to and assigns the role "Owner" to
63
    the client contacts assigned to the patient
64
    """
65
    # Add role "Owner" for this Patient to all contacts from this Client
66
    assign_owners_for(patient)
67
68
    # Unassign role "Owner" from contacts that belong to another Client
69
    patient_client = patient.getClient()
70
    patient_client_uid = patient_client and api.get_uid(patient_client) or None
71
    for client in api.search(dict(portal_type="Client"), "portal_catalog"):
72
        if api.get_uid(client) == patient_client_uid:
73
            continue
74
75
        client = api.get_object(client)
76
        contacts = client.objectValues("Contact")
77
        users = map(lambda contact: contact.getUser(), contacts)
78
        users = filter(None, users)
79
        for user in users:
80
            security.revoke_local_roles_for(patient, ["Owner"], user=user)
81
    patient.reindexObjectSecurity()
82