Skip to main content
 首页 » 编程设计

python之我如何删除 Plone 4 中的自定义灵巧对象

2025年05月04日24tuyile006

我正在尝试删除一些自定义敏捷对象,下面有这个自定义对象的片段代码。

class IDigitalFile(model.Schema): 
        """Dexterity-Schema 
        """ 
 
    ... 
 
    directives.read_permission(auto="cmf.ManagePortal") 
    directives.write_permission(auto="cmf.ManagePortal") 
    fieldset('Admins Only', fields=['auto', 'uploded_at', 'codLote'] ) 
    auto = schema.Bool( 
        title=_(u"Upload via Script?"), 
        required=True, 
        default=False, 
    ) 
    ... 

我尝试使用 sudo plone_daemon bin/client2 -O Plone debug 并应用此代码:

from zope.site.hooks import setSite 
site = 'Plone' 
context = app[site] 
setSite(context) 
portal_catalog = context.portal_catalog 
itens = portal_catalog(portal_type='digitalfile', path='/ged/') 
print itens 

朗姆酒这个脚本后我的输出是[],只是epmty但如果我在ZMI中使用类似的代码我有所有的大脑。

因此,在 ZMI 中,我们不能使用用户方法来删除,因为有限制。 我的目标是删除所有对象 digitalfileif flag Truein autofield.

如果我的结果是空的,我该怎么办? 或者我做错了什么输出是空的?

[更新 1]

这个问题适合我:

zero@srv-d:/opt/Plone439/zeocluster$ sudo -u plone_daemon bin/client2 -O Plone debug 
Starting debugger (the name "app" is bound to the top-level Zope object) 
2017-03-31 10:09:04 WARNING ZODB.blob (18048) Blob dir /opt/Plone439/zeocluster/var/blobstorage/ has insecure mode setting 
Traceback (most recent call last): 
  File "/opt/Plone439/zeocluster/parts/client2/bin/interpreter", line 281, in <module> 
    exec(_val) 
  File "<string>", line 1, in <module> 
  File "/opt/Plone439/buildout-cache/eggs/Zope2-2.13.24-py2.7.egg/ZPublisher/BaseRequest.py", line 623, in traverse 
    response.unauthorized() 
  File "/opt/Plone439/buildout-cache/eggs/Zope2-2.13.24-py2.7.egg/ZPublisher/HTTPResponse.py", line 756, in unauthorized 
    raise Unauthorized, m 
zExceptions.unauthorized.Unauthorized: You are not authorized to access this resource. 

[更新 2 - 已解决]

也许有一个简单的方法,但此时此刻,这个脚本解决了我的问题,如果有人需要,我会分享...

# -*- coding: utf-8 -*- 
from zope.site.hooks import setSite 
from plone import api 
import transaction 
 
# Setting the plone portal in var context 
site = 'Plone' 
context = app[site] 
setSite(context) 
 
# grabbing portal_catalog tool 
portal_catalog = context.portal_catalog 
 
# setting the user that have permission 
with api.env.adopt_user(username='admin'): 
    # grabbing all items digitalfile 
    brains = portal_catalog(portal_type='digitalfile', path='/ged') 
 
for brain in brains: 
    if 'TO BE DELETE' in brain.Description: 
        item = api.content.get(path=brain.getPath()) 
        api.content.delete(obj=item) 
        transaction.commit() 

请您参考如下方法:

这可能意味着您需要“安全许可”才能访问大脑/物体。

通常,门户目录搜索只会返回您具有访问权限的对象的结果。在 ZMI 中,它可以工作,因为您以管理员(或站点管理员)身份登录,而命令行脚本 do not have user information set by default .例如,如果对象未发布(处于 private 状态),它们将不会出现在命令行脚本的目录搜索中。

如果你有plone.api安装后,您可以使用“api.env.adopt_user”以指定用户运行命令:

from plone import api 
 
with api.env.adopt_user(username="admin"): 
    items = portal_catalog(portal_type='digitalfile', path='/ged/') 

或者,您可以使用 portal_catalog.unrestrictedSearchResults 在执行搜索时忽略安全检查:

items = portal_catalog.unrestrictedSearchResults( 
    portal_type='digitalfile', path='/ged/') 

我记得在后一种情况下,有时我不得不使用 brain._unrestrictedGetObject() 来获取实际对象,但现在它似乎也适用于常规 brain.getObject() (可能取决于使用的 Zope/Plone 版本)。