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 Products.CMFPlone.utils import safe_unicode |
22
|
|
|
from bika.lims import bikaMessageFactory as _ |
23
|
|
|
from bika.lims.utils import to_utf8 |
24
|
|
|
from Products.CMFCore.utils import getToolByName |
25
|
|
|
from zope.interface import implements |
26
|
|
|
from Products.validation import validation |
27
|
|
|
from Products.validation.interfaces.IValidator import IValidator |
28
|
|
|
from datetime import datetime |
29
|
|
|
from bika.lims import api |
30
|
|
|
from bika.health.catalog.patient_catalog import CATALOG_PATIENTS |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class Date_Format_Validator: |
34
|
|
|
|
35
|
|
|
""" Verifies whether the format is the correct or not """ |
36
|
|
|
|
37
|
|
|
implements(IValidator) |
38
|
|
|
name = "isDateFormat" |
39
|
|
|
|
40
|
|
|
def __call__(self, value, *args, **kwargs): |
41
|
|
|
field = kwargs.get('field', None) |
42
|
|
|
required = hasattr(field, "required") and field.required or False |
43
|
|
|
if not value and not required: |
44
|
|
|
return True |
45
|
|
|
|
46
|
|
|
try: |
47
|
|
|
datetime.strptime(value, '%Y-%m-%d') |
48
|
|
|
except ValueError: |
49
|
|
|
instance = kwargs['instance'] |
50
|
|
|
title = kwargs['field'].widget.label |
51
|
|
|
trans = getToolByName(instance, 'translation_service').translate |
52
|
|
|
msg = _( |
53
|
|
|
"Incorrect data format in '${title}', should be YYYY-MM-DD", |
54
|
|
|
mapping={'title': safe_unicode(value)} |
55
|
|
|
) |
56
|
|
|
return to_utf8(trans(msg)) |
57
|
|
|
return True |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
validation.register(Date_Format_Validator()) |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
class UniqueClientPatientIDValidator: |
64
|
|
|
""" |
65
|
|
|
Checks if the client patient ID is unique. It does |
66
|
|
|
so only if the checkbox 'Client Patient ID must be |
67
|
|
|
unique' is selected . This checkbox can be found in |
68
|
|
|
Bika Setup under Id server tab |
69
|
|
|
""" |
70
|
|
|
|
71
|
|
|
implements(IValidator) |
72
|
|
|
name = "unique_client_patient_ID_validator" |
73
|
|
|
|
74
|
|
|
def __call__(self, value, *args, **kwargs): |
75
|
|
|
# avoid the catalog query if the option is not selected |
76
|
|
|
setup = api.get_setup() |
77
|
|
|
if not getattr(setup, "ClientPatientIDUnique", False): |
78
|
|
|
return True |
79
|
|
|
query = dict(getClientPatientID=value) |
80
|
|
|
patients = api.search(query, CATALOG_PATIENTS) |
81
|
|
|
instance = kwargs.get('instance') |
82
|
|
|
# If there are no patients with this Client Patient ID |
83
|
|
|
# then it is valid |
84
|
|
|
if not patients: |
85
|
|
|
return True |
86
|
|
|
# If there is only one patient with this Client Patient ID |
87
|
|
|
# and it is the patient being edited then it also valid |
88
|
|
|
if len(patients) == 1 and api.get_uid(patients[0]) == api.get_uid(instance): |
89
|
|
|
return True |
90
|
|
|
trans = getToolByName(instance, 'translation_service').translate |
91
|
|
|
msg = _( |
92
|
|
|
"Validation failed: '${value}' is not unique", |
93
|
|
|
mapping={ |
94
|
|
|
'value': safe_unicode(value) |
95
|
|
|
}) |
96
|
|
|
return to_utf8(trans(msg)) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
validation.register(UniqueClientPatientIDValidator()) |
100
|
|
|
|