Back to index

plone3  3.1.7
portaltype.py
Go to the documentation of this file.
00001 from persistent import Persistent 
00002 from OFS.SimpleItem import SimpleItem
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 from zope.app.component.hooks import getSite
00009 
00010 from plone.contentrules.rule.interfaces import IExecutable, IRuleElementData
00011 
00012 from plone.app.contentrules.browser.formhelper import AddForm, EditForm 
00013 
00014 from zope.app.form.browser.itemswidgets import MultiSelectWidget
00015 
00016 from Acquisition import aq_inner, aq_base
00017 from Products.CMFCore.utils import getToolByName
00018 from Products.CMFPlone import PloneMessageFactory as _
00019 
00020 class IPortalTypeCondition(Interface):
00021     """Interface for the configurable aspects of a portal type condition.
00022     
00023     This is also used to create add and edit forms, below.
00024     """
00025     
00026     check_types = schema.Set(title=_(u"Content type"),
00027                               description=_(u"The content type to check for."),
00028                               required=True,
00029                               value_type=schema.Choice(vocabulary="plone.app.vocabularies.ReallyUserFriendlyTypes"))
00030          
00031 class PortalTypeCondition(SimpleItem):
00032     """The actual persistent implementation of the portal type condition element.
00033     
00034     Note that we must mix in SimpleItem to keep Zope 2 security happy.
00035     """
00036     implements(IPortalTypeCondition, IRuleElementData)
00037     
00038     check_types = []
00039     element = "plone.conditions.PortalType"
00040     
00041     @property
00042     def summary(self):
00043         portal_types = getToolByName(getSite(), 'portal_types')
00044         titles = []
00045         for name in self.check_types:
00046             fti = getattr(portal_types, name, None)
00047             if fti is not None:
00048                 titles.append(fti.title or name)
00049         return _(u"Content types are: ${names}", mapping=dict(names=", ".join(titles)))
00050 
00051 class PortalTypeConditionExecutor(object):
00052     """The executor for this condition.
00053     
00054     This is registered as an adapter in configure.zcml
00055     """
00056     implements(IExecutable)
00057     adapts(Interface, IPortalTypeCondition, Interface)
00058          
00059     def __init__(self, context, element, event):
00060         self.context = context
00061         self.element = element
00062         self.event = event
00063 
00064     def __call__(self):
00065         obj = aq_inner(self.event.object)
00066         if not hasattr(aq_base(obj), 'getTypeInfo'):
00067             return False
00068         return obj.getTypeInfo().getId() in self.element.check_types
00069         
00070 class PortalTypeAddForm(AddForm):
00071     """An add form for portal type conditions.
00072     """
00073     form_fields = form.FormFields(IPortalTypeCondition)
00074     label = _(u"Add Content Type Condition")
00075     description = _(u"A portal type condition makes the rule apply only to certain content types.")
00076     form_name = _(u"Configure element")
00077     
00078     def create(self, data):
00079         c = PortalTypeCondition()
00080         form.applyChanges(c, self.form_fields, data)
00081         return c
00082 
00083 class PortalTypeEditForm(EditForm):
00084     """An edit form for portal type conditions
00085     """
00086     form_fields = form.FormFields(IPortalTypeCondition)
00087     label = _(u"Edit Content Type Condition")
00088     description = _(u"A portal type condition makes the rule apply only to certain content types.")
00089     form_name = _(u"Configure element")