[Twisted-Python] Deferred problem

vicky lupien vlupien at drummonddesigns.com
Tue Oct 28 09:28:52 EST 2003


I'm trying to construct a new class for authentification by a database.
I used the class FilePasswordDB in checkers to build my own class
DatabaseAuthentification.  
I think my logic is alright but I get an Deferred error.  
I tested my query in an other page and I know that's working fine.  
The problem is that I don't know how I can make my program wait for the
result of my query (the query is very short).  
I tried to use defer.succeed but it doesn't seem to work.
 
Here's my code that I'm working on:
 
class DatabaseIdentification:
    
    __implements__ = (ICredentialsChecker,)
 
    def __init__(self, dbpool):
        self.dbpool = dbpool
        self.credentialInterfaces = (credentials.IUsernamePassword,)
 
    def _cbPasswordMatch(self, matched, username):
        if matched:
            return username
        else:
            return failure.Failure(error.UnauthorizedLogin())
 
    def getUser(self, username):
        sql = """SELECT * FROM users WHERE name='%s'""" %username
        results = self.dbpool.runQuery(sql)   ## the deferred result
        if results is not None:
            return results[0], results[1]
        raise KeyError(username)
 
    # same code as FilePasswordDB
    def requestAvatarId(self, c):
        try:
            u, p = self.getUser(c.username)
        except KeyError:
            return failure.Failure(error.UnauthorizedLogin())
        else:
            if components.implements(c,
credentials.IUsernameHashedPassword):
                h = self.hash(c.username, c.password, p)
                if h == p:
                    return u
                return failure.Failure(error.UnauthorizedLogin())
            else:
                return defer.maybeDeferred(c.checkPassword, p
                    ).addCallback(self._cbPasswordMatch, u)
            return failure.Failure(error.UnauthorizedLogin())
        
 
and here's the function where a call DatabaseAuthentification:
 
def createResource():
    dbpool = database.DataBase().getConnection()
    return simpleguard.guardResource(
        RootPage(dbpool=dbpool),
        #[checkers.DatabaseIdentification(sibpath(__file__,
"passwords.txt"))],
        [checkers.DatabaseIdentification(dbpool=dbpool)],
        callback=callback)    
 
THE ERROR I GET:
 
C:\PROGRA~1\Python22\lib\site-packages\twisted\cred\checkers.py, line
237 in getUser
235    results = self.dbpool.runQuery(sql)
236    if results is not None:
237      return results[0], results[1]
238    raise KeyError(username)
Locals 

username
Vicky

self
<twisted.cred.checkers.DatabaseIdentification instance at 0x007C4AA0>

results
<Deferred at 0xfe6af0>

sql
'"SELECT * FROM users WHERE name='vicky'
exceptions.AttributeError: Deferred instance has no attribute
'__getitem__'
 
I've been working for a long time on this problem, help would be really
appreciate,
 
Vicky
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20031028/8ae3d287/attachment.htm 


More information about the Twisted-Python mailing list