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
|
The CheckBox Matrix Widget
==========================
A custom widget to display checkboxes in multiple columns.
>>> from lp.app.widgets.itemswidgets import CheckBoxMatrixWidget
This widget is created to allow many options to be displayed in a single
page.
>>> from BeautifulSoup import BeautifulSoup
>>> from canonical.launchpad.webapp.servers import LaunchpadTestRequest
>>> from lp.registry.interfaces.product import (
... IProduct,
... IProductSet,
... License,
... )
>>> product = getUtility(IProductSet).get(1)
>>> licenses_field = IProduct['licenses'].bind(product)
>>> vtype = licenses_field.value_type
>>> request = LaunchpadTestRequest()
>>> matrix_widget = CheckBoxMatrixWidget(licenses_field, vtype, request)
Since we didn't provided a value in the request, the form value will be
empty:
>>> matrix_widget._getFormValue()
[]
If we pass in a value via the request, we'll be able to get the licenses
as a set from _getFormValue() or getInputValue():
>>> request = LaunchpadTestRequest(
... form={'field.licenses': ['GNU_LGPL_V2_1', 'GNU_GPL_V2']})
>>> matrix_widget = CheckBoxMatrixWidget(licenses_field, vtype, request)
>>> matrix_widget._getFormValue()
set([<...License.GNU_GPL_V2...>, <...License.GNU_LGPL_V2_1...>])
>>> matrix_widget.getInputValue()
set([<...License.GNU_GPL_V2...>, <...License.GNU_LGPL_V2_1...>])
It should render as many rows as are specified by the column_count attribute.
>>> import math
>>> matrix_widget.column_count = 4
>>> all_licenses = [token.value for token in License]
>>> html = matrix_widget.renderValue(all_licenses)
>>> soup = BeautifulSoup(html)
>>> row_count = math.ceil(len(License) / float(matrix_widget.column_count))
>>> len(soup.table('tr')) == row_count
True
>>> len(soup.table.tr('td'))
4
All the checkboxes should be checked since we passed in all the licenses
to renderValue().
>>> u'checked' in dict(soup.table.tr.td.input.attrs)
True
All the checkboxes should be unchecked if we pass in zero licenses
to renderValue().
>>> html = matrix_widget.renderValue([])
>>> soup = BeautifulSoup(html)
>>> u'checked' in dict(soup.table.tr.td.input.attrs)
False
|