~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Nevow/examples/formpost/formpost2.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- python -*-
2
 
 
3
 
from zope.interface import implements
4
 
 
5
 
from nevow import loaders
6
 
from nevow import rend
7
 
from nevow import tags
8
 
from nevow import inevow
9
 
from nevow import url
10
 
 
11
 
from formless import annotate
12
 
from formless import webform
13
 
 
14
 
from twisted.internet import defer
15
 
 
16
 
 
17
 
#oldChoicesWay = annotate.Choice(choicesAttribute='theChoices') # Doing this gives you a DeprecationWarning now
18
 
 
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"])
24
 
 
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
32
 
 
33
 
 
34
 
class IMyForm(annotate.TypedInterface):
35
 
    foo = annotate.Integer()
36
 
 
37
 
    def bar(baz=annotate.Integer(), 
38
 
        slam=newChoicesWay, ham=deferChoicesWay, radio=radioChoices, custom=customValueToKey):
39
 
        pass
40
 
    bar = annotate.autocallable(bar)
41
 
 
42
 
 
43
 
class Implementation(object):
44
 
    implements(IMyForm)
45
 
 
46
 
    foo = 5
47
 
 
48
 
    def bar(self, baz, slam, ham, radio, custom):
49
 
        return "You called bar! %s %s %s %s %r" % (baz, slam, ham, radio, custom)
50
 
 
51
 
    theChoices = [1, 2, 3]
52
 
 
53
 
 
54
 
class FormPage(rend.Page):
55
 
 
56
 
    addSlash = True
57
 
 
58
 
    child_webform_css = webform.defaultCSS
59
 
 
60
 
    def render_hand(self, ctx, data):
61
 
        hand = inevow.IHand(ctx, None)
62
 
        if hand is not None:
63
 
            return ctx.tag[hand]
64
 
        return ''
65
 
 
66
 
    docFactory = loaders.stan(
67
 
        tags.html[
68
 
            tags.head[
69
 
                tags.link(rel='stylesheet', type='text/css', href=url.here.child('webform_css')),
70
 
                ],
71
 
            tags.body[
72
 
                tags.h3(render=render_hand, style="color: red; font-size: xx-large"),
73
 
                "Hello! Here is a form:",
74
 
 
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'),
80
 
                ],
81
 
            ],
82
 
        )
83