-
Notifications
You must be signed in to change notification settings - Fork 4
/
__init__.py
111 lines (89 loc) · 3.38 KB
/
__init__.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
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation 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.
#
##############################################################################
"""Generic Widget base classes
$Id$
"""
from zope.app.form.interfaces import IWidget, InputErrors
from zope.component.interfaces import IViewFactory
from zope.interface import implements
from zope.i18n import translate
class Widget(object):
"""Mixin class providing functionality common across widget types."""
implements(IWidget)
_prefix = 'field.'
_data_marker = object()
_data = _data_marker
visible = True
def __init__(self, context, request):
self.context = context
self.request = request
self.name = self._prefix + context.__name__
label = property(lambda self: self._translate(
self.context.title))
hint = property(lambda self: self._translate(
self.context.description))
def _translate(self, text):
return translate(text, "zope", context=self.request, default=text)
def _renderedValueSet(self):
"""Returns True if the the widget's rendered value has been set.
This is a convenience method that widgets can use to check whether
or not setRenderedValue was called.
"""
return self._data is not self._data_marker
def setPrefix(self, prefix):
if not prefix.endswith("."):
prefix += '.'
self._prefix = prefix
self.name = prefix + self.context.__name__
def setRenderedValue(self, value):
self._data = value
class InputWidget(Widget):
"""Mixin class providing some default input widget methods."""
def hasValidInput(self):
try:
self.validate()
return True
except InputErrors:
return False
def validate(self):
self.getInputValue()
class CustomWidgetFactory(object):
"""Custom Widget Factory."""
implements(IViewFactory)
def __init__(self, *args, **kw):
self._widget_factory = args[0]
if len(args) > 1:
self.args = args[1:]
else:
self.args = ()
self.kw = kw
def __call__(self, context, request):
args = (context, request) + self.args
instance = self._widget_factory(*args)
for name, value in self.kw.items():
setattr(instance, name, value)
return instance
class CustomSequenceWidgetFactory(object):
"""Custom Widget Factory."""
implements(IViewFactory)
def __init__(self, widget_factory, *args, **kw):
self._widget_factory = widget_factory
self.args = args
self.kw = kw
def __call__(self, context, field, request):
args = (context, field, request) + self.args
instance = self._widget_factory(*args)
for name, value in self.kw.items():
setattr(instance, name, value)
return instance