Back to index

plone3  3.1.7
Modifiers.py
Go to the documentation of this file.
00001 #########################################################################
00002 # Copyright (c) 2005 Alberto Berti, Gregoire Weber.
00003 # All Rights Reserved.
00004 #
00005 # This file is part of CMFEditions.
00006 #
00007 # CMFEditions is free software; you can redistribute it and/or modify
00008 # it under the terms of the GNU General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or
00010 # (at your option) any later version.
00011 #
00012 # CMFEditions is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 # GNU General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU General Public License
00018 # along with CMFEditions; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00020 #########################################################################
00021 """Modifier wrappers
00022 
00023 $Id: Modifiers.py,v 1.3 2005/06/09 13:07:28 duncanb Exp $
00024 """
00025 
00026 from Globals import InitializeClass
00027 from Acquisition import aq_base, aq_parent, aq_inner
00028 
00029 from OFS.SimpleItem import SimpleItem
00030 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00031 from Products.PageTemplates.Expressions import SecureModuleImporter
00032 from Products.PageTemplates.Expressions import getEngine
00033 
00034 from Products.CMFCore.utils import getToolByName
00035 from Products.CMFCore.Expression import Expression
00036 
00037 from Products.CMFEditions.interfaces.IModifier import IConditionalTalesModifier
00038 from Products.CMFEditions.interfaces.IModifier import IConditionalModifier
00039 
00040 manage_addModifierForm = PageTemplateFile('www/modifierAddForm.pt',
00041                                           globals(),
00042                                           __name__='manage_addModifierForm')
00043 
00044 class ConditionalModifier(SimpleItem):
00045     """This is a wrapper for a modifier.
00046     """
00047 
00048     __implements__ = (
00049         IConditionalModifier,
00050     )
00051 
00052     modifierEditForm = PageTemplateFile('www/modifierEditForm.pt',
00053                                         globals(),
00054                                         __name__='modifierEditForm')
00055     modifierEditForm._owner = None
00056     manage = manage_main = modifierEditForm
00057     manage_options = ({'label' : 'Edit', 'action' : 'modifierEditForm'},) + SimpleItem.manage_options[:]
00058 
00059     def __init__(self, id, modifier, title=''):
00060         """See IConditionalModifier.
00061         """
00062         self.id = str(id)
00063         self.title = str(title)
00064         self.meta_type = 'edmod_%s' % id
00065         self._modifier = modifier
00066         self._enabled = False
00067 
00068     def edit(self, enabled=None, title='', REQUEST=None):
00069         """See IConditionalModifier.
00070         """
00071         self.title = title
00072 
00073         if enabled is not None and (enabled=='True' or enabled==True):
00074             self._enabled = enabled
00075         else:
00076             self._enabled = False
00077 
00078         if REQUEST:
00079             REQUEST.set("manage_tabs_message", "Changed")
00080             return self.modifierEditForm(self, REQUEST)
00081 
00082     def isApplicable(self, obj, portal=None):
00083         """See IConditionalModifier.
00084         """
00085         # check if disabled or an empty condition
00086         if self._enabled:
00087             return True
00088 
00089     def isEnabled(self):
00090         """See IConditionalModifier.
00091         """
00092         return self._enabled
00093 
00094     def getModifier(self):
00095         """See IConditionalModifier.
00096         """
00097         return self._modifier
00098 
00099 InitializeClass(ConditionalModifier)
00100 
00101 
00102 manage_addTalesModifierForm = PageTemplateFile('www/talesModifierAddForm.pt',
00103                                                globals(),
00104                                                __name__='manage_addTalesModifierForm')
00105 
00106 class ConditionalTalesModifier(ConditionalModifier):
00107     """This is a wrapper with a tales condition for a modifier.
00108     """
00109 
00110     __implements__ = (
00111         IConditionalTalesModifier,
00112     )
00113 
00114     modifierEditForm = PageTemplateFile('www/talesModifierEditForm.pt',
00115                                         globals(),
00116                                         __name__='modifierEditForm')
00117     manage_options = ({'label' : 'Edit', 'action' : 'modifierEditForm'},) + ConditionalModifier.manage_options[:]
00118 
00119     def __init__(self, id, modifier, title=''):
00120         """See IConditionalTalesModifier.
00121         """
00122         ConditionalModifier.__init__(self, id, modifier, title)
00123         self._condition = None
00124 
00125     def edit(self, enabled=None, condition=None, title='', REQUEST=None):
00126         """See IConditionalTalesModifier.
00127         """
00128         ConditionalModifier.edit(self, enabled, title)
00129         if condition is not None and condition != self.getTalesCondition():
00130             self._condition = Expression(condition)
00131 
00132         if REQUEST:
00133             REQUEST.set("manage_tabs_message", "Changed")
00134             return self.modifierEditForm(self, REQUEST)
00135 
00136     def isApplicable(self, obj, portal=None):
00137         """See IConditionalTalesModifier.
00138         """
00139         # check if disabled or an empty condition
00140         if not self._enabled or not self.getTalesCondition():
00141             return False
00142 
00143         # create the expression context and return result
00144         context = createExpressionContext(obj, portal)
00145         return self._condition(context)
00146 
00147     def getTalesCondition(self):
00148         """See IConditionalTalesModifier.
00149         """
00150         return getattr(self._condition, 'text', '')
00151 
00152 InitializeClass(ConditionalTalesModifier)
00153 
00154 
00155 def createExpressionContext(obj, portal=None, **more_symbols):
00156     """Creates a valid context for the expression.
00157 
00158     Tal expressions need a context in order to do the evaluation.
00159     obj is the object that will be mapped to "object" in the
00160     expression's context.
00161     Other symbols like "repo_clone" and "obj_clone" can be passed as keyword
00162     arguments.
00163     """
00164 
00165     def findNextFolderishParent(obj):
00166         """Try to find the folder of the given object by aquisition.
00167 
00168         XXX what's the correct strategy in Zope2 land to check for a folder?
00169             what's most relyable?
00170             a) check if isPrincipiaFolderish is True?
00171             b) check if the object is an ObjectManager?
00172             c) other?
00173 
00174             We have to do the right thing here to get things working
00175             correctly. I hope all the products out there do the right
00176             thing also ...
00177         """
00178         # XXX propose this check (should be the same):
00179         #    if aq_base(obj) is obj:
00180         if obj is None or not hasattr(obj, 'aq_base'):
00181             folder = None
00182         else:
00183             folder = obj
00184             # Search up the containment hierarchy until we find an
00185             # obj that claims it's a folder.
00186             while folder is not None:
00187                 if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
00188                     # found it.
00189                     break
00190                 else:
00191                     folder = aq_parent(aq_inner(folder))
00192         return folder
00193 
00194     try:
00195         obj_url = obj.absolute_url()
00196     except AttributeError:
00197         obj_url = ''
00198 
00199     # use the portal if folder lookup fails due to an unwrapped obj
00200     folder = findNextFolderishParent(obj) or portal
00201 
00202     pm = getToolByName(portal, 'portal_membership', None)
00203     if pm is None or pm.isAnonymousUser():
00204         member = None
00205     else:
00206         member = pm.getAuthenticatedMember()
00207 
00208     try:
00209         meta_type = obj.meta_type
00210     except AttributeError:
00211         meta_type = None
00212 
00213     try:
00214         portal_type = obj.getPortalTypeName()
00215     except AttributeError:
00216         portal_type  = None
00217 
00218     data = {
00219         'object_url':   obj_url,
00220         'folder_url':   folder is not None and folder.absolute_url() or '',
00221         'portal_url':   portal is not None and portal.absolute_url() or '',
00222         'object':       obj,
00223         'folder':       folder,
00224         'portal':       portal,
00225         'nothing':      None,
00226         'request':      getattr(obj, 'REQUEST', None),
00227         'modules':      SecureModuleImporter,
00228         'member':       member,
00229         'meta_type':    meta_type,
00230         'portal_type':  portal_type,
00231         }
00232     data.update(more_symbols)
00233     return getEngine().getContext(data)