children.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 » children.py
"""
Simple example of how child resources are located.
"""

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


class ChildPage(rend.Page):

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

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

    def render_name(self, context, data):
        return self.name

    docFactory = loaders.stan(
        T.html[
            T.body[
                T.h1['ChildPage'],
                T.p['My name is: ', T.span(id="name")[render_name]],
                T.p[
                    'I have child too: ',
                    T.a(id="child", href=url.here.child('childOfChild'))['my child']
                    ],
                ]
            ]
        )


class ChildOfChildPage(rend.Page):

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

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

    docFactory = loaders.stan(
        T.html[
            T.body[
                T.h1['ChildOfChildPage'],
                T.p['My parent is the ChildPage called: ', T.span(id="parentName")[render_parentName]]
                ]
            ]
        )


class RootPage(rend.Page):

    addSlash = True

    # 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
        # WARNING: This 3 lines work well until you use formless in this page
        # because formless will make locateChild return only one return value
        # (a deferred) on which you should add a callback that accepts a resource and 
        # an empty tuple that represents no remaining segments.
        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)), []
    
    docFactory = loaders.stan(
        T.html[
            T.body[
                T.h1['RootPage'],
                T.p['Fixed name, singleton resource: ', T.a(id="foo", href=url.here.child('foo'))['foo']],
                T.p['Fixed name, created per-request: ', T.a(id="bar", href=url.here.child('bar'))['bar']],
                T.p[
                    'Dynamically named resources, located via childFactory: ',
                    [(T.a(id=("d", n), href=url.here.child(n))[n],' ') for n in ['1', '2', '3']]
                    ],
                T.p[
                    'Dynamically named resources, located via locateChild: ',
                    [(T.a(id=("d", n), href=url.here.child(n))[n],' ') for n in ['4', '5', '6/7']]
                    ],
                ]
            ]
        )

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