-
Notifications
You must be signed in to change notification settings - Fork 39
/
field.py
332 lines (289 loc) · 12.3 KB
/
field.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
##############################################################################
#
# Copyright (c) 2006 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Field Implementation
$Id$
"""
__docformat__ = "reStructuredText"
import zope.component
import zope.interface
import zope.location
import zope.schema.interfaces
from z3c.form import interfaces, util
from z3c.form.error import MultipleErrors
from z3c.form.widget import AfterWidgetUpdateEvent
def _initkw(keepReadOnly=(), omitReadOnly=False, **defaults):
return keepReadOnly, omitReadOnly, defaults
class WidgetFactories(dict):
def __init__(self):
super(WidgetFactories, self).__init__()
self.default = None
def __getitem__(self, key):
if key not in self and self.default:
return self.default
return super(WidgetFactories, self).__getitem__(key)
def get(self, key, default=None):
if key not in self and self.default:
return self.default
return super(WidgetFactories, self).get(key, default)
class WidgetFactoryProperty(object):
def __get__(self, inst, klass):
if not hasattr(inst, '_widgetFactories'):
inst._widgetFactories = WidgetFactories()
return inst._widgetFactories
def __set__(self, inst, value):
if not hasattr(inst, '_widgetFactories'):
inst._widgetFactories = WidgetFactories()
inst._widgetFactories.default = value
@zope.interface.implementer(interfaces.IField)
class Field(object):
"""Field implementation."""
widgetFactory = WidgetFactoryProperty()
def __init__(self, field, name=None, prefix='', mode=None, interface=None,
ignoreContext=None, showDefault=None):
self.field = field
if name is None:
name = field.__name__
assert name
self.__name__ = util.expandPrefix(prefix) + name
self.prefix = prefix
self.mode = mode
if interface is None:
interface = field.interface
self.interface = interface
self.ignoreContext = ignoreContext
self.showDefault = showDefault
def __repr__(self):
return '<%s %r>' % (self.__class__.__name__, self.__name__)
@zope.interface.implementer(interfaces.IFields)
class Fields(util.SelectionManager):
"""Field manager."""
managerInterface = interfaces.IFields
def __init__(self, *args, **kw):
keepReadOnly, omitReadOnly, defaults = _initkw(**kw)
fields = []
for arg in args:
if isinstance(arg, zope.interface.interface.InterfaceClass):
for name, field in zope.schema.getFieldsInOrder(arg):
fields.append((name, field, arg))
elif zope.schema.interfaces.IField.providedBy(arg):
name = arg.__name__
if not name:
raise ValueError("Field has no name")
fields.append((name, arg, arg.interface))
elif self.managerInterface.providedBy(arg):
for form_field in arg.values():
fields.append(
(form_field.__name__, form_field, form_field.interface))
elif isinstance(arg, Field):
fields.append((arg.__name__, arg, arg.interface))
else:
raise TypeError("Unrecognized argument type", arg)
self._data_keys = []
self._data_values = []
self._data = {}
for name, field, iface in fields:
if isinstance(field, Field):
form_field = field
else:
if field.readonly:
if omitReadOnly and (name not in keepReadOnly):
continue
customDefaults = defaults.copy()
if iface is not None:
customDefaults['interface'] = iface
form_field = Field(field, **customDefaults)
name = form_field.__name__
if name in self._data:
raise ValueError("Duplicate name", name)
self._data_values.append(form_field)
self._data_keys.append(name)
self._data[name] = form_field
def select(self, *names, **kwargs):
"""See interfaces.IFields"""
prefix = kwargs.pop('prefix', None)
interface = kwargs.pop('interface', None)
assert len(kwargs) == 0
if prefix:
names = [util.expandPrefix(prefix) + name for name in names]
mapping = self
if interface is not None:
mapping = dict([(field.field.__name__, field)
for field in self.values()
if field.field.interface is interface])
return self.__class__(*[mapping[name] for name in names])
def omit(self, *names, **kwargs):
"""See interfaces.IFields"""
prefix = kwargs.pop('prefix', None)
interface = kwargs.pop('interface', None)
assert len(kwargs) == 0
if prefix:
names = [util.expandPrefix(prefix) + name for name in names]
return self.__class__(
*[field for name, field in self.items()
if not ((name in names and interface is None) or
(field.field.interface is interface and
field.field.__name__ in names)) ])
@zope.interface.implementer_only(interfaces.IWidgets)
class FieldWidgets(util.Manager):
"""Widget manager for IFieldWidget."""
zope.component.adapts(
interfaces.IFieldsForm, interfaces.IFormLayer, zope.interface.Interface)
prefix = 'widgets.'
mode = interfaces.INPUT_MODE
errors = ()
hasRequiredFields = False
ignoreContext = False
ignoreRequest = False
ignoreReadonly = False
ignoreRequiredOnExtract = False
setErrors = True
def __init__(self, form, request, content):
super(FieldWidgets, self).__init__()
self.form = form
self.request = request
self.content = content
def validate(self, data):
fields = self.form.fields.values()
# Step 1: Collect the data for the various schemas
schemaData = {}
for field in fields:
schema = field.interface
if schema is None:
continue
fieldData = schemaData.setdefault(schema, {})
if field.__name__ in data:
fieldData[field.field.__name__] = data[field.__name__]
# Step 2: Validate the individual schemas and collect errors
errors = ()
content = self.content
if self.ignoreContext:
content = None
for schema, fieldData in schemaData.items():
validator = zope.component.getMultiAdapter(
(content, self.request, self.form, schema, self),
interfaces.IManagerValidator)
errors += validator.validate(fieldData)
return errors
def update(self):
"""See interfaces.IWidgets"""
# Create a unique prefix.
prefix = util.expandPrefix(self.form.prefix)
prefix += util.expandPrefix(self.prefix)
# Walk through each field, making a widget out of it.
uniqueOrderedKeys = []
for field in self.form.fields.values():
# Step 0. Determine whether the context should be ignored.
ignoreContext = self.ignoreContext
if field.ignoreContext is not None:
ignoreContext = field.ignoreContext
# Step 1: Determine the mode of the widget.
mode = self.mode
if field.mode is not None:
mode = field.mode
elif field.field.readonly and not self.ignoreReadonly:
mode = interfaces.DISPLAY_MODE
elif not ignoreContext:
# If we do not have enough permissions to write to the
# attribute, then switch to display mode.
dm = zope.component.getMultiAdapter(
(self.content, field.field), interfaces.IDataManager)
if not dm.canWrite():
mode = interfaces.DISPLAY_MODE
# Step 2: Get the widget for the given field.
shortName = field.__name__
newWidget = True
if shortName in self._data:
# reuse existing widget
widget = self._data[shortName]
newWidget = False
elif field.widgetFactory.get(mode) is not None:
factory = field.widgetFactory.get(mode)
widget = factory(field.field, self.request)
else:
widget = zope.component.getMultiAdapter(
(field.field, self.request), interfaces.IFieldWidget)
# Step 3: Set the prefix for the widget
widget.name = prefix + shortName
widget.id = (prefix + shortName).replace('.', '-')
# Step 4: Set the context
widget.context = self.content
# Step 5: Set the form
widget.form = self.form
# Optimization: Set both interfaces here, rather in step 4 and 5:
# ``alsoProvides`` is quite slow
zope.interface.alsoProvides(
widget, interfaces.IContextAware, interfaces.IFormAware)
# Step 6: Set some variables
widget.ignoreContext = ignoreContext
widget.ignoreRequest = self.ignoreRequest
if field.showDefault is not None:
widget.showDefault = field.showDefault
# Step 7: Set the mode of the widget
widget.mode = mode
# Step 8: Update the widget
widget.update()
zope.event.notify(AfterWidgetUpdateEvent(widget))
# Step 9: Add the widget to the manager
if widget.required:
self.hasRequiredFields = True
uniqueOrderedKeys.append(shortName)
if newWidget:
self._data_values.append(widget)
self._data[shortName] = widget
zope.location.locate(widget, self, shortName)
# always ensure that we add all keys and keep the order given from
# button items
self._data_keys = uniqueOrderedKeys
def extract(self):
"""See interfaces.IWidgets"""
data = {}
errors = ()
for name, widget in self.items():
if widget.mode == interfaces.DISPLAY_MODE:
continue
value = widget.field.missing_value
try:
widget.setErrors = self.setErrors
raw = widget.extract()
if raw is not interfaces.NO_VALUE:
value = interfaces.IDataConverter(widget).toFieldValue(raw)
widget.ignoreRequiredOnValidation = self.ignoreRequiredOnExtract
zope.component.getMultiAdapter(
(self.content,
self.request,
self.form,
getattr(widget, 'field', None),
widget),
interfaces.IValidator).validate(value)
except (zope.interface.Invalid,
ValueError, MultipleErrors) as error:
view = zope.component.getMultiAdapter(
(error, self.request, widget, widget.field,
self.form, self.content), interfaces.IErrorViewSnippet)
view.update()
if self.setErrors:
widget.error = view
errors += (view,)
else:
name = widget.__name__
data[name] = value
for error in self.validate(data):
view = zope.component.getMultiAdapter(
(error, self.request, None, None, self.form, self.content),
interfaces.IErrorViewSnippet)
view.update()
errors += (view,)
if self.setErrors:
self.errors = errors
return data, errors