[Twisted-web] Get access to Avatar from Resource object?

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu Feb 23 13:29:51 EST 2012


On 05:37 pm, jacek99 at gmail.com wrote:
>D'oh!
>That's what happens when you copy code from the web, sorry :-(
>
>So just a follow up to my other question: how do I add my child 
>resources?
>Does my TestRealm need to extend Resource? Is that the recommended 
>approach?

Your TestRealm is responsible for giving out avatars, in this case 
IResource implementations.  If you want to add child resources, they 
need to be added to the IResource implementation that TestRealm is going 
to give out.  I don't remember if you gave the implementation of 
TestRealm anywhere in this thread already, but take this simple 
imaginary version:

    class User(Resource):
        def __init__(self):
            Resource.__init__(self)
            self.addChild("innocuous", InnocuousResource())


    class Administrator(User):
        def __init__(self):
            User.__init__(self)            self.addChild("sensitive", 
SensitiveResource())


    class TestRealmobject):
        ...
        def requestAvatar(self, avatarId, mind, *interfaces):
            ...
            if self.isAdministrator(avatarId):
                avatar = Administrator()
            else:
                avatar = User()

            return IResource, avatar, logout


Put another way, the resources given out by TestRealm are just 
resources.  Treat them as you would treat any other resources.

Jean-Paul



More information about the Twisted-web mailing list