Making an adapter traversable

4 Messages Forum Options Options
Permalink
Hedley Roos-2
Making an adapter traversable
Reply Threaded More
Print post
Permalink
Hi all

I'm playing around with schemaextender and it is very useful. I'm
trying to make a method on my adapter traversable, so if I navigate to
http://server/my-adapted-object/foo I want method foo to execute.

I've got this method on the adapter

def foo(self):
  """Callable"""
  return "something"

My configure.zcml has
'provides="zope.traversing.interfaces.ITraversable"' in the adapter
registration. My class does implements(ISchemaExtender, ITraversable),
but it subclass only object and not Traversable.

I keep getting an Unauthorized error. Setting
__allow_access_to_unprotected_subobjects also does nothing.

What is the correct combination to get it working?

Thanks
Hedley

_______________________________________________
Product-Developers mailing list
Product-Developers@...
http://lists.plone.org/mailman/listinfo/product-developers
Martin Aspeli-2
Re: Making an adapter traversable
Reply Threaded More
Print post
Permalink
Hedley Roos wrote:
> Hi all
>
> I'm playing around with schemaextender and it is very useful. I'm
> trying to make a method on my adapter traversable, so if I navigate to
> http://server/my-adapted-object/foo I want method foo to execute.

This isn't really possible without subclassing, though you could
register view 'foo' for the type, and then have that call the method and
do something.

In general, the idea of traversing to methods is a bit fishy. If you
want something to be traversable, you should think views.

> I've got this method on the adapter
>
> def foo(self):
>   """Callable"""
>   return "something"
>
> My configure.zcml has
> 'provides="zope.traversing.interfaces.ITraversable"' in the adapter
> registration. My class does implements(ISchemaExtender, ITraversable),
> but it subclass only object and not Traversable.

This doesn't work. It's the context object that needs to be traversable
(which it is anyway, just not in the way you want it to be).

> I keep getting an Unauthorized error. Setting
> __allow_access_to_unprotected_subobjects also does nothing.

Who knows why, but 'foo' is not an attribute on your object even though
the schema extender adds it to its Schema().

> What is the correct combination to get it working?

Views. ;-)

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book


_______________________________________________
Product-Developers mailing list
Product-Developers@...
http://lists.plone.org/mailman/listinfo/product-developers
Hedley Roos-2
Re: Re: Making an adapter traversable
Reply Threaded More
Print post
Permalink
> Who knows why, but 'foo' is not an attribute on your object even though the
> schema extender adds it to its Schema().

I wasn't being clear in my original post. foo really is just an
arbitrary method on the adapter. I do not include it in fields.

I think traversable did work, because whereas
http://server/my-adapted-object/foo gives me the Unauthorized,
http://server/my-adapted-object/not_existing_thingy gives me a
NotFound (I guess an AttributeError originally).

> Views. ;-)
I thought so...

I really don't need it to be traversable - I was just exploring schemaextender.

Thanks Martin!

_______________________________________________
Product-Developers mailing list
Product-Developers@...
http://lists.plone.org/mailman/listinfo/product-developers
Martin Aspeli-2
Re: Making an adapter traversable
Reply Threaded More
Print post
Permalink
Hedley Roos wrote:
>> Who knows why, but 'foo' is not an attribute on your object even though the
>> schema extender adds it to its Schema().
>
> I wasn't being clear in my original post. foo really is just an
> arbitrary method on the adapter. I do not include it in fields.

Fine. But you can't traverse to the adapter in the first place.

> I think traversable did work, because whereas
> http://server/my-adapted-object/foo gives me the Unauthorized,
> http://server/my-adapted-object/not_existing_thingy gives me a
> NotFound (I guess an AttributeError originally).

I think that's a red herring. Or some horrid __bobo_traverse__ thing in
AT maybe that you really don't want to know about.

>> Views. ;-)
> I thought so...
>
> I really don't need it to be traversable - I was just exploring schemaextender.

Schemaextender doesn't add methods to the original object. It just adds
things to what's returned by object.Schema().

Martin

--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book


_______________________________________________
Product-Developers mailing list
Product-Developers@...
http://lists.plone.org/mailman/listinfo/product-developers