Back to index

plone3  3.1.7
fileextension.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.ATContentTypes.interface import IFileContent
00013 from Products.CMFPlone import PloneMessageFactory as _
00014 
00015 class IFileExtensionCondition(Interface):
00016     """Interface for the configurable aspects of a portal type condition.
00017     
00018     This is also used to create add and edit forms, below.
00019     """
00020     
00021     file_extension = schema.TextLine(title=_(u"File extension"),
00022                                     description=_(u"The file extension to check for"),
00023                                     required=True)
00024          
00025 class FileExtensionCondition(SimpleItem):
00026     """The actual persistent implementation of the file extension condition.
00027     
00028     Note that we must mix in Explicit to keep Zope 2 security happy.
00029     """
00030     implements(IFileExtensionCondition, IRuleElementData)
00031     
00032     file_extension = u''
00033     element = "plone.conditions.FileExtension"
00034     
00035     @property
00036     def summary(self):
00037         return _(u"File extension is ${ext}", mapping=dict(ext=self.file_extension))
00038 
00039 class FileExtensionConditionExecutor(object):
00040     """The executor for this condition.
00041     
00042     This is registered as an adapter in configure.zcml
00043     """
00044     implements(IExecutable)
00045     adapts(Interface, IFileExtensionCondition, Interface)
00046          
00047     def __init__(self, context, element, event):
00048         self.context = context
00049         self.element = element
00050         self.event = event
00051 
00052     def __call__(self):
00053         obj = self.event.object
00054         if not IFileContent.providedBy(obj):
00055             return False
00056         
00057         base_unit = obj.getFile()
00058         get_filename = getattr(base_unit, 'getFilename', None)
00059         if not get_filename:
00060             return False
00061         
00062         name = get_filename()
00063         extension = name[name.rfind('.')+1:]
00064         return extension == self.element.file_extension
00065         
00066 class FileExtensionAddForm(AddForm):
00067     """An add form for file extension rule conditions.
00068     """
00069     form_fields = form.FormFields(IFileExtensionCondition)
00070     label = _(u"Add File Extension Condition")
00071     description = _(u"A file extension condition can restrict a rule from executing unless the target is a File with a particular extension.")
00072     form_name = _(u"Configure element")
00073     
00074     def create(self, data):
00075         c = FileExtensionCondition()
00076         form.applyChanges(c, self.form_fields, data)
00077         return c
00078 
00079 class FileExtensionEditForm(EditForm):
00080     """An edit form for portal type conditions
00081     
00082     Formlib does all the magic here.
00083     """
00084     form_fields = form.FormFields(IFileExtensionCondition)
00085     label = _(u"Edit File Extension Condition")
00086     description = _(u"A file extension condition can restrict a rule from executing unless the target is a File with a particular extension.")
00087     form_name = _(u"Configure element")