[Twisted-web] Multiple choices

Tommi Virtanen tv at tv.debian.net
Mon Jul 5 06:02:08 MDT 2004


Justin Johnson wrote:
> Here's an example of what I did to get a multiple choice select box. 
> It's been a while since I did this so it could probably use some
> cleanup.  But it is working for me.

Here's a cleaned up and modernized version, with an example.

Please explicitly say you are cool with this getting included
in nevow -- I'd be happy to file the issue.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: multichoice.py
Type: text/x-python
Size: 3141 bytes
Desc: not available
Url : http://pyramid.twistedmatrix.com/pipermail/twisted-web/attachments/20040705/91f35e1b/multichoice.py
-------------- next part --------------
# -*- python -*-
from twisted.application import service, internet
from nevow import compy, rend, inevow, appserver
from formless import webform, annotate, iformless
import multichoice

ANIMALS = ['dogs', 'cats', 'snakes']

class IAnimals(annotate.TypedInterface):
    def remember(self,
                 like=multichoice.MultipleChoice(choices=ANIMALS),
                 hate=multichoice.MultipleChoice(choices=ANIMALS)):
        pass
    remember = annotate.autocallable(remember)

class Animals(object):
    __implements__ = IAnimals
    like = None
    hate = None

    def remember(self, like, hate):
        self.like = like
        self.hate = hate
        print 'REMEMBER', self
        return self

    def __repr__(self):
        return '<%s like=%r hate=%r>' % (
            self.__class__.__name__,
            self.like,
            self.hate,
            )

class MyResource(rend.Page):
    docFactory = rend.htmlstr(
        """\
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:xi="http://nevow.com/ns/nevow/0.1">
  <head>
    <title>Thingies</title>
    <link href="/form_css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <nevow:invisible nevow:render="input"/>
  </body>
</html>
""")
    child_form_css = webform.defaultCSS

    def configurable_(self, context):
        try:
            return context.locate(inevow.IHand)
        except KeyError:
            return Animals()

    def render_input(self, context, data):
        formDefaults = context.locate(iformless.IFormDefaults)
        methodDefaults = formDefaults.getAllDefaults('remember')
        conf = self.locateConfigurable(context, '')
        methodDefaults['like'] = conf.original.like
        methodDefaults['hate'] = conf.original.hate
        print 'RENDER', conf.original
        return webform.renderForms()

site = appserver.NevowSite(MyResource())
application = service.Application("multichoice-example")
webServer = internet.TCPServer(8080, site)
webServer.setServiceParent(application)


More information about the Twisted-web mailing list