formpost2.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » examples » formpost » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » examples » formpost » formpost2.py
# -*- python -*-

from zope.interface import implements

from nevow import loaders
from nevow import rend
from nevow import tags
from nevow import inevow
from nevow import url

from formless import annotate
from formless import webform

from twisted.internet import defer


#oldChoicesWay = annotate.Choice(choicesAttribute='theChoices') # Doing this gives you a DeprecationWarning now

# If you still want to use an attribute or method of some other object, you should use a function as shown below,
# but look up IResource(ctx) or IConfigurable(ctx), whichever is more appropriate.
newChoicesWay = annotate.Choice(lambda c, d: range(30))
deferChoicesWay = annotate.Choice(lambda c, d: defer.succeed(['abcd', 'efgh', 'ijkl']))
radioChoices = annotate.Radio(["Old", "Tyme", "Radio"])

## An example of using custom valueToKey and keyToValue functions to serialize/deserialize items
values = {0: dict(name="Zero", stuff=1234), 1: dict(name="One", stuff=1234), 2: dict(name="Two", stuff=2345435)}
customValueToKey = annotate.Choice(
    [0, 1, 2], # Perhaps these are primary keys in a database
    stringify=lambda x: values[x]['name'], # Do a database lookup to render a nice label in the ui
    valueToKey=str,                                  # Convert the primary key to a value suitable for sending across the web
    keyToValue=lambda x: values[int(x)]) # Do a database lookup to get the actual value to pass to the binding


class IMyForm(annotate.TypedInterface):
    foo = annotate.Integer()

    def bar(baz=annotate.Integer(), 
        slam=newChoicesWay, ham=deferChoicesWay, radio=radioChoices, custom=customValueToKey):
        pass
    bar = annotate.autocallable(bar)


class Implementation(object):
    implements(IMyForm)

    foo = 5

    def bar(self, baz, slam, ham, radio, custom):
        return "You called bar! %s %s %s %s %r" % (baz, slam, ham, radio, custom)

    theChoices = [1, 2, 3]


class FormPage(rend.Page):

    addSlash = True

    child_webform_css = webform.defaultCSS

    def render_hand(self, ctx, data):
        hand = inevow.IHand(ctx, None)
        if hand is not None:
            return ctx.tag[hand]
        return ''

    docFactory = loaders.stan(
        tags.html[
            tags.head[
                tags.link(rel='stylesheet', type='text/css', href=url.here.child('webform_css')),
                ],
            tags.body[
                tags.h3(render=render_hand, style="color: red; font-size: xx-large"),
                "Hello! Here is a form:",

                # We want to render forms defined by the Implementation instance.
                # When we pass the Implementation instance to FormPage below,
                # rend.Page sets it as the .original attribute. To tell webform to render
                # forms described by this object, we use the configurable name "original".
                webform.renderForms('original'),
                ],
            ],
        )

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.