|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
from senaite.core.schema.interfaces import IBaseField |
|
4
|
|
|
from z3c.form.datamanager import AttributeField |
|
5
|
|
|
from z3c.form.datamanager import DictionaryField |
|
6
|
|
|
from z3c.form.interfaces import NO_VALUE |
|
7
|
|
|
from zope.component import adapts |
|
8
|
|
|
from zope.interface import Interface |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class AttributeDataManager(AttributeField): |
|
12
|
|
|
"""Senaite Data Manager for Attribute Fields |
|
13
|
|
|
|
|
14
|
|
|
Original implementation: `z3c.form.datamanager` |
|
15
|
|
|
|
|
16
|
|
|
NOTE: The original implementation does not use the setter/getter of the |
|
17
|
|
|
field, see z3c/form/datamanager.txt for explanation. |
|
18
|
|
|
|
|
19
|
|
|
However, we want to have that control at field level and also be able to |
|
20
|
|
|
execute some custom logic when getting/setting the values, e.g. fire |
|
21
|
|
|
modification events, check permissions, audit-logging etc. |
|
22
|
|
|
""" |
|
23
|
|
|
adapts(Interface, IBaseField) |
|
24
|
|
|
|
|
25
|
|
|
def __init__(self, context, field): |
|
26
|
|
|
super(AttributeDataManager, self).__init__(context, field) |
|
27
|
|
|
|
|
28
|
|
|
def get(self): |
|
29
|
|
|
"""Delegate to the field getter |
|
30
|
|
|
""" |
|
31
|
|
|
return self.field.get(self.adapted_context) |
|
32
|
|
|
|
|
33
|
|
|
def set(self, value): |
|
34
|
|
|
"""Delegate to the field setter |
|
35
|
|
|
""" |
|
36
|
|
|
self.field.set(self.adapted_context, value) |
|
37
|
|
|
|
|
38
|
|
|
def query(self, default=NO_VALUE): |
|
39
|
|
|
"""Delegate to the field `get_raw` method |
|
40
|
|
|
|
|
41
|
|
|
This method is called from widget `update` to get the value. |
|
42
|
|
|
It should therefore return a value that is suitable, e.g. not objects. |
|
43
|
|
|
""" |
|
44
|
|
|
get_raw = getattr(self.field, "get_raw", None) |
|
45
|
|
|
if not callable(get_raw): |
|
46
|
|
|
return super(AttributeDataManager, self).query(default=default) |
|
47
|
|
|
return get_raw(self.adapted_context) |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
class DictionaryDataManager(DictionaryField): |
|
51
|
|
|
"""Senaite Data Manager for Dictionary Fields |
|
52
|
|
|
|
|
53
|
|
|
Original implementation: `z3c.form.datamanager` |
|
54
|
|
|
|
|
55
|
|
|
Currently only implemented as a boiler plate for eventual later use. |
|
56
|
|
|
""" |
|
57
|
|
|
adapts(dict, IBaseField) |
|
58
|
|
|
|
|
59
|
|
|
def __init__(self, data, field): |
|
60
|
|
|
super(DictionaryDataManager, self).__init__(data, field) |
|
61
|
|
|
|