Question

I am traversing folders with content items within them. I use the portal_catalog to get brains searched on certain paths. The brains have access to metadata, and brain.getObject() will return the actual object. I have gotten the parent for an object with brain.getObject().aq_parent. Now I want to get the object's position in the parent. At first I tried brain.getObject().getObjPositionInParent(), and afterwards, I realized that the getObjPositionInParent() is an attribute accessible from the index data.

idxData = catalog.getIndexDataForRID(brain.getRID())

sJson = json.dumps( idxData )
l = brain.getObject()
lUpdate = {'path': '/'.join( l.getPhysicalPath()), 'meta_type': l.meta_type, 'title':l.getRawTitle(), 'remoteUrl': l.getRemoteUrl(), 'json':sJson}

When I printed this out to the screen, I see all of the items within the dict that is returned from the catalog.getIndexDataForRID call. The problem is that for all of the objects, getObjPositionInParent() is an empty array ([]). On this page http://developer.plone.org/searching_and_indexing/query.html, it appears the value should be an integer. This made me wonder if I have to go create the index data, and, if so, then I might be reaching too far outward from the object to get data that must already be there (since the folders obviously know what position to put each child in). What is the best way to get a content object's position in the parent? Thanks in advance for any information?

More:

I am unsure why the adapter cannot be found, but it may have to do with lack of registering it. This is a script that I build the Zope environment to read the ZODB directly from the file as opposed to on top of the running Zope instance. Is it possible that I have to register the adapter with the GlobalSiteManager?

Thank you, Mathias. When I use the sort_on="getObjPositionInParent", I get the following error:

Traceback (most recent call last):
  File "extractMenuStructure.py", line 459, in <module>
    res = processFolder( home['childItems'], '/Sanford Guide Web Edition/' + appFolderNm + '', config['screens'] )
  File "extractMenuStructure.py", line 390, in processFolder
    results = portal_catalog(path={"query":currentPath, "depth":d},sort_on="getObjPositionInParent")
  File "/Applications/Plone/buildout-cache/eggs/Products.CMFPlone-4.1.2-py2.6.egg/Products/CMFPlone/CatalogTool.py", line 427, in searchResults
    return ZCatalog.searchResults(self, REQUEST, **kw)
  File "/Applications/Plone/buildout-cache/eggs/Products.ZCatalog-2.13.20-py2.6.egg/Products/ZCatalog/ZCatalog.py", line 604, in searchResults
    return self._catalog.searchResults(REQUEST, used, **kw)
  File "/Applications/Plone/buildout-cache/eggs/Products.ZCatalog-2.13.20-py2.6.egg/Products/ZCatalog/Catalog.py", line 909, in searchResults
    return self.search(args, sort_index, reverse, sort_limit, _merge)
  File "/Applications/Plone/buildout-cache/eggs/Products.ZCatalog-2.13.20-py2.6.egg/Products/ZCatalog/Catalog.py", line 658, in search
    b_size=b_size)
  File "/Applications/Plone/buildout-cache/eggs/Products.ZCatalog-2.13.20-py2.6.egg/Products/ZCatalog/Catalog.py", line 678, in sortResults
    index_key_map = sort_index.documentToKeyMap()
  File "/Applications/Plone/buildout-cache/eggs/plone.app.folder-1.0.4-py2.6.egg/plone/app/folder/nogopip.py", line 91, in documentToKeyMap
    ids = folder.getOrdering().idsInOrder()
  File "/Applications/Plone/buildout-cache/eggs/plone.folder-1.0.1-py2.6.egg/plone/folder/ordered.py", line 41, in getOrdering
    adapter = getAdapter(self, IOrdering)
  File "/Applications/Plone/buildout-cache/eggs/zope.component-3.9.5-py2.6.egg/zope/component/_api.py", line 96, in getAdapter
    raise ComponentLookupError(object, interface, name)
zope.component.interfaces.ComponentLookupError: (&lt;ATFolder at /Sanford Guide Web Edition/amt&gt;, &lt;InterfaceClass plone.folder.interfaces.IOrdering&gt;, u'')
Was it helpful?

Solution

The best way is to do like the index itself.

Code snipped based on the CatalogTool (Products.CMFPlone)

from Acquisition import aq_inner
from Acquisition import aq_parent
from OFS.interfaces import IOrderedContainer

obj = brain.getObject()
parent = aq_parent(aq_inner(obj))
ordered = IOrderedContainer(parent, None)
if ordered is not None:
    return ordered.getObjectPosition(obj.getId())
return 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top