|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
from AccessControl import Unauthorized |
|
4
|
|
|
from bika.lims import api |
|
5
|
|
|
from bika.lims.interfaces import IAnalysisRequest |
|
6
|
|
|
from Products.Archetypes.utils import mapply |
|
7
|
|
|
from senaite.core import logger |
|
8
|
|
|
from senaite.core.datamanagers import DataManager |
|
9
|
|
|
from zope.component import adapter |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
@adapter(IAnalysisRequest) |
|
13
|
|
|
class SampleDataManager(DataManager): |
|
14
|
|
|
"""Data Manager for Samples |
|
15
|
|
|
""" |
|
16
|
|
|
|
|
17
|
|
|
@property |
|
18
|
|
|
def fields(self): |
|
19
|
|
|
return api.get_fields(self.context) |
|
20
|
|
|
|
|
21
|
|
|
def is_field_readable(self, field): |
|
22
|
|
|
"""Checks if the field is readable |
|
23
|
|
|
""" |
|
24
|
|
|
return field.checkPermission("get", self.context) |
|
25
|
|
|
|
|
26
|
|
|
def is_field_writeable(self, field): |
|
27
|
|
|
"""Checks if the field is writeable |
|
28
|
|
|
""" |
|
29
|
|
|
return field.checkPermission("set", self.context) |
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
def get(self, name): |
|
|
|
|
|
|
32
|
|
|
"""Get sample field |
|
33
|
|
|
""" |
|
34
|
|
|
# schema field |
|
35
|
|
|
field = self.fields.get(name) |
|
36
|
|
|
|
|
37
|
|
|
# check if the field exists |
|
38
|
|
|
if field is None: |
|
39
|
|
|
raise AttributeError("Field '{}' not found".format(name)) |
|
40
|
|
|
|
|
41
|
|
|
# Check the permission of the field |
|
42
|
|
|
if not self.is_field_readable(field): |
|
43
|
|
|
raise Unauthorized("Field '{}' not readable!".format(name)) |
|
44
|
|
|
|
|
45
|
|
|
# return the value with the field accessor |
|
46
|
|
|
if hasattr(field, "getAccessor"): |
|
47
|
|
|
accessor = field.getAccessor(self.context) |
|
48
|
|
|
return accessor() |
|
49
|
|
|
else: |
|
50
|
|
|
# Set the value on the field directly |
|
51
|
|
|
return field.get(self.context) |
|
52
|
|
|
|
|
53
|
|
|
def set(self, name, value): |
|
54
|
|
|
"""Set sample field or analysis result |
|
55
|
|
|
""" |
|
56
|
|
|
# set of updated objects |
|
57
|
|
|
updated_objects = set() |
|
58
|
|
|
|
|
59
|
|
|
# schema field |
|
60
|
|
|
field = self.fields.get(name) |
|
61
|
|
|
|
|
62
|
|
|
if field is None: |
|
63
|
|
|
raise AttributeError("Field '{}' not found".format(name)) |
|
64
|
|
|
|
|
65
|
|
|
# Check the permission of the field |
|
66
|
|
|
if not self.is_field_writeable(field): |
|
67
|
|
|
logger.error("Field '{}' not writeable!".format(name)) |
|
68
|
|
|
return [] |
|
69
|
|
|
# get the field mutator (works only for AT content types) |
|
70
|
|
|
if hasattr(field, "getMutator"): |
|
71
|
|
|
mutator = field.getMutator(self.context) |
|
72
|
|
|
mapply(mutator, value) |
|
73
|
|
|
else: |
|
74
|
|
|
# Set the value on the field directly |
|
75
|
|
|
field.set(self.context, value) |
|
76
|
|
|
|
|
77
|
|
|
updated_objects.add(self.context) |
|
78
|
|
|
|
|
79
|
|
|
# return a unified list of the updated objects |
|
80
|
|
|
return list(updated_objects) |
|
81
|
|
|
|