childrenhtml.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » examples » children » 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 » children » childrenhtml.py
"""
Simple example of how child resources are located.
"""

from twisted.application import internet
from twisted.application import service

from twisted.python import util

from nevow import appserver
from nevow import loaders
from nevow import rend
from nevow import url


class ChildPage(rend.Page):

    docFactory = loaders.xmlfile(util.sibpath(__file__, "childrenhtml_ChildPage.html"))

    def __init__(self, name):
        rend.Page.__init__(self)
        self.name = name

    def child_childOfChild(self, context):
        print "istanzaaa"
        return ChildOfChildPage(self.name)

    def render_name(self, context, data):
        return context.tag[self.name]
    
    def render_link(self, context, data):
        context.fillSlots('childLink', url.here.child('childOfChild'))
        return context.tag


class ChildOfChildPage(rend.Page):

    docFactory = loaders.xmlfile(util.sibpath(__file__, "childrenhtml_ChildOfChildPage.html"))

    def __init__(self, parentName):
        rend.Page.__init__(self)
        self.parentName = parentName

    def render_parentName(self, context, data):
        return context.tag[self.parentName]


class RootPage(rend.Page):

    addSlash = True

    docFactory = loaders.xmlfile(util.sibpath(__file__, "childrenhtml_RootPage.html"))

    # A resource that is always called 'foo' and only needs to be created once
    child_foo = ChildPage('foo')

    def child_bar(self, context):
        """A resource that is always called 'bar' but is created per-request
        """
        return ChildPage('bar')

    def childFactory(self, ctx, name):
        """Create and return a child resource where the name is dynamic
        """
        if name in ['1', '2', '3']:
            return ChildPage(name)

    def locateChild(self, ctx, segments):
        """Create and return a dynamically named child resource if child_ or
        childFactory didn't help. However, this time we get the chance to
        consume multiple path segments (inluding the childOfChild link).
        
        Note: locateChild is actually the main resource location API (see
        inevow.IReource) and it is actually rend.Page's implementation of the
        method that provides the child_ and childFactory functionality.
        """

        # Let parent class have a go first
        child, remainingSegments = rend.Page.locateChild(self, ctx, segments)
        if child:
            return child, remainingSegments

        # Consume all remaining path segments for the name
        return ChildPage('/'.join(segments)), []


application = service.Application('children')
internet.TCPServer(8080, appserver.NevowSite(RootPage())).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.