Back to index

plone3  3.1.7
notify.py
Go to the documentation of this file.
00001 from OFS.SimpleItem import SimpleItem
00002 from persistent import Persistent 
00003 
00004 from zope.interface import implements, Interface
00005 from zope.component import adapts
00006 from zope.formlib import form
00007 from zope import schema
00008 
00009 from Products.CMFPlone import PloneMessageFactory as _
00010 from Products.CMFPlone import PloneMessageFactory
00011 from plone.contentrules.rule.interfaces import IExecutable, IRuleElementData
00012 
00013 from plone.app.contentrules.browser.formhelper import AddForm, EditForm 
00014 
00015 from Products.statusmessages.interfaces import IStatusMessage
00016 
00017 class INotifyAction(Interface):
00018     """Interface for the configurable aspects of a notify action.
00019     
00020     This is also used to create add and edit forms, below.
00021     """
00022     
00023     message = schema.TextLine(title=_(u"Message"),
00024                               description=_(u"The message to send to the user."),
00025                               required=True)
00026                                     
00027     message_type = schema.Choice(title=_(u"Message type"),
00028                                  description=_(u"Select the type of message to display."),
00029                                  values=("info", "warning", "error"),
00030                                  required=True,
00031                                  default="info")
00032          
00033 class NotifyAction(SimpleItem):
00034     """The actual persistent implementation of the notify action element.
00035     """
00036     implements(INotifyAction, IRuleElementData)
00037     
00038     message = ''
00039     message_type = ''
00040     
00041     element = 'plone.actions.Notify'
00042     
00043     @property
00044     def summary(self):
00045         return _(u"Notify with message ${message}", mapping=dict(message=self.message))
00046 
00047 class NotifyActionExecutor(object):
00048     """The executor for this action.
00049     
00050     This is registered as an adapter in configure.zcml
00051     """
00052     implements(IExecutable)
00053     adapts(Interface, INotifyAction, Interface)
00054          
00055     def __init__(self, context, element, event):
00056         self.context = context
00057         self.element = element
00058         self.event = event
00059 
00060     def __call__(self):
00061         request = self.context.REQUEST
00062         message = PloneMessageFactory(self.element.message)
00063         message_type = self.element.message_type
00064         IStatusMessage(request).addStatusMessage(message, type=message_type)
00065         return True 
00066         
00067 class NotifyAddForm(AddForm):
00068     """An add form for notify rule actions.
00069     """
00070     form_fields = form.FormFields(INotifyAction)
00071     label = _(u"Add Notify Action")
00072     description = _(u"A notify action can show a message to the user.")
00073     form_name = _(u"Configure element")
00074     
00075     def create(self, data):
00076         a = NotifyAction()
00077         form.applyChanges(a, self.form_fields, data)
00078         return a
00079 
00080 class NotifyEditForm(EditForm):
00081     """An edit form for notify rule actions.
00082     
00083     Formlib does all the magic here.
00084     """
00085     form_fields = form.FormFields(INotifyAction)
00086     label = _(u"Edit Notify Action")
00087     description = _(u"A notify action can show a message to the user.")
00088     form_name = _(u"Configure element")