tree.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » examples » tree » 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 » tree » tree.py
from zope.interface import implements

from twisted.python.components import registerAdapter

from nevow import loaders,rend,inevow,tags
from formless import annotate,webform
 
class Tree(dict):
    def __init__(self, name, description, *children):
        self.name = name
        self.description = description
        for child in children:
            self.add(child)
    def add(self, child):
        self[child.name] = child
    def __nonzero__(self):
        return True
 
class ITreeEdit(annotate.TypedInterface):
    def setDescription(description=annotate.String()):
        pass
    setDescription = annotate.autocallable(setDescription)
    def deleteChild(name=annotate.String(required=True)):
        pass
    deleteChild = annotate.autocallable(deleteChild, invisible=True)
    def addChild(name=annotate.String(required=True),
                       description=annotate.String()):
        pass
    addChild = annotate.autocallable(addChild)
 
class TreeRenderer(rend.Page):
    implements(ITreeEdit)
    addSlash = True
    docFactory = loaders.htmlstr("""
<html>
<head><title>Tree Editor</title></head>
<body><h1><span nevow:data="description"
                nevow:render="string">Tree Description</span></h1>
<span nevow:render="descriptionForm"/>
<ol nevow:data="children" nevow:render="sequence">
<li nevow:pattern="item"><span nevow:render="childLink"/>
<span nevow:render="childDel"/>
</li>
</ol>
<a href="..">Up</a>
</body>
</html>
    """)
    def setDescription(self, description):
        self.original.description = description
    def addChild(self, name, description):
        self.original.add(Tree(name, description))
    def deleteChild(self, name):
        del self.original[name]
    def data_description(self, context, data):
        return self.original.description
    def data_children(self, context, data):
        return self.original.items()
    def render_childLink(self, context, data):
        return T.a(href='subtree_%s/'%data[0])[data[1].description]
    def childFactory(self, ctx, name):
        if name.startswith('subtree_'):
            return self.original[name[len('subtree_'):]]
    def render_descriptionForm(self, context, data):
        return webform.renderForms()
    def render_childDel(self, context, (name, _)):
        ret = T.form(action="./freeform_post!!deleteChild",
                     enctype="multipart/form-data", method="POST")[
               T.input(type="hidden", name="name", value=name),
               T.input(type="submit", value="Delete")]
        return ret
 
registerAdapter(TreeRenderer, Tree, inevow.IResource)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.