在Plone中通过破碎的物体循环

时间:2013-04-11 13:20:14

标签: python plone zope zodb

我有几个破碎的对象,我希望在python脚本中循环。我的用例如下:我已将自定义产品从my.oldproduct重命名为my.newproduct。这导致先前使用my.oldproduct保存的对象被破坏,因此无法访问。这里有详细解决方法:Updating broken objects

现在我要做的是在ZMI中创建一个python脚本来循环所有损坏的内容,更改/更新它们,从而使用my.newproduct保存它们。

我一直无法获取旧对象,因为它们未列出。查看我的python脚本示例以列出网站中的所有内容,但它们仍未显示:

from Products.CMFCore.utils import getToolByName

app = context.restrictedTraverse('/')
sm = app.plone.getSiteManager()

catalog = getToolByName(context, 'portal_catalog')
results = catalog.searchResults()

count = 0
for obj in results:
    print obj.meta_type
    count += 1

print str("Found " + str(count) + " matching objects")

return printed

如何从my.oldproduct列出要破坏的对象?

1 个答案:

答案 0 :(得分:4)

我担心你需要手动遍历整个ZODB。如果这些对象是内容对象,您应该能够使用标准的OFS方法:

from collections import deque
from datetime import datetime

import transaction
from zope.app.component.hooks import setSite
from Testing.makerequest import makerequest
from AccessControl.SecurityManagement import newSecurityManager

from my.newproduct.types import ArchetypesContentType


site_id = 'Plone'     # adjust to match your Plone site object id.
admin_user = 'admin'  # usually 'admin', probably won't need adjusting
app = makerequest(app)
site = app[site_id]
setSite(site)
user = app.acl_users.getUser(admin_user).__of__(site.acl_users)
newSecurityManager(None, user)


def treeWalker(root):
    # stack holds (parent, id, obj) tuples
    stack = deque([(None, None, root)])
    while stack:
        parent, id, next = stack.popleft()
        try:
            stack.extend((next, id, child) for id, child in next.objectItems())
        except AttributeError:
            # No objectItems method
            pass
        yield parent, id, next


count = 0
for parent, id, obj in treeWalker(site):
    if isinstance(obj, ArchetypesContentType):
        print 'Found content type object {} at {}'.format(id, '/'.join(object.getPhysicalPath()))
        obj._p_changed = True  # mark it as changed, force a commit
        count += 1
        if count % 100 == 0:
            # flush changes so far to disk to minimize memory usage
            transaction.savepoint(True)
            print '{} - Processed {} items'.format(datetime.now(), count)

transaction.commit()

这假设您已经包含您链接到的解决方案;尝试使用ZODB.broken.Broken个对象进行上述操作毫无意义。

上述脚本充当bin/instance run脚本,按原样运行:

bin/instance run path/to/this/script.py

您将在网站中处理所有,这是一个相当沉重的过程,会涉及大量缓存流失,并可能会发生潜在冲突的大型提交。你真的不想将它作为一个通过web脚本运行。