[Twisted-web] Nevow IResource trouble

Remi Cool mailinglists at smartology.nl
Wed Nov 16 08:49:46 MST 2005


Hello, after batteling with XMLRPC and getting nevow to put something on
the screen, I got stuck implementing a livepage and webform example.

After changing the implements to inevow.IResource in httpResource, I got
a message about a missing locateChild method (is that about the same as
the getChild method in a twisted web resource?) ... I've created one,
but I'm not shure if it's correct.

Bottomline is I can't get it to work, not from the code and not from a
resource file ... what's wrong with this code?


Here's my code:
-----------------------------------------------------------------------------------------------

class ISimpleMethod(TypedInterface):
    def simple(self, name=String(description="Your name."),
age=Integer(description="Your age.")):
        """Simple
       
        Please enter your name and age.
        """

class Implementation(object):
   
    implements(ISimpleMethod)

    def simple(self, name, age):
        print "Hello, %s, who is %s" % (name, age)


class WebForm(rend.Page):
   
    print 'WEBFORMS:', webform.renderForms('original')
   
    docFactory = loaders.stan(tags.html[
                                tags.body[
                                    tags.h1["Here is the form:"],
                                        webform.renderForms()]])


class httpResource(resource.Resource):

    implements(inevow.IResource)
   
    def __init__(self, service):
        resource.Resource.__init__(self)
        self.service = service
        ##self.putChild('RPC2', Example(self.service))
        self.putChild('formtest', WebForm(Implementation()))
           
        rt = static.File("rpy")
        rt.ignoreExt(".rpy")
        rt.processors = {'.rpy': script.ResourceScript}
        self.putChild('rpy', rt)
       
       
    def render(self, request):
        self._clientIP = request.getClientIP()
        return resource.Resource.render(self, request)

    def render_GET(self, request):
        """Process HTTP GET Requests."""
        print 'GET: request ->', request
        return '<html><body><h3>Not Implemented</h3></body></html>'
       
    def getChild(self, path, request):
        """This method handles http calls"""
        print 'getChild path:', path
        print 'getChild request:', request
        if path=="":
            return httpResource(self.service)
        else:
            return httpResource(self.service)

    def locateChild(self, ctx, segments):
        """"""
        print 'CTX:', ctx
        print 'SEGMENTS:', segments
        return self, segments

components.registerAdapter(httpResource, IowwService, inevow.IResource)


def main():
    """"""
    application = service.Application('OWW', uid=1000, gid=1000)
    s = owwService()
    serviceCollection = service.IServiceCollection(application)
    internet.TCPServer(7080,
appserver.NevowSite(s)).setServiceParent(serviceCollection)
    serviceCollection.startService()
    reactor.run()

if __name__ == '__main__':
    main()
   
----------------------------------------------------------------------------
livepage.rpy
----------------------------------------------------------------------------
from nevow.livepage import handler
from nevow import rend, tags, loaders


def greeter(client, nodeName):
    client.alert("Greetings. You clicked the %s node." % nodeName)

# Any string arguments after the event handler function will be evaluated
# as JavaScript in the context of the web browser and results passed to the
# Python event handler
handler = handler(greeter, 'node.name')

class Live(rend.Page):
    docFactory = loaders.stan(
    tags.html[
    tags.body[
        tags.ol[
            tags.li(onclick=handler, name="one")["One"],
            tags.li(onclick=handler, name="two")["Two"],
            tags.li(onclick=handler, name="three")["Three"]
        ]
    ]
])

resource = Live()

------------------------------------------------------------------
testform.rpy
------------------------------------------------------------------
from formless.annotate import TypedInterface, Integer, String
from nevow import rend, tags, loaders, appserver, inevow
from formless import webform
from zope.interface import Interface, implements

class ISimpleMethod(TypedInterface):
    def simple(self, name=String(description="Your name."),
age=Integer(description="Your age.")):
        """Simple
       
        Please enter your name and age.
        """

class Implementation(object):
   
    implements(ISimpleMethod)

    def simple(self, name, age):
        print "Hello, %s, who is %s" % (name, age)


class WebForm(rend.Page):
   
    print 'WEBFORMS:', webform.renderForms('original')
   
    docFactory = loaders.stan(tags.html[
                                tags.body[
                                    tags.h1["Here is the form:"],
                                        webform.renderForms()]])

resource = WebForm(Implementation())





More information about the Twisted-web mailing list