Passed
Pull Request — 2.x (#1806)
by Ramon
06:55 queued 02:08
created

senaite.core.z3cform.datamanager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 18
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A AttributeDataManager.__init__() 0 2 1
A AttributeDataManager.set() 0 4 1
A DictionaryDataManager.__init__() 0 2 1
A AttributeDataManager.get() 0 4 1
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 zope.component import adapts
7
from zope.interface import Interface
8
9
10
class AttributeDataManager(AttributeField):
11
    """Senaite Data Manager for Attribute Fields
12
13
    Original implementation: `z3c.form.datamanager`
14
15
    NOTE: The original implementation does not use the setter/getter of the
16
          field, see z3c/form/datamanager.txt for explanation.
17
18
    However, we want to have that control at field level and also be able to
19
    execute some custom logic when getting/setting the values, e.g. fire
20
    modification events, check permissions, audit-logging etc.
21
    """
22
    adapts(Interface, IBaseField)
23
24
    def __init__(self, context, field):
25
        super(AttributeDataManager, self).__init__(context, field)
26
27
    def get(self):
28
        """Delegate to the field getter
29
        """
30
        return self.field.get(self.adapted_context)
31
32
    def set(self, value):
33
        """Delegate to the field setter
34
        """
35
        self.field.set(self.adapted_context, value)
36
37
38
class DictionaryDataManager(DictionaryField):
39
    """Senaite Data Manager for Dictionary Fields
40
41
    Original implementation: `z3c.form.datamanager`
42
43
    Currently only implemented as a boiler plate for eventual later use.
44
    """
45
    adapts(dict, IBaseField)
46
47
    def __init__(self, data, field):
48
        super(DictionaryDataManager, self).__init__(data, field)
49