Back to index

plone3  3.1.7
wfstate.py
Go to the documentation of this file.
00001 from OFS.SimpleItem import SimpleItem
00002 
00003 from zope.interface import implements, Interface
00004 from zope.component import adapts
00005 from zope.formlib import form
00006 from zope import schema
00007 
00008 from plone.contentrules.rule.interfaces import IExecutable, IRuleElementData
00009 
00010 from plone.app.contentrules.browser.formhelper import AddForm, EditForm 
00011 
00012 from Products.CMFCore.utils import getToolByName
00013 from Products.CMFPlone import PloneMessageFactory as _
00014 
00015 class IWorkflowStateCondition(Interface):
00016     """Interface for the configurable aspects of a workflow state condition.
00017     
00018     This is also used to create add and edit forms, below.
00019     """
00020     
00021     wf_states = schema.Set(title=u"Workflow state",
00022                            description=u"The workflow states to check for.",
00023                            required=True,
00024                            value_type=schema.Choice(vocabulary="plone.app.vocabularies.WorkflowStates"))
00025          
00026 class WorkflowStateCondition(SimpleItem):
00027     """The actual persistent implementation of the workflow state condition element.py.
00028     """
00029     implements(IWorkflowStateCondition, IRuleElementData)
00030     
00031     wf_states = []
00032     element = "plone.conditions.WorkflowState"
00033     
00034     @property
00035     def summary(self):
00036         return _(u"Workflow states are: ${states}", mapping=dict(states=", ".join(self.wf_states)))
00037 
00038 class WorkflowStateConditionExecutor(object):
00039     """The executor for this condition.
00040     """
00041     implements(IExecutable)
00042     adapts(Interface, IWorkflowStateCondition, Interface)
00043          
00044     def __init__(self, context, element, event):
00045         self.context = context
00046         self.element = element
00047         self.event = event
00048 
00049     def __call__(self):
00050         portal_workflow = getToolByName(self.context, 'portal_workflow', None)
00051         if portal_workflow is None:
00052             return False
00053         state = portal_workflow.getInfoFor(self.event.object, 'review_state', None)
00054         if state is None:
00055             return False
00056         return state in self.element.wf_states
00057         
00058 class WorkflowStateAddForm(AddForm):
00059     """An add form for workflow state conditions.
00060     """
00061     form_fields = form.FormFields(IWorkflowStateCondition)
00062     label = _(u"Add Workflow State Condition")
00063     description = _(u"A workflow state condition can restrict rules to objects in particular workflow states")
00064     form_name = _(u"Configure element")
00065     
00066     def create(self, data):
00067         c = WorkflowStateCondition()
00068         form.applyChanges(c, self.form_fields, data)
00069         return c
00070 
00071 class WorkflowStateEditForm(EditForm):
00072     """An edit form for portal type conditions
00073     
00074     Formlib does all the magic here.
00075     """
00076     form_fields = form.FormFields(IWorkflowStateCondition)
00077     label = _(u"Edit Workflow State Condition")
00078     description = _(u"A workflow state condition can restrict rules to objects in particular workflow states")
00079     form_name = _(u"Configure element")