Deferred 101

<[email protected]>
Newsgroups gmane.comp.python.twisted.web
Message-ID <[email protected]>
Hi guys,
 I'm totally new to Twisted although everything I try so far has been a lot of
fun. I have a really simple situation it seems I cannot wrap my head around.
Although the tutorial have been really helpful I cannof find how to fix this
last part.

I created a "login" method that connects with my db through adbapi and
returns a deferred. I'm using it with pyamf and it's working ok. Now I have to
implement the same thing but accept logins through a standard web page. I
followed the Twisted web in 60 seconds tutorial and created a render_POST
method that processes the login method. The only catch is that I'm returning a
mako template page. Ok, finally my problem: I cannot make it return the result
of the deferred: if I debug the response I see a "<Deferred at ...>" but
not the intended message. Actually if I add a callback method that just prints
the response, I can get it (but since that prints the message in the log, it
doesn't help). What am I missing?

Here's some code to help me explain my problem:

class Login(Resource):
    """LogIn post processing"""
    isLeaf = True

    def render_POST(self, request):
        rsp = None
        errc = None
        usr = User()
        username = cgi.escape(request.args['username'][0])
        passwd = cgi.escape(request.args['passwd'][0])
        try:
            # here I get the answer of the process. I would expect the login
            # message but instead I get a deferred repr object.
            rsp = usr.login(request, username, passwd)
        except Exception, e:
            errc = e.args[0]
        # this should return the contents of postproc.mak (a regular file) 
        # but with certain keywords replaced.
        request.write(serve_tpl('postproc.mak',
                **{'status':rsp, 'username':username,
                   'errorcode':errc}))
        request.finish()
        return NOT_DONE_YET

class Userobject):

    def login(self, request, username, password):
        rp = defer.Deferred()

        def cb(rs):
            resp = u'Unidentified reason'
            if len(rs)==0:
                resp = u'No account exists for the given username'
            elif rs[0]['password'!=password:
                resp = u'Password does not match'
            else:
                resp = u'Ok'
            rp.callback(resp)

        def eb(failure): rp.errback(failure)

        d = self.conn_pool.runQuery('select password from user where '
                        'username=?', (username,))
        d.addCallback(cb).addErrback(eb)

        return rp

TIA,
Eduardo Miguez
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.