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
|
StrippedTextLine field
======================
The StrippedTextLine field strips the leading and trailing text from the
set value.
>>> from lp.services.fields import StrippedTextLine
>>> non_required_field = StrippedTextLine(
... __name__='field', title=u'Title', required=False)
>>> class Thing:
... def __init__(self, field):
... self.field = field
>>> thing = Thing('abc')
>>> non_required_field.set(thing, ' egf ')
>>> non_required_field.get(thing)
'egf'
None is an accepted field value.
>>> non_required_field.set(thing, None)
>>> print non_required_field.get(thing)
None
StrippedTextLine Widget
-----------------------
This custom widget is used to strip leading and trailing whitespaces.
>>> from canonical.launchpad.webapp.servers import LaunchpadTestRequest
>>> from lp.app.widgets.textwidgets import StrippedTextWidget
>>> from lp.bugs.interfaces.bugtracker import IRemoteBug
We pass a string with leading and trailing whitespaces to the widget
>>> field = IRemoteBug['remotebug']
>>> request = LaunchpadTestRequest(
... form={'field.remotebug':' 123456 '})
>>> widget = StrippedTextWidget(field, request)
And check that the leading and trailing whitespaces were correctly stripped.
>>> widget.getInputValue()
u'123456'
If only whitespace is provided, the widget acts like no input was
provided.
>>> non_required_field.missing_value is None
True
>>> request = LaunchpadTestRequest(form={'field.field':' \n '})
>>> widget = StrippedTextWidget(non_required_field, request)
>>> widget.getInputValue() is None
True
>>> required_field = StrippedTextLine(
... __name__='field', title=u'Title', required=True)
>>> request = LaunchpadTestRequest(form={'field.field':' \n '})
>>> widget = StrippedTextWidget(required_field, request)
>>> widget.getInputValue()
Traceback (most recent call last):
...
WidgetInputError: ('field', u'Title', RequiredMissing())
|