indexing - 获取内容对象在父级中的位置的最佳方法是什么? Plone 人4号

标签 indexing position plone catalog plone-4.x

我正在遍历其中包含内容项的文件夹。我使用portal_catalog 在某些路径上搜索大脑。大脑可以访问元数据,并且 Brain.getObject() 将返回实际的对象。我已经通过 Brain.getObject().aq_parent 获得了一个对象的父对象。现在我想获取对象在父级中的位置。一开始我尝试了brain.getObject().getObjPositionInParent(),后来我意识到getObjPositionInParent()是一个可以从索引数据访问的属性。

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}

当我将其打印到屏幕上时,我看到了从 Catalog.getIndexDataForRID 调用返回的字典中的所有项目。问题是对于所有对象, getObjPositionInParent() 都是一个空数组 ([])。在此页 http://developer.plone.org/searching_and_indexing/query.html ,看起来该值应该是一个整数。这让我想知道我是否必须去创建索引数据,如果是这样,那么我可能距离对象太远而无法获取必须已经存在的数据(因为文件夹显然知道将每个子项放在什么位置)在)。获取内容对象在父级中的位置的最佳方法是什么?预先感谢您提供任何信息?

更多:

我不确定为什么找不到适配器,但这可能与缺少注册有关。这是我构建 Zope 环境以直接从文件读取 ZODB 的脚本,而不是在正在运行的 Zope 实例之上读取 ZODB。我是否必须向 GlobalSiteManager 注册适配器?

谢谢你,马蒂亚斯。当我使用 sort_on="getObjPositionInParent"时,出现以下错误:

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'')

最佳答案

最好的方法是像索引本身一样。

基于目录工具 (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

关于indexing - 获取内容对象在父级中的位置的最佳方法是什么? Plone 人4号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405734/

相关文章:

php - 如何有效地查找具有类似 WordPress 的 URL 信息的帖子

Javascript:如何替换字符串的内容,但不在 HTML 标记中?

css3 旋转字居中对齐

html - 在iphone中的固定位置

error-handling - 向用户添加共享权限时发生AttributeError

Plone:关闭 document_byline (创建信息)

python - Plone - 从 View 中的成员文件夹获取并列出对象

python - Python3.5中列表索引的快捷方式是什么?

c++ - 任何令人满意的迭代器递归来替换类型为$ a [i] = b [i] * c [i] $的for循环

html - 将 CSS 菜单移到屏幕右侧?