formbuilder.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » examples » formbuilder » 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 » formbuilder » formbuilder.py
## formbuilder

from zope.interface import implements

from nevow import rend
from nevow import loaders
from nevow import tags
from nevow import util

from formless import annotate
from formless import webform
from formless import configurable

from twisted.python import reflect


class BuilderCore(configurable.Configurable):
    def __init__(self):
        configurable.Configurable.__init__(self, None)
        self.formElements = []

    def getBindingNames(self, ctx):
        return ['form']

    def bind_form(self, ctx):
        return annotate.MethodBinding(
            'action',
            annotate.Method(arguments=self.formElements))

    def action(self, **kw):
        print "ACTION!", kw

    def addElement(self, name, type):
        self.formElements.append(
            annotate.Argument(name, type()))


allTypes = [annotate.String, annotate.Text, annotate.Integer, annotate.Real, annotate.Password]
typeChoice = annotate.Choice(choices=allTypes, valueToKey=reflect.qual, keyToValue=reflect.namedAny, stringify=lambda x: x.__name__)


class IFormBuilder(annotate.TypedInterface):
    def addElement(name=annotate.String(required=True), type=typeChoice):
        """Add Element
        
        Add an element to this form.
        """
        pass
    addElement = annotate.autocallable(addElement)

    def clearForm():
        """Clear Form
        
        Clear this form.
        """
    clearForm = annotate.autocallable(clearForm)
        

class FormBuilder(rend.Page):
    implements(IFormBuilder)
    addSlash = True

    def __init__(self):
        rend.Page.__init__(self)
        self.clearForm()

    def configurable_formBuilder(self, ctx):
        return configurable.TypedInterfaceConfigurable(self)

    def configurable_dynamicForm(self, ctx):
        return self.builderCore

    def addElement(self, name, type):
        self.builderCore.addElement(name, type)

    def clearForm(self):
        self.builderCore = BuilderCore()

    docFactory = loaders.stan(T.html[
    T.head[
        T.title["Form builder!"]],
        T.style(type="text/css")[
            open(util.resource_filename('formless', 'freeform-default.css')).read()],
    T.body[
        T.h1["Welcome to form builder"],
        webform.renderForms('formBuilder'),
        T.h2["Here is your form:"],
        webform.renderForms('dynamicForm')]])


## Startup glue
from nevow import appserver
from twisted.application import service
from twisted.application import internet

application = service.Application('formbuilder')
internet.TCPServer(8080, appserver.NevowSite(FormBuilder())).setServiceParent(application)

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