Hi,

On 2 May 2010 14:11, mnieper <[email protected]> wrote:
>
> Hi,
>
> I'm currently developing a product using the plone.app.dexterity content
> types, in which I have two schemas, say IMyFolder and IMyItem, which give
> rise to content types via dexterity. Furthermore, I am implementing a
> portlet, say MyPortlet.
>
> I would like to achieve the following: If MyPortlet is added to any folder,
> it should traverse the containment hierarchy until it finds an IMyFolder.
> Then it should display the IMyItems contained inside this IMyFolder.
>
> I have thought about different ways, but I am not sure, which is the proper
> or correct way to do this:
>
> 1) Implement a view "@@myitems" without a template on IMyFolder, which uses
> a catalog search to return the IMyItems contained in it. The MyPortlet
> applies @@myitems on the current context and relies on acquisition so that
> it bubbles up until an IMyFolder is found.

A view will not be acquired in that way: if the context doesn't
provide an interface for which @@myitems is registered, the view
lookup will fail.

And even if it did work (e.g. you register it for a generic
interface), the context will always be "the context", so the view
would need to do the "walk" up the chain to find the IMyFolder.

I wouldn't use this approach.

> 2) Define a custom class for IMyFolder based on the dexterity folder class.
> Add a method "myitems" that returns the contained IMyItems. The MyPortlet
> then tries to call this method on the current context and relies again on
> acquisition.

That would work, but it's ugly and error prone. Relying on acquisition
in this way is discouraged.

> 3) Define an adapter from the IMyFolder interface to a new interface, say
> IHasMyItems, which has a method called "myitems". The MyPortlet then calls
> aq_chain and traverses the acquisition chain by hand until it has found an
> object which can be adaptet to a IHasMyItems.

I'd do something similar to this, but the adapter sounds superfluous.

In the renderer, I'd do:

folder = None
for parent in aq_chain(aq_inner(self.context)):
    if IMyFolder.providedBy(parent):
        folder = parent
        break

if folder is not None:
    ...

You already have an interface describing the type of object you're
looking for, so I'd just use that.

Martin
_______________________________________________
Product-Developers mailing list
[email protected]
http://lists.plone.org/mailman/listinfo/product-developers

Reply via email to