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
|
LaunchpadRadioWidget
====================
There are two Launchpad radio widgets, one that shows descriptions,
and one that doesn't.
>>> from lp.app.widgets.itemswidgets import (
... LaunchpadRadioWidget, LaunchpadRadioWidgetWithDescription)
The LaunchpadRadioWidget is mostly used to display items from
an enumerated type.
>>> from BeautifulSoup import BeautifulSoup
>>> from canonical.launchpad.webapp.servers import LaunchpadTestRequest
>>> from lp.code.interfaces.branch import IBranch
>>> branch = factory.makeAnyBranch()
>>> branch_type_field = IBranch['branch_type'].bind(branch)
>>> request = LaunchpadTestRequest()
>>> radio_widget = LaunchpadRadioWidget(
... branch_type_field, branch_type_field.vocabulary, request)
>>> radio_widget.setRenderedValue(branch_type_field.vocabulary.HOSTED)
The widget is rendered as a collection of labels with the radio
buttons inside.
>>> html = BeautifulSoup(radio_widget())
>>> for label in html.findAll('label'):
... print label.renderContents()
<input class="radioType" checked="checked" id="field.branch_type.0"
name="field.branch_type" type="radio" value="HOSTED" /> Hosted
<input class="radioType" id="field.branch_type.1" name="field.branch_type"
type="radio" value="MIRRORED" /> Mirrored
<input class="radioType" id="field.branch_type.2" name="field.branch_type"
type="radio" value="IMPORTED" /> Imported
<input class="radioType" id="field.branch_type.3" name="field.branch_type"
type="radio" value="REMOTE" /> Remote
LaunchpadRadioWidgetWithDescription
-----------------------------------
The LaunchpadRadioWidgetWithDescription widget renders the descriptions
along with the titles from the enumerated type vocabulary.
>>> radio_widget = LaunchpadRadioWidgetWithDescription(
... branch_type_field, branch_type_field.vocabulary, request)
>>> radio_widget.setRenderedValue(branch_type_field.vocabulary.HOSTED)
The widget is rendered in a table with the descriptions lined up
under the labels. The labels are rendered next to the radio buttons,
in a different table cell, and use the 'for' attribute of the label
to associate the label with the radio button input.
>>> print radio_widget()
<table class="radio-button-widget"><tr>
<td rowspan="2"><input class="radioType" checked="checked"
id="field.branch_type.0" name="field.branch_type" type="radio"
value="HOSTED" /></td>
<td><label for="field.branch_type.0">Hosted</label></td>
</tr>
<tr>
<td class="formHelp">Launchpad is the primary location of this branch.
</td>
</tr>
<tr>
<td rowspan="2"><input class="radioType" id="field.branch_type.1"
name="field.branch_type" type="radio" value="MIRRORED" /></td>
<td><label for="field.branch_type.1">Mirrored</label></td>
</tr>
<tr>
<td class="formHelp">Primarily hosted elsewhere and is periodically
mirrored from the external location into Launchpad. </td>
</tr>
<tr>
<td rowspan="2"><input class="radioType" id="field.branch_type.2"
name="field.branch_type" type="radio" value="IMPORTED" /></td>
<td><label for="field.branch_type.2">Imported</label></td>
</tr>
<tr>
<td class="formHelp">Branches that have been imported from an
externally hosted branch in bzr or another VCS and are made available
through Launchpad. </td>
</tr>
<tr>
<td rowspan="2"><input class="radioType" id="field.branch_type.3"
name="field.branch_type" type="radio" value="REMOTE" /></td>
<td><label for="field.branch_type.3">Remote</label></td>
</tr>
<tr>
<td class="formHelp">Registered in Launchpad with an external location,
but is not to be mirrored, nor available through Launchpad. </td>
</tr>
</table>
<input name="field.branch_type-empty-marker" type="hidden" value="1" />
If the enumerated type doesn't have any descriptions, then the extra
rows are not rendered.
>>> from lazr.enum import EnumeratedType, Item
>>> class SomeFruit(EnumeratedType):
... "A choice of fruit."
... APPLE = Item('Apple')
... PEAR = Item('Pear')
... ORANGE = Item('Orange')
>>> radio_widget = LaunchpadRadioWidgetWithDescription(
... branch_type_field, SomeFruit, request)
>>> print radio_widget()
<table class="radio-button-widget"><tr>
<td><input class="radioType" id="field.branch_type.0"
name="field.branch_type" type="radio" value="APPLE" /></td>
<td><label for="field.branch_type.0">Apple</label></td>
</tr>
<tr>
<td><input class="radioType" id="field.branch_type.1"
name="field.branch_type" type="radio" value="PEAR" /></td>
<td><label for="field.branch_type.1">Pear</label></td>
</tr>
<tr>
<td><input class="radioType" id="field.branch_type.2"
name="field.branch_type" type="radio" value="ORANGE" /></td>
<td><label for="field.branch_type.2">Orange</label></td>
</tr>
</table>
<input name="field.branch_type-empty-marker" type="hidden" value="1" />
LaunchpadBooleanRadioWidget
---------------------------
The LaunchpadBooleanRadioWidget renders a boolean field as radio buttons.
This widget is uses the LaunchpadRadioWidget to render the items. The values
are rendered as 'yes' and 'no'; a missing value radio item is not rendered.
>>> from zope.schema import Bool
>>> from lp.app.widgets.itemswidgets import LaunchpadBooleanRadioWidget
>>> field = Bool(
... __name__='sentient',
... title=u"Are you sentient?",
... description=u"Are you human or a bot?",
... required=False, readonly=False, default=True)
>>> class Agent:
... def __init__(self, sentient):
... self.sentient = sentient
>>> agent = Agent(True)
>>> bound_field = field.bind(agent)
>>> radio_widget = LaunchpadBooleanRadioWidget(bound_field, request)
>>> print radio_widget()
<label style="font-weight: normal"><input
class="radioType" checked="checked" id="field.sentient.0"
name="field.sentient" type="radio" value="yes"
/> yes</label><br
/><label style="font-weight: normal"><input
class="radioType" id="field.sentient.1" name="field.sentient"
type="radio" value="no" /> no</label>
<input name="field.sentient-empty-marker" type="hidden" value="1" />
The labels for True and False values can be set using the true_label and
false_label attributes.
>>> radio_widget.true_label = 'I think therefore I am'
>>> radio_widget.false_label = 'I am a turing test'
>>> print radio_widget()
<label style="font-weight: normal"><input
class="radioType" checked="checked" id="field.sentient.0"
name="field.sentient" type="radio" value="yes"
/> I think therefore I am</label><br
/><label style="font-weight: normal"><input
class="radioType" id="field.sentient.1" name="field.sentient"
type="radio" value="no" /> I am a turing test</label>
<input name="field.sentient-empty-marker" type="hidden" value="1" />
|