1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of SENAITE.CORE |
4
|
|
|
# |
5
|
|
|
# Copyright 2018 by it's authors. |
6
|
|
|
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst. |
7
|
|
|
|
8
|
|
|
"""Generic field extensions |
9
|
|
|
""" |
10
|
|
|
|
11
|
|
|
from Products.ATExtensions.ateapi import RecordField |
12
|
|
|
from Products.ATExtensions.ateapi import RecordsField |
13
|
|
|
from Products.Archetypes.public import AnnotationStorage |
14
|
|
|
from Products.Archetypes.public import BooleanField |
15
|
|
|
from Products.Archetypes.public import ComputedField |
16
|
|
|
from Products.Archetypes.public import DateTimeField |
17
|
|
|
from Products.Archetypes.public import FloatField |
18
|
|
|
from Products.Archetypes.public import IntegerField |
19
|
|
|
from Products.Archetypes.public import LinesField |
20
|
|
|
from Products.Archetypes.public import ReferenceField |
21
|
|
|
from Products.Archetypes.public import StringField |
22
|
|
|
from Products.Archetypes.public import TextField |
23
|
|
|
from archetypes.schemaextender.interfaces import IExtensionField |
24
|
|
|
from zope.interface import implements |
25
|
|
|
from zope.site.hooks import getSite |
26
|
|
|
|
27
|
|
|
from bika.lims.browser.fields.proxyfield import ProxyField |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class ExtensionField(object): |
31
|
|
|
|
32
|
|
|
"""Mix-in class to make Archetypes fields not depend on generated |
33
|
|
|
accessors and mutators, and use AnnotationStorage by default. |
34
|
|
|
|
35
|
|
|
""" |
36
|
|
|
|
37
|
|
|
implements(IExtensionField) |
38
|
|
|
|
39
|
|
|
storage = AnnotationStorage() |
40
|
|
|
|
41
|
|
|
def __init__(self, *args, **kwargs): |
42
|
|
|
super(ExtensionField, self).__init__(*args, **kwargs) |
43
|
|
|
self.args = args |
44
|
|
|
self.kwargs = kwargs |
45
|
|
|
|
46
|
|
|
def getAccessor(self, instance): |
47
|
|
|
def accessor(): |
48
|
|
|
if self.getType().endswith('ReferenceField'): |
49
|
|
|
return self.get(instance.__of__(getSite())) |
50
|
|
|
else: |
51
|
|
|
return self.get(instance) |
52
|
|
|
return accessor |
53
|
|
|
|
54
|
|
|
def getEditAccessor(self, instance): |
55
|
|
|
def edit_accessor(): |
56
|
|
|
if self.getType().endswith('ReferenceField'): |
57
|
|
|
return self.getRaw(instance.__of__(getSite())) |
58
|
|
|
else: |
59
|
|
|
return self.getRaw(instance) |
60
|
|
|
return edit_accessor |
61
|
|
|
|
62
|
|
|
def getMutator(self, instance): |
63
|
|
|
def mutator(value, **kw): |
64
|
|
|
if self.getType().endswith('ReferenceField'): |
65
|
|
|
self.set(instance.__of__(getSite()), value) |
66
|
|
|
else: |
67
|
|
|
self.set(instance, value) |
68
|
|
|
return mutator |
69
|
|
|
|
70
|
|
|
def getIndexAccessor(self, instance): |
71
|
|
|
name = getattr(self, 'index_method', None) |
72
|
|
|
if name is None or name == '_at_accessor': |
73
|
|
|
return self.getAccessor(instance) |
74
|
|
|
elif name == '_at_edit_accessor': |
75
|
|
|
return self.getEditAccessor(instance) |
76
|
|
|
elif not isinstance(name, basestring): |
77
|
|
|
raise ValueError('Bad index accessor value: %r', name) |
78
|
|
|
else: |
79
|
|
|
return getattr(instance, name) |
80
|
|
|
|
81
|
|
|
class ExtBooleanField(ExtensionField, BooleanField): |
82
|
|
|
|
83
|
|
|
"Field extender" |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
class ExtComputedField(ExtensionField, ComputedField): |
87
|
|
|
|
88
|
|
|
"Field extender" |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class ExtDateTimeField(ExtensionField, DateTimeField): |
92
|
|
|
|
93
|
|
|
"Field extender" |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
class ExtFloatField(ExtensionField, FloatField): |
97
|
|
|
|
98
|
|
|
"Field extender" |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
class ExtIntegerField(ExtensionField, IntegerField): |
102
|
|
|
|
103
|
|
|
"Field extender" |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
class ExtLinesField(ExtensionField, LinesField): |
107
|
|
|
|
108
|
|
|
"Field extender" |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
class ExtRecordField(ExtensionField, RecordField): |
112
|
|
|
|
113
|
|
|
"Field extender" |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
class ExtRecordsField(ExtensionField, RecordsField): |
117
|
|
|
|
118
|
|
|
"Field extender" |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
class ExtReferenceField(ExtensionField, ReferenceField): |
122
|
|
|
|
123
|
|
|
"Field extender" |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
class ExtStringField(ExtensionField, StringField): |
127
|
|
|
|
128
|
|
|
"Field extender" |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
class ExtTextField(ExtensionField, TextField): |
132
|
|
|
|
133
|
|
|
"Field extender" |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
class ExtProxyField(ExtensionField, ProxyField): |
137
|
|
|
|
138
|
|
|
"""Field extender""" |
139
|
|
|
|