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 ObjectCreatedEventHandler(patient, event): |
26
|
|
|
# Ensure Clients can contain Patients. Doing this here we guarantee that |
27
|
|
|
# Patients are allowed content types even if the types tool is reloaded in |
28
|
|
|
# senaite.core due to an upgrade step |
29
|
|
|
# https://github.com/senaite/senaite.health/pull/159 |
30
|
|
|
allow_patients_inside_clients() |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def ObjectModifiedEventHandler(patient, event): |
34
|
|
|
"""Actions to be done when a patient is modified. Moves the Patient to |
35
|
|
|
Client folder if assigned |
36
|
|
|
""" |
37
|
|
|
# Ensure Clients can contain Patients. Doing this here we guarantee that |
38
|
|
|
# Patients are allowed content types even if the types tool is reloaded in |
39
|
|
|
# senaite.core due to an upgrade step |
40
|
|
|
# https://github.com/senaite/senaite.health/pull/159 |
41
|
|
|
allow_patients_inside_clients() |
42
|
|
|
|
43
|
|
|
# If client is assigned, move the Patient to the Client's folder |
44
|
|
|
# Note here we get the Client directly from the Schema, cause |
45
|
|
|
# getPrimaryReferrer is overriden in Patient content type to always look to |
46
|
|
|
# aq_parent in order to prevent inconsistencies (the PrimaryReferrer schema |
47
|
|
|
# field is only used to allow the user to assign a Client to the Patient). |
48
|
|
|
client = patient.getField("PrimaryReferrer").get(patient) |
49
|
|
|
|
50
|
|
|
# Check if the Patient is being created inside the Client |
51
|
|
|
if client and client.UID() != patient.aq_parent.UID(): |
52
|
|
|
# Move the Patient inside the client |
53
|
|
|
cp = patient.aq_parent.manage_cutObjects(patient.id) |
54
|
|
|
client.manage_pasteObjects(cp) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def allow_patients_inside_clients(): |
58
|
|
|
"""Adds Patient content type to the list of allowed objects inside Client |
59
|
|
|
""" |
60
|
|
|
portal_types = api.get_tool("portal_types") |
61
|
|
|
client_fti = portal_types.getTypeInfo("Client") |
62
|
|
|
allowed_types = client_fti.allowed_content_types |
63
|
|
|
if "Patient" not in allowed_types: |
64
|
|
|
client_fti.allowed_content_types = allowed_types + ("Patient", ) |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
# TODO: This is no longer needed! |
68
|
|
|
def assign_owners_for(patient): |
69
|
|
|
"""Assign the role "Owner" to the contacts of the client assigned to the |
70
|
|
|
patient passed in, if any |
71
|
|
|
""" |
72
|
|
|
client = patient.getClient() |
73
|
|
|
if not client: |
74
|
|
|
return False |
75
|
|
|
|
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.grant_local_roles_for(patient, roles=["Owner"], user=user) |
81
|
|
|
patient.reindexObjectSecurity() |
82
|
|
|
return True |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
def purge_owners_for(patient): |
86
|
|
|
"""Remove role "Owner" from all those client contacts that do not belong to |
87
|
|
|
the same Client the patient is assigned to and assigns the role "Owner" to |
88
|
|
|
the client contacts assigned to the patient |
89
|
|
|
""" |
90
|
|
|
# Add role "Owner" for this Patient to all contacts from this Client |
91
|
|
|
assign_owners_for(patient) |
92
|
|
|
|
93
|
|
|
# Unassign role "Owner" from contacts that belong to another Client |
94
|
|
|
patient_client = patient.getClient() |
95
|
|
|
patient_client_uid = patient_client and api.get_uid(patient_client) or None |
96
|
|
|
for client in api.search(dict(portal_type="Client"), "portal_catalog"): |
97
|
|
|
if api.get_uid(client) == patient_client_uid: |
98
|
|
|
continue |
99
|
|
|
|
100
|
|
|
client = api.get_object(client) |
101
|
|
|
contacts = client.objectValues("Contact") |
102
|
|
|
users = map(lambda contact: contact.getUser(), contacts) |
103
|
|
|
users = filter(None, users) |
104
|
|
|
for user in users: |
105
|
|
|
security.revoke_local_roles_for(patient, ["Owner"], user=user) |
106
|
|
|
patient.reindexObjectSecurity() |
107
|
|
|
|