Remembering things in context from a Realm
I have a Realm class that does something akin to the following: class MyRealm: def requestAvatar(self, id, mind, *interfaces): if IResource is in interfaces: user = figureOutWhoTheUserIs(id) resource = getResourceForUser(user) resource.remember(user, ICurrentUser) return (IResource, resource, lambda:None) ...and this doesn't seem to be working with the ubiquitous context. _Should_ it work, or do I need a new way to do something like this? Help is appreciated; I'm trying hard to get back in the loop. :/ -- Alex Levy WWW: http://mesozoic.geecs.org/ "Never let your sense of morals prevent you from doing what is right." -- Salvor Hardin, Isaac Asimov's _Foundation_
On Sep 28, 2004, at 12:41 AM, Alex Levy wrote:
I have a Realm class that does something akin to the following:
class MyRealm: def requestAvatar(self, id, mind, *interfaces): if IResource is in interfaces: user = figureOutWhoTheUserIs(id) resource = getResourceForUser(user) resource.remember(user, ICurrentUser) return (IResource, resource, lambda:None)
...and this doesn't seem to be working with the ubiquitous context. _Should_ it work, or do I need a new way to do something like this?
Help is appreciated; I'm trying hard to get back in the loop. :/
You are going to have to put user as an attribute on resource, then remember it in the context in an overridden locateChild. dp
On Tue, 2004-09-28 at 14:48, Donovan Preston wrote:
On Sep 28, 2004, at 12:41 AM, Alex Levy wrote:
I have a Realm class that does something akin to the following:
class MyRealm: def requestAvatar(self, id, mind, *interfaces): if IResource is in interfaces: user = figureOutWhoTheUserIs(id) resource = getResourceForUser(user) resource.remember(user, ICurrentUser) return (IResource, resource, lambda:None)
...and this doesn't seem to be working with the ubiquitous context. _Should_ it work, or do I need a new way to do something like this?
Help is appreciated; I'm trying hard to get back in the loop. :/
You are going to have to put user as an attribute on resource, then remember it in the context in an overridden locateChild.
Or, you can create a IResource wrapper that is used like this from requestAvatar: return (IResource, RememberWrapper(resource, user), lambda:None) and looks like this (untested): class RememberWrapper: __implements__ = inevow.IResource, def __init__(self, resource, user): self.resource = resource self.user = user def locateChild(self, ctx, segments): ctx.remember(self.user, ICurrentUser) return self.resource, segments def renderHTTP(self, ctx): ctx.remember(self.user, ICurrentUser) return self.resource.renderHTTP(ctx) This avoids setting attributes on the resource which means you can reuse a single *real* resource object if that makes sense to your application. Hope this helps. Cheers, Matt -- __ / \__ Matt Goodall, Pollenation Internet Ltd \__/ \ w: http://www.pollenation.net __/ \__/ e: matt@pollenation.net / \__/ \ t: +44 (0)113 2252500 \__/ \__/ / \ Any views expressed are my own and do not necessarily \__/ reflect the views of my employer.
On Tue, 2004-09-28 at 14:48, Donovan Preston wrote:
On Sep 28, 2004, at 12:41 AM, Alex Levy wrote:
I have a Realm class that does something akin to the following:
class MyRealm: def requestAvatar(self, id, mind, *interfaces): if IResource is in interfaces: user = figureOutWhoTheUserIs(id) resource = getResourceForUser(user) resource.remember(user, ICurrentUser) return (IResource, resource, lambda:None)
...and this doesn't seem to be working with the ubiquitous context. _Should_ it work, or do I need a new way to do something like this?
Help is appreciated; I'm trying hard to get back in the loop. :/
You are going to have to put user as an attribute on resource, then remember it in the context in an overridden locateChild.
Or, you can create a IResource wrapper that is used like this from requestAvatar: return (IResource, RememberWrapper(resource, user), lambda:None) and looks like this (untested): class RememberWrapper: __implements__ = inevow.IResource, def __init__(self, resource, user): self.resource = resource self.user = user def locateChild(self, ctx, segments): ctx.remember(self.user, ICurrentUser) return self.resource, segments def renderHTTP(self, ctx): ctx.remember(self.user, ICurrentUser) return self.resource.renderHTTP(ctx) This avoids setting attributes on the resource which means you can reuse a single *real* resource object if that makes sense to your application. Hope this helps. Cheers, Matt -- __ / \__ Matt Goodall, Pollenation Internet Ltd \__/ \ w: http://www.pollenation.net __/ \__/ e: matt@pollenation.net / \__/ \ t: +44 (0)113 2252500 \__/ \__/ / \ Any views expressed are my own and do not necessarily \__/ reflect the views of my employer.
On 9/28/2004, "Matt Goodall" <matt@pollenation.net> wrote:
On Tue, 2004-09-28 at 14:48, Donovan Preston wrote:
On Sep 28, 2004, at 12:41 AM, Alex Levy wrote:
I have a Realm class that does something akin to the following:
class MyRealm: def requestAvatar(self, id, mind, *interfaces): if IResource is in interfaces: user = figureOutWhoTheUserIs(id) resource = getResourceForUser(user) resource.remember(user, ICurrentUser) return (IResource, resource, lambda:None)
...and this doesn't seem to be working with the ubiquitous context. _Should_ it work, or do I need a new way to do something like this?
Help is appreciated; I'm trying hard to get back in the loop. :/
You are going to have to put user as an attribute on resource, then remember it in the context in an overridden locateChild.
Or, you can create a IResource wrapper that is used like this from requestAvatar:
return (IResource, RememberWrapper(resource, user), lambda:None)
and looks like this (untested):
class RememberWrapper: __implements__ = inevow.IResource,
def __init__(self, resource, user): self.resource = resource self.user = user
def locateChild(self, ctx, segments): ctx.remember(self.user, ICurrentUser) return self.resource, segments
def renderHTTP(self, ctx): ctx.remember(self.user, ICurrentUser) return self.resource.renderHTTP(ctx)
This avoids setting attributes on the resource which means you can reuse a single *real* resource object if that makes sense to your application.
This is what I'd expected to do -- instead of just returning a regular resource, return an EverythingLivesInsideHereResource (with a more suitable name) that contains everything the site needs to pass down to the resources. Still, passing the context through the entire resource chain will let me cut out a _lot_ of cruft from [Paya]go. Kudos! :)
On Sep 28, 2004, at 10:20 AM, Alex Levy wrote:
This is what I'd expected to do -- instead of just returning a regular resource, return an EverythingLivesInsideHereResource (with a more suitable name) that contains everything the site needs to pass down to the resources.
Still, passing the context through the entire resource chain will let me cut out a _lot_ of cruft from [Paya]go. Kudos! :)
That was the point and the expected result. Glad it is going to work as desired ;-) Perhaps that resource should be nevow proper? Riffing off what mg did: class RememberWrapper: __implements__ = inevow.IResource, def __init__(self, resource, remember): self.resource = resource self.remember = remember def locateChild(self, ctx, segments): for interface, adapter in self.remember: ctx.remember(adapter, interface) return self.resource, segments def renderHTTP(self, ctx): for interface, adapter in self.remember: ctx.remember(adapter, interface) return self.resource.renderHTTP(ctx) dp
participants (3)
-
Alex Levy
-
Donovan Preston
-
Matt Goodall