Back to index

plone3  3.1.7
_base.py
Go to the documentation of this file.
00001 # Marshall: A framework for pluggable marshalling policies
00002 # Copyright (C) 2004-2006 Enfold Systems, LLC
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License as published by
00006 # the Free Software Foundation; either version 2 of the License, or
00007 # (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #
00018 """
00019 $Id: _base.py 5626 2006-01-24 02:26:47Z dreamcatcher $
00020 """
00021 
00022 from OFS.SimpleItem import SimpleItem
00023 from Globals import InitializeClass
00024 from AccessControl import ClassSecurityInfo
00025 from AccessControl.Permissions import view, manage_properties
00026 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00027 from Products.Marshall.expression import Expression, createExprContext
00028 from Products.Marshall.registry import createPredicate, registerPredicate
00029 from Products.Marshall.registry import getRegisteredPredicates
00030 from Products.Marshall.registry import getRegisteredComponents
00031 from Products.Marshall.interfaces import IPredicate
00032 
00033 class Predicate(SimpleItem):
00034     """ A Predicate for selecting marshallers.
00035 
00036     Each Predicate is composed of a Expression  which
00037     is evaluated in the context of the object to
00038     decide if the Predicate applies, and returns
00039     a component name to be used for that object.
00040     """
00041 
00042     __implements__ = (IPredicate,)
00043 
00044     meta_type = "Marshaller Predicate"
00045     security = ClassSecurityInfo()
00046 
00047     manage_options = (
00048         {'label':'Edit', 'action':'manage_changePredicateForm'},
00049         ) + SimpleItem.manage_options
00050 
00051     security.declareProtected('View management screens',
00052       'manage_changePredicateForm', 'manage_main', 'manage')
00053 
00054     manage_changePredicateForm = PageTemplateFile(
00055         '../www/predicateChange', globals(),
00056         __name__='manage_changePredicateForm')
00057 
00058     manage_changePredicateForm._owner = None
00059     manage = manage_main = manage_changePredicateForm
00060 
00061     def __init__(self, id, title, expression='', component_name=''):
00062         self.id = id
00063         self.title = title
00064         self.setExpression(expression)
00065         self.setComponentName(component_name)
00066 
00067     security.declareProtected(view, 'apply')
00068     def apply(self, obj, **kw):
00069         """ Evaluate expression using the object as
00070         context and return a component name if applicable.
00071         """
00072         if not self.getExpression():
00073             # Short-circuit for speedup when no expression.
00074             return (self.getComponentName(),)
00075         context = createExprContext(obj, **kw)
00076         if self.expression(context):
00077             return (self.getComponentName(),)
00078         return ()
00079 
00080     security.declareProtected(view, 'getComponentName')
00081     def getComponentName(self):
00082         """ Return the component name configured for
00083         this predicate.
00084         """
00085         return self._component_name
00086 
00087     security.declareProtected(manage_properties, 'setComponentName')
00088     def setComponentName(self, component_name):
00089         """ Change the component name """
00090         valid_components = [i['name'] for i in getRegisteredComponents()]
00091         if component_name not in valid_components:
00092             raise ValueError, ('Not a valid registered '
00093                                'component: %s' % component_name)
00094         self._component_name = component_name
00095 
00096     security.declareProtected(manage_properties, 'setExpression')
00097     def setExpression(self, expression):
00098         """ Change the expression """
00099         self._expression_text = expression
00100         self._expression = Expression(expression)
00101 
00102     security.declareProtected(view, 'expression')
00103     def expression(self, context):
00104         """ Evaluate the expression using context """
00105         return self._expression(context)
00106 
00107     security.declareProtected(view, 'getExpression')
00108     def getExpression(self):
00109         """ Get the expression as text """
00110         return self._expression_text
00111 
00112     def manage_availableComponents(self):
00113         return getRegisteredComponents()
00114 
00115     security.declareProtected(manage_properties, 'manage_changePredicate')
00116     def manage_changePredicate(self, title=None,
00117                                expression=None,
00118                                component_name=None,
00119                                REQUEST=None):
00120         """ Change the settings of this predicate """
00121 
00122         if title is not None:
00123             self.title = title
00124         if expression is not None:
00125             self.setExpression(expression)
00126         if component_name is not None:
00127             self.setComponentName(component_name)
00128 
00129         if REQUEST is not None:
00130             message = 'Predicate Constraints Changed.'
00131             return self.manage_changePredicateForm(
00132                 manage_tabs_message=message,
00133                 management_view='Edit')
00134         return self
00135 
00136 InitializeClass(Predicate)
00137 registerPredicate('default', 'Default Predicate', Predicate)
00138 
00139 def manage_addPredicate(self, id, title, predicate, expression,
00140                         component_name, REQUEST=None):
00141     """ Factory method that creates a Property Set Predicate"""
00142     obj = createPredicate(predicate, id, title, expression, component_name)
00143     self._setObject(id, obj)
00144 
00145     if REQUEST is not None:
00146         next = 'manage_main'
00147         if hasattr(obj, 'manage_changeSettingsForm'):
00148             next = 'manage_changeSettingsForm'
00149         REQUEST['RESPONSE'].redirect('/'.join((self.absolute_url(), id, next)))
00150 
00151     return self._getOb(id)
00152 
00153 def manage_availablePredicates(self):
00154     return getRegisteredPredicates()
00155 
00156 def manage_availableComponents(self):
00157     return getRegisteredComponents()
00158 
00159 manage_addPredicateForm = PageTemplateFile(
00160     '../www/predicateAdd', globals(),
00161     __name__='manage_addPredicateForm')
00162 
00163 constructors = (
00164   ('manage_addPredicateForm', manage_addPredicateForm),
00165   ('manage_addPredicate', manage_addPredicate),
00166   ('manage_availablePredicates', manage_availablePredicates),
00167   ('manage_availableComponents', manage_availableComponents),
00168 )