~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Nevow/examples/most_basic/most_basic.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from zope.interface import implements
2
 
 
3
 
from nevow import inevow
4
 
 
5
 
##
6
 
## How does a request come to the Page?
7
 
##
8
 
## or How to use Nevow without all the fancy automations
9
 
##
10
 
 
11
 
# This is a simple Root page object, the inevow.IResource interface
12
 
# tells us that it must implement 2 methods:
13
 
# locateChild and renderHTTP.
14
 
# locateChild is used to find children of the current page, it must return a 
15
 
# tuple of (page, remaining_segments)
16
 
# if there is no page, and you want to display a 404 page, you will need to return
17
 
# a None, () tuple.
18
 
class Root(object):
19
 
    implements(inevow.IResource)
20
 
 
21
 
    def locateChild(self, ctx, segments):
22
 
        # This locateChild is 'stupid' since it can only work if the tree of
23
 
        # pages is static. Anyway it will work for our simple example
24
 
        if segments[0] == '':
25
 
            # If the server is looking for the root page segments will be ('',)
26
 
            # then renderHTTP will be called on self
27
 
            return self, ()
28
 
        elif segments[0] == 'foo':
29
 
            # Now we received a request whose segments had in the first position
30
 
            # the string foo
31
 
            # like http://example.org/foo/baz/ -> ('foo', 'baz')
32
 
            # after the page has been located we return it with the remaining segments
33
 
            # ('baz')
34
 
            return self.foo, segments[1:]
35
 
        else:
36
 
            return None, ()
37
 
        
38
 
    def renderHTTP(self, ctx):
39
 
        # When the server needs to return a response to the request it will call
40
 
        # the renderHTTP method that will return a string of what needs to be sent.
41
 
        return """<html><body>Hello world!<br />
42
 
        <a href="./foo" id="foo">foo</a></body></html>
43
 
"""
44
 
 
45
 
class Foo(object):
46
 
    implements(inevow.IResource)
47
 
    
48
 
    def locateChild(self, ctx, segments):
49
 
        # segments is the remaining segments returned by the root locateChild
50
 
        # see segments[1:]
51
 
        if segments[0] == 'baz':
52
 
            return self.baz, segments[1:]
53
 
        else:
54
 
            return None, ()
55
 
    
56
 
    def renderHTTP(self, ctx):
57
 
        return """<html><body><h1 id="heading">You are in Foo</h1>
58
 
        <a href="./foo/baz" id="baz">baz</a></body></html>
59
 
"""
60
 
 
61
 
class Baz(object):
62
 
    implements(inevow.IResource)
63
 
    def locateChild(self, ctx, segments):
64
 
        return None, ()
65
 
    def renderHTTP(self, ctx):
66
 
        return '<html><body><h1 id="heading">You are in Baz</h1></body></html>'
67
 
 
68
 
# We are adding children to the pages.
69
 
# This could also happen inside the class.
70
 
root = Root()
71
 
root.foo = Foo()
72
 
root.foo.baz = Baz()