The difference between getattr and restrictedTraverse

Tim Hicks tim at sitefusion.co.uk
Sun Jun 10 11:51:39 UTC 2007


Tom Lazar wrote:

>> What I'm trying to do now is make it so that methods on my weblog object
>> will return lists of IWeblogEntry-ish objects that know what sort of URL
>> they should have.  That way, I can call IWeblog.getLazyEntries(), which
>> does a catalog search for appropriate IWeblogEntry objects, and then
>> call absolute_url (or getURL) on each of the returned objects and have
>> it return a blog-archive-ish URL rather than where it really lives.
> 
> IMHO this is not a good approach as it blurs the boundary between
> content and representation. why should a (weblog entry) object 'know'
> what its display url is? what if you change the mapping? you would have
> to change your code...

I take your point.  In response to your "why should a (weblog entry)
object 'know' what its display url is?", I'd say "because people expect
it to".  That is, 'absolute_url' is the recognised way of retrieving an
object's url.  In essence, my question to you might be "why should a
developer have to use a special API to get the appropriate url of an
object when there is a perfectly good, and widely-known, zope API method
already?".

Now, as I understand it, the zope3 way has an IAbsoluteURL interface
which presumably gets adapted to.  That probably makes a lot of sense.
But I don't think we are there (yet) in zope2, and I'd like not to
complicate people's lives (if possible).

> if you want to optimize the retrieval of this url via catalog brains,
> why not add a method 'pretty_url' (or somesuch) and add that to the
> catalog? this method would adapt the context to something that 'knows'
> how to map the url and return the 'pretty' url.

The approach needs to work for both catalog brains and the 'real' objects.

> incidentially i'm faced with a very similar problem in a client project
> at the moment (where we have a shop located in e.g. `/shops/superstore`
> but want to present `/shops/europe/germany/berlin/superstore`....)
> 
> also, i'm still interested in quills development ;-)
> 
> so perhaps we can work something out together how to handle this?
> 
> also, this would then make nice fodder for a howto/tutorial. i can
> imagine that this is a sort of problem that many product developers are
> faced with...
> 
> any input appreciated,

>From your objection above, it sounds like you would favour declaring a
view for IWeblogEntry objects (and their catalog brains) that provides,
say, a getArchiveURL method.  That's what Martijn (Pieters) was suggesting.

Taking the approach that I have pursued at
<http://dev.plone.org/collective/browser/Quills/branches/maurits-traversal/traversalfaker.py>,
I can have code that looks like::

  <div tal:repeat="entry weblog/getLazyEntries">
    <a tal:attributes="href entry/getURL"
       tal:content="entry/Title">title</a>
  </div>

This compares to what I envisage the view approach looking like::

  <div tal:repeat="brain weblog/getLazyEntries">
    <a tal:define="weblogentryview brain/@@weblogentryview"
       tal:attributes="href weblogentryview/getArchiveURL"
       tal:content="brain/Title">title</a>
  </div>

Maybe the latter is better simply because it is less "magic" and I'm
over-estimating the down-side of not using the standard API.


Tim




More information about the Product-Developers mailing list