3
from zope.interface import implements
5
from nevow import loaders
8
from nevow import inevow
11
from formless import annotate
12
from formless import webform
14
from twisted.internet import defer
17
#oldChoicesWay = annotate.Choice(choicesAttribute='theChoices') # Doing this gives you a DeprecationWarning now
19
# If you still want to use an attribute or method of some other object, you should use a function as shown below,
20
# but look up IResource(ctx) or IConfigurable(ctx), whichever is more appropriate.
21
newChoicesWay = annotate.Choice(lambda c, d: range(30))
22
deferChoicesWay = annotate.Choice(lambda c, d: defer.succeed(['abcd', 'efgh', 'ijkl']))
23
radioChoices = annotate.Radio(["Old", "Tyme", "Radio"])
25
## An example of using custom valueToKey and keyToValue functions to serialize/deserialize items
26
values = {0: dict(name="Zero", stuff=1234), 1: dict(name="One", stuff=1234), 2: dict(name="Two", stuff=2345435)}
27
customValueToKey = annotate.Choice(
28
[0, 1, 2], # Perhaps these are primary keys in a database
29
stringify=lambda x: values[x]['name'], # Do a database lookup to render a nice label in the ui
30
valueToKey=str, # Convert the primary key to a value suitable for sending across the web
31
keyToValue=lambda x: values[int(x)]) # Do a database lookup to get the actual value to pass to the binding
34
class IMyForm(annotate.TypedInterface):
35
foo = annotate.Integer()
37
def bar(baz=annotate.Integer(),
38
slam=newChoicesWay, ham=deferChoicesWay, radio=radioChoices, custom=customValueToKey):
40
bar = annotate.autocallable(bar)
43
class Implementation(object):
48
def bar(self, baz, slam, ham, radio, custom):
49
return "You called bar! %s %s %s %s %r" % (baz, slam, ham, radio, custom)
51
theChoices = [1, 2, 3]
54
class FormPage(rend.Page):
58
child_webform_css = webform.defaultCSS
60
def render_hand(self, ctx, data):
61
hand = inevow.IHand(ctx, None)
66
docFactory = loaders.stan(
69
tags.link(rel='stylesheet', type='text/css', href=url.here.child('webform_css')),
72
tags.h3(render=render_hand, style="color: red; font-size: xx-large"),
73
"Hello! Here is a form:",
75
# We want to render forms defined by the Implementation instance.
76
# When we pass the Implementation instance to FormPage below,
77
# rend.Page sets it as the .original attribute. To tell webform to render
78
# forms described by this object, we use the configurable name "original".
79
webform.renderForms('original'),