Direkt zum Inhalt | Direkt zur Navigation

Benutzerspezifische Werkzeuge

Sie sind hier: Startseite / Tech-Blog / Plone: prevent unwanted indexing of child objects thru Zope acquisition

Plone: prevent unwanted indexing of child objects thru Zope acquisition

The Zope catalog will index child objects for attributes which are exist on parent objects but not on the child it self. Most of the time this is unwanted and can result in errors. We can use indexers to prevent that.

First we define two indexers, a dummy indexer and the more specific real indexer for our index we want:

from plone.indexer import indexer


@indexer(IDexterityContent)
def dummy_index(obj):
    """ dummy to prevent indexing child objects
    """
    raise AttributeError("This field should not indexed here!")


@indexer(ILibrary)
def my_parent_index(obj):
    """
    :return: value of field my_parent_field
    """
    return obj.my_parent_field

For these indexers we register two adapters:

<configure
    xmlns="http://namespaces.zope.org/zope">

    <adapter factory=".library.dummy_index" name="my_parent_field" />
    <adapter factory=".library.my_parent_index" name="my_parent_field" />

</configure>

Both adapters have the same name. The catalog now lookup for these adapters and will use the more specific adapter for the current object. So for the parent object with the interface ILibrary it will be the second and it will result the index value. But for a child object the catalog will get the adapter which calls the dummy_index index and this will raise an AttributeError and the catalog will ignore that.

abgelegt unter: , ,