Back to index

plone3  3.1.7
workflow.py
Go to the documentation of this file.
00001 from zope.app.schema.vocabulary import IVocabularyFactory
00002 from zope.interface import implements
00003 from zope.schema.vocabulary import SimpleTerm
00004 from zope.schema.vocabulary import SimpleVocabulary
00005 from zope.i18nmessageid import MessageFactory
00006 from zope.i18n import translate
00007 
00008 from Acquisition import aq_get
00009 
00010 from Products.CMFCore.utils import getToolByName
00011 _ = MessageFactory('plone')
00012 
00013 class WorkflowsVocabulary(object):
00014     """Vocabulary factory for workflows.
00015 
00016       >>> from zope.component import queryUtility
00017       >>> from plone.app.vocabularies.tests.base import DummyContext
00018       >>> from plone.app.vocabularies.tests.base import DummyTool
00019 
00020       >>> name = 'plone.app.vocabularies.Workflows'
00021       >>> util = queryUtility(IVocabularyFactory, name)
00022       >>> context1 = DummyContext()
00023       >>> context2 = DummyContext()
00024       >>> context1.context = context2
00025 
00026       >>> util(context1) is None
00027       True
00028 
00029       >>> class Workflow(object):
00030       ...     def __init__(self, id, title):
00031       ...         self.id = id
00032       ...         self.title = title
00033 
00034       >>> tool = DummyTool('portal_workflow')
00035       >>> def objectValues():
00036       ...     return (Workflow('default', 'Default Workflow'),
00037       ...             Workflow('intranet', 'Intranet Workflow'))
00038       >>> tool.objectValues = objectValues
00039       >>> context2.portal_workflow = tool
00040 
00041       >>> workflows = util(context1)
00042       >>> workflows
00043       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00044 
00045       >>> len(workflows.by_token)
00046       2
00047 
00048       >>> intranet = workflows.by_token['intranet']
00049       >>> intranet.title, intranet.token, intranet.value
00050       ('Intranet Workflow', 'intranet', 'intranet')
00051     """
00052     implements(IVocabularyFactory)
00053 
00054     def __call__(self, context):
00055         context = getattr(context, 'context', context)
00056         wtool = getToolByName(context, 'portal_workflow', None)
00057         if wtool is None:
00058             return None
00059         items = [(w.title, w.id) for w in wtool.objectValues()]
00060         items.sort()
00061         items = [SimpleTerm(i[1], i[1], i[0]) for i in items]
00062         return SimpleVocabulary(items)
00063 
00064 WorkflowsVocabularyFactory = WorkflowsVocabulary()
00065 
00066 class WorkflowStatesVocabulary(object):
00067     """Vocabulary factory for workflow states.
00068 
00069       >>> from zope.component import queryUtility
00070       >>> from plone.app.vocabularies.tests.base import DummyContext
00071       >>> from plone.app.vocabularies.tests.base import DummyTool
00072 
00073       >>> name = 'plone.app.vocabularies.WorkflowStates'
00074       >>> util = queryUtility(IVocabularyFactory, name)
00075       >>> context1 = DummyContext()
00076       >>> context2 = DummyContext()
00077       >>> context1.context = context2
00078 
00079       >>> util(context1) is None
00080       True
00081 
00082       >>> tool = DummyTool('portal_workflow')
00083       >>> def listWFStatesByTitle(filter_similar=None):
00084       ...     return (('Private', 'private'), ('Published', 'published'))
00085       >>> tool.listWFStatesByTitle = listWFStatesByTitle
00086       >>> context2.portal_workflow = tool
00087 
00088       >>> states = util(context1)
00089       >>> states
00090       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00091 
00092       >>> len(states.by_token)
00093       2
00094 
00095       >>> pub = states.by_token['published']
00096       >>> pub.title, pub.token, pub.value
00097       (u'Published [published]', 'published', 'published')
00098     """
00099     implements(IVocabularyFactory)
00100 
00101     def __call__(self, context):
00102         context = getattr(context, 'context', context)
00103         # XXX This is evil. A vocabulary shouldn't be request specific.
00104         # The sorting should go into a separate widget.
00105         request = aq_get(context, 'REQUEST', None)
00106         wtool = getToolByName(context, 'portal_workflow', None)
00107         if wtool is None:
00108             return None
00109         items = wtool.listWFStatesByTitle(filter_similar=True)
00110         items_dict = dict([(i[1], translate(_(i[0]), context=request)) for i in items])
00111         items_list = [(k, v) for k, v in items_dict.items()]
00112         items_list.sort(lambda x, y: cmp(x[1], y[1]))
00113         terms = [SimpleTerm(k, title=u'%s [%s]' % (v, k)) for k, v in items_list]
00114         return SimpleVocabulary(terms)
00115 
00116 WorkflowStatesVocabularyFactory = WorkflowStatesVocabulary()
00117 
00118 
00119 class WorkflowTransitionsVocabulary(object):
00120     """Vocabulary factory for workflow transitions
00121 
00122       >>> from zope.component import queryUtility
00123       >>> from plone.app.vocabularies.tests.base import DummyContext
00124       >>> from plone.app.vocabularies.tests.base import DummyTool
00125 
00126       >>> name = 'plone.app.vocabularies.WorkflowTransitions'
00127       >>> util = queryUtility(IVocabularyFactory, name)
00128       >>> context1 = DummyContext()
00129       >>> context2 = DummyContext()
00130       >>> context1.context = context2
00131 
00132       >>> util(context1) is None
00133       True
00134 
00135       >>> class Transition(object):
00136       ...     def __init__(self, id, actbox_name):
00137       ...         self.id = id
00138       ...         self.actbox_name = actbox_name
00139 
00140       >>> class TransitionsFolder(object):
00141       ...     def __init__(self, values):
00142       ...         self.values = values
00143       ...
00144       ...     def objectValues(self):
00145       ...         return self.values
00146 
00147       >>> class Workflow(object):
00148       ...     def __init__(self, id, title, values):
00149       ...         self.id = id
00150       ...         self.title = title
00151       ...         self.transitions = TransitionsFolder(values)
00152 
00153       >>> tool = DummyTool('portal_workflow')
00154       >>> t1 = Transition('publish', 'Publish')
00155       >>> t2 = Transition('reject', 'Reject')
00156 
00157       >>> wf1 = Workflow('default', 'Default Workflow', (t1, t2))
00158       >>> wf2 = Workflow('intranet', 'Intranet Workflow', (t1, ))
00159 
00160       >>> def objectValues():
00161       ...     return (wf1, wf2)
00162       >>> tool.objectValues = objectValues
00163       >>> context2.portal_workflow = tool
00164 
00165       >>> transitions = util(context1)
00166       >>> transitions
00167       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00168 
00169       >>> len(transitions.by_token)
00170       2
00171 
00172       >>> pub = transitions.by_token['Publish [publish]']
00173       >>> pub.title, pub.token, pub.value
00174       (None, 'Publish [publish]', 'publish')
00175     """
00176     implements(IVocabularyFactory)
00177 
00178     def __call__(self, context):
00179         context = getattr(context, 'context', context)
00180         wtool = getToolByName(context, 'portal_workflow', None)
00181         if wtool is None:
00182             return None
00183 
00184         transitions = {}
00185         for wf in wtool.objectValues():
00186             transition_folder = getattr(wf, 'transitions', None)
00187             wf_name = wf.title or wf.id
00188             if transition_folder is not None:
00189                 for transition in transition_folder.objectValues():
00190                     transition_title = transition.actbox_name
00191                     transitions.setdefault(transition.id, []).append(dict(title=transition_title,
00192                                                                         wf_name=wf_name))
00193         items = []
00194         for transition_id, info in transitions.items():
00195             titles = set([i['title'] for i in info])
00196             item_title = ' // '.join(sorted(titles))
00197             items.append(("%s [%s]" % (item_title, transition_id,), transition_id),)
00198 
00199         return SimpleVocabulary.fromItems(sorted(items))
00200 
00201 WorkflowTransitionsVocabularyFactory = WorkflowTransitionsVocabulary()