Search portal_catalog for the values of one index of a single object

Hello,

I have an content object (Dexterity type) with some fields. I want to search through the portal_catalog for the index values of this single object. I got only information about the way to get the complete values of an index, but not about the way how to get only the ones that are connected to the single object.

I explain why I had to get the values (to display them in the view, created using TAL) from the catalog:
The field of the Dexterity is created from a vocabulary (different license choices). I want to remove some legacy entries from the active choice of the field (by adding an if clause to the vocabulary Python function). But I had to show the legacy entries for some older objects (because I cannot change their licenses).

I looked through the Plone documentation especially for catalog queries, but could find a pointer to work on a solution for my issue.

It'd be great if I could get such a pointer.

Thanks in advance for any help
Andreas

Got to the portal_catalog object in the ZMI (ie http://localhost:8080/Plone/portal_catalog/manage_catalogView), and use the path to locate your object.
Then click the link to see the indexed values for the object.

Hello Roel,

thanks for your hint. Sorry for the late answer.
I already know the link to the catalog view and the indexes. My issue is to get the value(s) of a keyword index for the current item (a dexterity content object) and display the values on the fly in the view (TAL page template used) of this item (it is a release page with the listing of the licenses of this release). I need to get the information about the releases from the catalog (not all license information are currently available from the release objects form (because they are legacy licenses).

I hope there is a way to get this information within a self written function from the portal_catalog.

You could follow this approach:
You know that a ZMI view presents (among others) the information you need. Determine the source of this view and look at its implementation. This way, you will learn which catalog methods are used to obtain the information.

You will likely need the following: The catalog identifies objects by integer ids. It has an attribute which allows for the mapping from the object's path to its (catalog) id. The view mentioned above (likely) works with the catalog id.

Something like the following may work, I haven't tried it:

path = obj.getPhysicalPath()
idx_data = portal_catalog.getIndexDataForUID(path)
meta_data = portal_catalog.getMetadataForUID(path)

Also see https://github.com/zopefoundation/Products.ZCatalog/blob/master/src/Products/ZCatalog/ZCatalog.py

1 Like

Thank you for your tips, it help me to make this vocabulary to list all date indexes (working for both Plone 4.3.x and 5.x):

from plone import api
from Products.PluginIndexes.interfaces import IDateIndex
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary

import pkg_resources


try:
    pkg_resources.get_distribution('Products.DateRecurringIndex')
except pkg_resources.DistributionNotFound:
    HAS_DATERECURRING = False
else:
    from Products.DateRecurringIndex.index import IDateRecurringIndex
    HAS_DATERECURRING = True


def AllDateIndexVocabulary(context):
    """Vocabulary factory for all date indexes."""
    catalog = api.portal.get_tool('portal_catalog')
    return SimpleVocabulary([
        SimpleTerm(value=index.id)
        for index in catalog.getIndexObjects()
        if IDateIndex.providedBy(index) or
           HAS_DATERECURRING and
           IDateRecurringIndex.providedBy(index)
    ])

Well done!

A quick note: to keep your code a bit simple, I would use a try..except ImportError instead of DistributionNotFound as it doesn't really matter why you cannot import the interface:

from plone import api
from Products.PluginIndexes.interfaces import IDateIndex
from zope.interface import Interface
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary

try:
    from Products.DateRecurringIndex.index import IDateRecurringIndex
except ImportError:
    class IDateRecurringIndex(Interface):
        """Fake interface
        """

def AllDateIndexVocabulary(context):
    """Vocabulary factory for all date indexes."""
    catalog = api.portal.get_tool('portal_catalog')
    return SimpleVocabulary([
        SimpleTerm(value=index.id)
        for index in catalog.getIndexObjects()
        if IDateIndex.providedBy(index) or IDateRecurringIndex.providedBy(index)
    ])
1 Like