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
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
import datetime
import re
import pytz
from z3c.ptcompat import ViewPageTemplateFile
from zope.app.form.browser.textwidgets import (
TextAreaWidget,
TextWidget,
)
from zope.app.form.interfaces import ConversionError
from zope.datetime import (
DateTimeError,
parse,
)
from lp.app.errors import UnexpectedFormData
class StrippedTextWidget(TextWidget):
"""A widget that strips leading and trailing whitespaces."""
def _toFieldValue(self, input):
return TextWidget._toFieldValue(self, input.strip())
class LowerCaseTextWidget(StrippedTextWidget):
"""A widget that converts text to lower case."""
cssClass = 'lowerCaseText'
def _toFieldValue(self, input):
return StrippedTextWidget._toFieldValue(self, input.lower())
class TokensTextWidget(StrippedTextWidget):
"""A widget that normalises the space between words.
Punctuation is removed, and extra whitespace is stripped.
"""
def _toFieldValue(self, input):
"""See `SimpleInputWidget`.
Accept only alphanumeric characters and '-'. Everything
else is replaced with a single space.
"""
normalised_text = re.sub(r'[^\w-]+', ' ', input)
return super(TokensTextWidget, self)._toFieldValue(normalised_text)
class NoneableTextWidget(StrippedTextWidget):
"""A widget that that is None if it's value is empty or whitespace."""
def _toFieldValue(self, input):
value = super(NoneableTextWidget, self)._toFieldValue(input)
if value == '':
return None
else:
return value
class LocalDateTimeWidget(TextWidget):
"""A datetime widget that uses a particular time zone."""
timeZoneName = 'UTC'
def _toFieldValue(self, input):
"""Convert a string to a datetime value.
>>> from zope.publisher.browser import TestRequest
>>> from zope.schema import Field
>>> field = Field(__name__='foo', title=u'Foo')
>>> widget = LocalDateTimeWidget(field, TestRequest())
The widget converts an empty string to the missing value:
>>> widget._toFieldValue('') == field.missing_value
True
By default, the date is interpreted as UTC:
>>> print widget._toFieldValue('2006-01-01 12:00:00')
2006-01-01 12:00:00+00:00
But it will handle other time zones:
>>> widget.timeZoneName = 'Australia/Perth'
>>> print widget._toFieldValue('2006-01-01 12:00:00')
2006-01-01 12:00:00+08:00
Invalid dates result in a ConversionError:
>>> print widget._toFieldValue('not a date') #doctest: +ELLIPSIS
Traceback (most recent call last):
...
ConversionError: ('Invalid date value', ...)
"""
if input == self._missing:
return self.context.missing_value
try:
year, month, day, hour, minute, second, dummy_tz = parse(input)
second, micro = divmod(second, 1.0)
micro = round(micro * 1000000)
dt = datetime.datetime(year, month, day,
hour, minute, int(second), int(micro))
except (DateTimeError, ValueError, IndexError), v:
raise ConversionError('Invalid date value', v)
tz = pytz.timezone(self.timeZoneName)
return tz.localize(dt)
def _toFormValue(self, value):
"""Convert a date to its string representation.
>>> from zope.publisher.browser import TestRequest
>>> from zope.schema import Field
>>> field = Field(__name__='foo', title=u'Foo')
>>> widget = LocalDateTimeWidget(field, TestRequest())
The 'missing' value is converted to an empty string:
>>> widget._toFormValue(field.missing_value)
u''
Dates are displayed without an associated time zone:
>>> dt = datetime.datetime(2006, 1, 1, 12, 0, 0,
... tzinfo=pytz.timezone('UTC'))
>>> widget._toFormValue(dt)
'2006-01-01 12:00:00'
The date value will be converted to the widget's time zone
before being displayed:
>>> widget.timeZoneName = 'Australia/Perth'
>>> widget._toFormValue(dt)
'2006-01-01 20:00:00'
"""
if value == self.context.missing_value:
return self._missing
tz = pytz.timezone(self.timeZoneName)
return value.astimezone(tz).strftime('%Y-%m-%d %H:%M:%S')
class URIWidget(TextWidget):
"""A widget that represents a URI."""
displayWidth = 44
cssClass = 'urlTextType'
def __init__(self, field, request):
super(URIWidget, self).__init__(field, request)
self.field = field
def _toFieldValue(self, input):
if isinstance(input, list):
raise UnexpectedFormData('Only a single value is expected')
return TextWidget._toFieldValue(self, input)
class URIComponentWidget(LowerCaseTextWidget):
"""A text input widget that looks like a URL path component entry."""
template = ViewPageTemplateFile('templates/uri-component.pt')
read_only = False
def __call__(self):
return self.template()
@property
def base_url(self):
raise NotImplementedError()
@property
def current_name(self):
return self._getFormValue().lower()
@property
def widget_type(self):
if self.read_only:
return 'hidden'
else:
return 'text'
class DelimitedListWidget(TextAreaWidget):
"""A widget that represents a list as whitespace-delimited text.
The delimiting methods can be easily overridden to work with
comma, semi-colon, or other delimiters.
"""
def __init__(self, field, value_type, request):
# We don't use value_type.
super(DelimitedListWidget, self).__init__(field, request)
# The default splitting function, which splits on
# white-space. Subclasses can override this if different
# delimiting rules are needed.
split = staticmethod(unicode.split)
# The default joining function, which simply separates each list
# item with a newline. Subclasses can override this if different
# delimiters are needed.
join = staticmethod(u'\n'.join)
def _toFormValue(self, value):
"""Converts a list to a newline separated string.
>>> from zope.publisher.browser import TestRequest
>>> from zope.schema import Field
>>> field = Field(__name__='foo', title=u'Foo')
>>> widget = DelimitedListWidget(field, None, TestRequest())
The 'missing' value is converted to an empty string:
>>> widget._toFormValue(field.missing_value)
u''
By default, lists are displayed one item on a line:
>>> names = ['fred', 'bob', 'harry']
>>> widget._toFormValue(names)
u'fred\\r\\nbob\\r\\nharry'
"""
if value == self.context.missing_value:
value = self._missing
elif value is None:
value = self._missing
else:
value = self.join(value)
return super(DelimitedListWidget, self)._toFormValue(value)
def _toFieldValue(self, value):
"""Convert the input string into a list.
>>> from zope.publisher.browser import TestRequest
>>> from zope.schema import Field
>>> field = Field(__name__='foo', title=u'Foo')
>>> widget = DelimitedListWidget(field, None, TestRequest())
The widget converts an empty string to the missing value:
>>> widget._toFieldValue('') == field.missing_value
True
By default, lists are split by whitespace:
>>> print widget._toFieldValue(u'fred\\nbob harry')
[u'fred', u'bob', u'harry']
"""
value = super(
DelimitedListWidget, self)._toFieldValue(value)
if value == self.context.missing_value:
return value
else:
return self.split(value)
class TitleWidget(StrippedTextWidget):
"""A launchpad title widget; a little wider than a normal Textline."""
displayWidth = 44
class SummaryWidget(TextAreaWidget):
"""A widget to capture a summary."""
width = 44
height = 3
class DescriptionWidget(TextAreaWidget):
"""A widget to capture a description."""
width = 44
height = 5
class NoneableDescriptionWidget(DescriptionWidget):
"""A widget that is None if it's value is empty or whitespace.."""
def _toFieldValue(self, input):
value = super(
NoneableDescriptionWidget, self)._toFieldValue(input.strip())
if value == '':
return None
else:
return value
class WhiteboardWidget(TextAreaWidget):
"""A widget to capture a whiteboard."""
width = 44
height = 5
|