2014-04-24 14:10:25 +02:00
|
|
|
import inspect
|
|
|
|
from logging import getLogger
|
|
|
|
|
2014-02-06 17:04:32 +01:00
|
|
|
from Acquisition import aq_parent
|
2013-03-26 14:58:33 +01:00
|
|
|
from five import grok
|
|
|
|
|
2013-10-11 15:06:15 +02:00
|
|
|
from OFS.interfaces import IObjectWillBeRemovedEvent
|
|
|
|
from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent
|
2013-07-11 16:58:00 +02:00
|
|
|
from zope.container.contained import ContainerModifiedEvent
|
2013-03-26 14:58:33 +01:00
|
|
|
|
|
|
|
from plone import api
|
2013-06-21 17:45:23 +02:00
|
|
|
from Products.DCWorkflow.interfaces import IAfterTransitionEvent
|
2013-03-26 14:58:33 +01:00
|
|
|
|
2013-06-24 14:08:03 +02:00
|
|
|
from collective.z3cform.rolefield.field import LocalRolesToPrincipalsDataManager
|
|
|
|
|
2013-06-25 15:51:08 +02:00
|
|
|
from collective.task.behaviors import ITarget
|
2013-03-26 14:58:33 +01:00
|
|
|
from collective.task.content.task import ITask
|
2013-06-24 14:08:03 +02:00
|
|
|
from collective.task.content.opinion import IOpinion
|
|
|
|
from collective.task.content.validation import IValidation
|
2013-06-25 15:51:08 +02:00
|
|
|
from collective.task.interfaces import IBaseTask
|
2013-07-11 16:58:00 +02:00
|
|
|
from collective.dms.basecontent.dmsdocument import IDmsDocument
|
2013-03-26 14:58:33 +01:00
|
|
|
|
2014-04-24 14:10:25 +02:00
|
|
|
log = getLogger(__name__)
|
|
|
|
|
2013-03-26 14:58:33 +01:00
|
|
|
|
2013-07-01 11:18:58 +02:00
|
|
|
def grant_local_role_to_responsible(context, role, target):
|
|
|
|
"""Grant local role to responsible on target"""
|
2014-06-16 10:51:26 +02:00
|
|
|
for responsible in context.responsible:
|
|
|
|
target.manage_addLocalRoles(responsible, [role])
|
2013-07-11 15:12:51 +02:00
|
|
|
target.reindexObject()
|
2013-07-01 11:18:58 +02:00
|
|
|
|
|
|
|
|
2013-06-21 17:45:23 +02:00
|
|
|
@grok.subscribe(ITask, IAfterTransitionEvent)
|
|
|
|
def task_changed_state(context, event):
|
|
|
|
"""When a task is abandoned or done, check if it is a subtask
|
|
|
|
and make the wanted transition to parent
|
|
|
|
"""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2013-06-21 17:45:23 +02:00
|
|
|
parent = context.getParentNode()
|
|
|
|
if parent.portal_type == 'task':
|
|
|
|
with api.env.adopt_roles(['Reviewer']):
|
|
|
|
if event.new_state.id == 'done':
|
2014-03-24 12:37:51 +01:00
|
|
|
with api.env.adopt_user('admin'):
|
2014-06-30 15:03:42 +02:00
|
|
|
try:
|
|
|
|
api.content.transition(obj=parent, transition='subtask-done')
|
|
|
|
except api.exc.InvalidParameterError:
|
|
|
|
pass
|
2013-07-19 18:07:26 +02:00
|
|
|
parent.reindexObject(idxs=['review_state'])
|
2013-06-21 17:45:23 +02:00
|
|
|
elif event.new_state.id == 'abandoned':
|
2019-01-18 16:14:49 +01:00
|
|
|
# don't transition parent task if other subtasks are still live
|
|
|
|
sibling_subtasks = set(parent.listFolderContents()).difference([context])
|
|
|
|
if any([
|
|
|
|
task for task in sibling_subtasks
|
|
|
|
if api.content.get_state(task) not in ('abandoned', 'done')
|
|
|
|
]):
|
|
|
|
return
|
|
|
|
|
2014-03-24 12:37:51 +01:00
|
|
|
with api.env.adopt_user('admin'):
|
2014-06-30 15:03:42 +02:00
|
|
|
try:
|
|
|
|
api.content.transition(obj=parent, transition='subtask-abandoned')
|
|
|
|
except api.exc.InvalidParameterError:
|
|
|
|
pass
|
2013-07-19 18:07:26 +02:00
|
|
|
parent.reindexObject(idxs=['review_state'])
|
2013-06-21 17:45:23 +02:00
|
|
|
|
|
|
|
|
2013-10-11 15:06:15 +02:00
|
|
|
@grok.subscribe(ITask, IObjectWillBeRemovedEvent)
|
2013-03-26 14:58:33 +01:00
|
|
|
def reopen_parent_task(context, event):
|
|
|
|
"""When a task is deleted, reopen its parent task
|
|
|
|
"""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2013-03-26 14:58:33 +01:00
|
|
|
parent = context.getParentNode()
|
2013-05-28 14:32:18 +02:00
|
|
|
parent_state = api.content.get_state(parent)
|
2013-06-04 17:05:53 +02:00
|
|
|
if parent.portal_type == 'task' and parent_state == 'attributed':
|
2013-10-11 15:06:15 +02:00
|
|
|
state = api.content.get_state(context)
|
|
|
|
if state == 'todo':
|
|
|
|
with api.env.adopt_roles(['Reviewer']):
|
|
|
|
api.content.transition(obj=context, transition='abandon')
|
|
|
|
context.reindexObject(idxs=['review_state'])
|
|
|
|
elif state == 'refusal-requested':
|
|
|
|
with api.env.adopt_roles(['Reviewer']):
|
|
|
|
api.content.transition(obj=context, transition='accept-refusal')
|
|
|
|
context.reindexObject(idxs=['review_state'])
|
|
|
|
elif state == 'in-progress':
|
|
|
|
with api.env.adopt_roles(['Editor']):
|
|
|
|
api.content.transition(obj=context, transition='ask-for-refusal')
|
|
|
|
context.reindexObject(idxs=['review_state'])
|
|
|
|
with api.env.adopt_roles(['Reviewer']):
|
|
|
|
api.content.transition(obj=context, transition='accept-refusal')
|
|
|
|
context.reindexObject(idxs=['review_state'])
|
2013-06-24 14:08:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
@grok.subscribe(IBaseTask, IObjectAddedEvent)
|
|
|
|
def set_enquirer(context, event):
|
|
|
|
"""Set Enquirer field after task creation"""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2013-06-24 14:08:03 +02:00
|
|
|
enquirer = api.user.get_current().id
|
|
|
|
enquirer_dm = LocalRolesToPrincipalsDataManager(context, IBaseTask['enquirer'])
|
2013-06-24 14:58:16 +02:00
|
|
|
enquirer_dm.set((enquirer,))
|
2014-02-06 17:04:32 +01:00
|
|
|
|
|
|
|
parent = aq_parent(context)
|
|
|
|
if IBaseTask.providedBy(parent):
|
|
|
|
# parent is also a task, we create Reader local role value on its own
|
|
|
|
# roles, unless parent enquirer is the greffier.
|
|
|
|
if not 'Greffier' in api.user.get_roles(parent.enquirer[0]):
|
|
|
|
for user_id, roles in parent.get_local_roles():
|
|
|
|
if 'Reader' in roles or 'Reviewer' in roles:
|
|
|
|
context.manage_addLocalRoles(user_id, ['Reader'])
|
|
|
|
|
2014-04-24 14:11:48 +02:00
|
|
|
context.reindexObjectSecurity()
|
2014-10-08 16:38:51 +02:00
|
|
|
context.reindexObject(idxs=['allowedRolesAndUsers', 'enquirer'])
|
2013-06-24 14:08:03 +02:00
|
|
|
|
|
|
|
|
2013-06-25 15:51:08 +02:00
|
|
|
@grok.subscribe(ITarget, IObjectAddedEvent)
|
|
|
|
def set_reader_on_target(context, event):
|
|
|
|
"""Set Reader role on target to responsible after opinion or validation creation"""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2013-11-06 14:43:48 +01:00
|
|
|
if context.target:
|
|
|
|
target = context.target.to_object
|
|
|
|
grant_local_role_to_responsible(context, 'Reader', target)
|
2013-07-01 11:18:58 +02:00
|
|
|
|
2013-06-27 16:58:46 +02:00
|
|
|
|
|
|
|
@grok.subscribe(IValidation, IObjectAddedEvent)
|
|
|
|
def set_reviewer_on_target(context, event):
|
|
|
|
"""Set Reviewer role on target to responsible after validation creation"""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2013-11-06 14:43:48 +01:00
|
|
|
if context.target:
|
|
|
|
target = context.target.to_object
|
|
|
|
grant_local_role_to_responsible(context, 'Reviewer', target)
|
2013-07-01 11:18:58 +02:00
|
|
|
|
|
|
|
|
2013-07-20 13:07:06 +02:00
|
|
|
@grok.subscribe(IValidation, IObjectAddedEvent)
|
2013-07-01 11:18:58 +02:00
|
|
|
@grok.subscribe(IOpinion, IObjectAddedEvent)
|
|
|
|
def set_contributor_on_document(context, event):
|
2013-07-20 13:07:06 +02:00
|
|
|
"""Set Contributor role on document to responsible after opinion and
|
|
|
|
validation creation. (Contributor can create a new version)
|
2013-07-01 11:18:58 +02:00
|
|
|
"""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2013-07-01 11:18:58 +02:00
|
|
|
document = context.getParentNode()
|
|
|
|
grant_local_role_to_responsible(context, 'Contributor', document)
|
2013-07-11 16:58:00 +02:00
|
|
|
|
|
|
|
|
2014-03-26 10:42:00 +01:00
|
|
|
@grok.subscribe(IValidation, IObjectAddedEvent)
|
|
|
|
def set_editor_on_document(context, event):
|
|
|
|
"""Set Editor role on document to responsibile after validation
|
|
|
|
creation."""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2014-03-26 10:42:00 +01:00
|
|
|
document = context.getParentNode()
|
|
|
|
grant_local_role_to_responsible(context, 'Editor', document)
|
|
|
|
|
|
|
|
|
2013-07-11 16:58:00 +02:00
|
|
|
@grok.subscribe(IDmsDocument, IObjectModifiedEvent)
|
|
|
|
def reindex_brain_metadata_on_basetask(doc, event):
|
2013-07-12 09:44:09 +02:00
|
|
|
"""Reindex brain metadatas when document is modified"""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2013-07-11 16:58:00 +02:00
|
|
|
if isinstance(event, ContainerModifiedEvent):
|
|
|
|
return
|
|
|
|
|
|
|
|
catalog = api.portal.get_tool('portal_catalog')
|
|
|
|
tasks = catalog.unrestrictedSearchResults({
|
|
|
|
'object_provides': IBaseTask.__identifier__,
|
|
|
|
'path': '/'.join(doc.getPhysicalPath())})
|
|
|
|
for b in tasks:
|
|
|
|
# reindex id index just to trigger the update of metadata on brain
|
|
|
|
b._unrestrictedGetObject().reindexObject(idxs=['id'])
|
2013-07-12 09:42:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
@grok.subscribe(IOpinion, IAfterTransitionEvent)
|
|
|
|
def set_reader_on_versions(context, event):
|
2013-07-12 09:44:09 +02:00
|
|
|
"""Set Reader role on Opinion enquirer on all versions created by
|
|
|
|
the responsible when the Opinion is returned"""
|
2014-04-24 14:10:25 +02:00
|
|
|
log.info(inspect.stack()[0][3])
|
2013-07-12 09:42:51 +02:00
|
|
|
if event.new_state.id == 'done':
|
|
|
|
responsible = context.responsible[0]
|
|
|
|
enquirer = context.enquirer[0]
|
|
|
|
container_path = '/'.join(context.getParentNode().getPhysicalPath())
|
2013-08-30 19:08:37 +02:00
|
|
|
query = {'path': {'query': container_path},
|
2013-07-12 09:42:51 +02:00
|
|
|
'portal_type': 'dmsmainfile',
|
|
|
|
'Creator': responsible}
|
|
|
|
catalog = api.portal.get_tool('portal_catalog')
|
|
|
|
versions = catalog.searchResults(query)
|
|
|
|
for brain in versions:
|
|
|
|
version = brain.getObject()
|
|
|
|
version.manage_addLocalRoles(enquirer, ['Reader'])
|
|
|
|
version.reindexObject()
|