Execute multi-actions in the background #33748
This commit is contained in:
parent
463d140eb0
commit
a58ad366df
|
@ -72,6 +72,14 @@
|
|||
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
|
||||
/>
|
||||
|
||||
<browser:page
|
||||
name="background_document_transition"
|
||||
class=".multiactions.BackgroundDocumentTransitionView"
|
||||
permission="zope2.View"
|
||||
for="*"
|
||||
layer="collective.taskqueue.interfaces.ITaskQueueLayer"
|
||||
/>
|
||||
|
||||
<adapter factory=".widget.PFWBRelatedDocsFieldWidget" />
|
||||
|
||||
<!--
|
||||
|
|
|
@ -1,8 +1,27 @@
|
|||
from Products.Five.browser import BrowserView
|
||||
from pfwbged.collection import _
|
||||
from collective.taskqueue import taskqueue
|
||||
from plone import api
|
||||
from plone.api.exc import InvalidParameterError
|
||||
from zope.i18n import translate
|
||||
|
||||
|
||||
class BackgroundDocumentTransitionView(BrowserView):
|
||||
|
||||
def __call__(self):
|
||||
document = self.context
|
||||
action = self.request.form['action']
|
||||
|
||||
if action in ('validate', 'refuse'):
|
||||
for child in reversed(document.values()):
|
||||
if child.portal_type == 'dmsmainfile':
|
||||
api.content.transition(child, action)
|
||||
break
|
||||
else:
|
||||
api.content.transition(document, action)
|
||||
|
||||
document.reindexObject(idxs=['review_state'])
|
||||
|
||||
if action == 'to_process' and document.portal_type == 'dmsincomingmail':
|
||||
from pfwbged.policy.subscribers.mail import incoming_mail_attributed
|
||||
incoming_mail_attributed(document, u'')
|
||||
|
||||
|
||||
class MultiActionsView(BrowserView):
|
||||
|
@ -11,33 +30,11 @@ class MultiActionsView(BrowserView):
|
|||
documents = self.request.form['documents[]']
|
||||
if isinstance(documents, basestring):
|
||||
documents = [documents]
|
||||
failures = []
|
||||
for document_id in documents:
|
||||
document = api.content.get(document_id)
|
||||
try:
|
||||
if action in ('validate', 'refuse'):
|
||||
for child in reversed(document.values()):
|
||||
if child.portal_type == 'dmsmainfile':
|
||||
api.content.transition(child, action)
|
||||
break
|
||||
else:
|
||||
api.content.transition(document, action)
|
||||
except InvalidParameterError:
|
||||
failures.append(document)
|
||||
else:
|
||||
document.reindexObject(idxs=['review_state'])
|
||||
if action == 'to_process' and document.portal_type == 'dmsincomingmail':
|
||||
from pfwbged.policy.subscribers.mail import incoming_mail_attributed
|
||||
incoming_mail_attributed(document, u'')
|
||||
if failures:
|
||||
self.request.response.setStatus(403)
|
||||
header = translate(
|
||||
_(u'You don\'t have the right to change the state of these objects:'),
|
||||
context=self.request
|
||||
|
||||
for document_path in documents:
|
||||
taskqueue.add(
|
||||
'{}/background_document_transition'.format(document_path),
|
||||
params={"action": action}
|
||||
)
|
||||
return u'{}\n{}'.format(
|
||||
header,
|
||||
u'\n'.join(['- {}'.format(failure.Title()) for failure in failures]),
|
||||
)
|
||||
else:
|
||||
return 'OK'
|
||||
|
||||
return "OK"
|
||||
|
|
Reference in New Issue