Do task deletions through a taskqueue #4753

This commit is contained in:
Nicolas Demonte 2020-12-21 12:32:45 +01:00
parent 656681290a
commit e23e31aca0
3 changed files with 43 additions and 5 deletions

View File

@ -19,6 +19,7 @@
<grok:grok package="." />
<include package=".browser" />
<include package=".subscribers" />
<include package="collective.dms.basecontent" />
<include package="collective.dms.batchimport" />

View File

@ -0,0 +1,16 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:i18n="http://namespaces.zope.org/i18n"
xmlns:browser="http://namespaces.zope.org/browser"
xmlns:plone="http://namespaces.plone.org/plone"
i18n_domain="pfwbged.policy">
<browser:page
name="background_delete_tasks"
class=".document.BackgroundDeleteTasksView"
permission="zope2.View"
for="*"
layer="collective.taskqueue.interfaces.ITaskQueueLayer"
/>
</configure>

View File

@ -1,8 +1,12 @@
import logging
import os
import datetime
import pickle
from Acquisition import aq_chain, aq_parent
from Products.Five import BrowserView
from collective.taskqueue import taskqueue
from collective.task.indexers import get_document
from five import grok
from DateTime import DateTime
@ -147,11 +151,28 @@ def delete_tasks(context, event):
query = {'to_id': version_intid,
'from_interfaces_flattened': IBaseTask,
'from_attribute': 'target'}
for rv in catalog.findRelations(query):
obj = rv.from_object
#obj.aq_parent.manage_delObjects([obj.getId()]) # we don't want to verify Delete object permission on object
del aq_parent(obj)[obj.getId()]
reindex_after_version_changes(aq_parent(context))
task_paths = [rv.from_path for rv in catalog.findRelations(query)]
taskqueue.add(
'{}/background_delete_tasks'.format(api.portal.get().absolute_url_path()),
payload=pickle.dumps(task_paths),
)
class BackgroundDeleteTasksView(BrowserView):
def __call__(self):
portal = api.portal.get()
document = None
for task_path in pickle.load(self.request.stdin):
try:
task = portal.unrestrictedTraverse(task_path)
except KeyError:
continue
if task:
document = get_document(task)
del document[task.getId()]
if document:
reindex_after_version_changes(document)
@grok.subscribe(IDmsFile, IObjectAddedEvent)