pythonplonedexterityplone-4.x

How I delete custom dexterity objects in Plone 4?


I'm trying delete some custom dexterity objects, bellow have snipets code of this custom object.

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,
    )
    ...

And I try use sudo plone_daemon bin/client2 -O Plone debug and apply this code:

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

After rum this script my output is [], just epmty but if i use the similar code in ZMI I have all brains.

So, in ZMI we can't user methods to delete 'cuz have restrictions. And my goal is delete all objects digitalfileif flag Truein autofield.

How can I do it if my results are empties? Or what I did wrong for output is empty?

[UPDATE 1]

This problem apper to me:

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.

[UPDATE 2 - SOLVED]

Maybe have a easy way to do, but in this moment, this script solved my problem and I'm sharing if someone need...

# -*- 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()

Solution

  • It might mean that you need "security clearance" to access the brains/objects.

    Normally portal catalog search will only return results for objects for which you have access permissions. In ZMI it works because you are logged-in as an admin (or a site manager), while command line scripts do not have user information set by default. For example, if objects are not published (in private state), they will not show up in catalog search in a command line script.

    If you have plone.api installed, you can use "api.env.adopt_user" to run a command with the specified user:

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

    Alternatively, you can use portal_catalog.unrestrictedSearchResults to ignore security checks when performing the search:

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

    I remember that in the later case sometimes I had to use brain._unrestrictedGetObject() to get the actual object, but now it seems to work also with regular brain.getObject() (perhaps depends on Zope/Plone version used).