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
|
Launchpadform views
===================
The custom_widget accepts arbitrary attribute assignments for the
widget. One that launchpadform utilizes is 'widget_class'. The
widget rendering is wrapped with a <div> using the widget_class, which
can be used for subordinate field indentation, for example.
>>> from zope.app.form.browser import TextWidget
>>> from zope.interface import Interface
>>> from zope.schema import TextLine
>>> from canonical.config import config
>>> from z3c.ptcompat import ViewPageTemplateFile
>>> from lp.app.browser.launchpadform import (
... custom_widget, LaunchpadFormView)
>>> from canonical.launchpad.testing.pages import find_tags_by_class
>>> from canonical.launchpad.webapp.servers import LaunchpadTestRequest
>>> class ITestSchema(Interface):
... displayname = TextLine(title=u"Title")
... nickname = TextLine(title=u"Nickname")
>>> class TestView(LaunchpadFormView):
... page_title = 'Test'
... template = ViewPageTemplateFile(
... config.root + '/lib/lp/app/templates/generic-edit.pt')
... schema = ITestSchema
... custom_widget('nickname', TextWidget,
... widget_class="field subordinate")
>>> login('foo.bar@canonical.com')
>>> person = factory.makePerson()
>>> request = LaunchpadTestRequest()
>>> request.setPrincipal(person)
>>> view = TestView(person, request)
>>> view.initialize()
>>> for tag in find_tags_by_class(view.render(), 'subordinate'):
... print tag
<div class="field subordinate">
<label for="field.nickname">Nickname:</label>
<div>
<input class="textType" id="field.nickname" name="field.nickname" ... />
</div>
</div>
|