Back to index

plone3  3.1.7
FSControllerPythonScript.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE
00011 #
00012 # THIS FILE CONTAINS MODIFIED CODE FROM ZOPE 2.6.2
00013 ##############################################################################
00014 """ Customizable controlled python scripts that come from the filesystem.
00015 """
00016 
00017 import copy
00018 from zope.tales.tales import CompilerError
00019 
00020 import Globals, Acquisition
00021 from AccessControl import ClassSecurityInfo
00022 from OFS.Cache import Cacheable
00023 from Products.CMFCore.DirectoryView import registerFileExtension, registerMetaType
00024 from Products.CMFCore.permissions import View
00025 from Products.CMFCore.utils import getToolByName
00026 from Script import FSPythonScript as BaseClass
00027 from ControllerPythonScript import ControllerPythonScript
00028 from ControllerState import ControllerState
00029 from FSControllerBase import FSControllerBase
00030 from utils import log, logException
00031 from interfaces import IControllerPythonScript
00032 
00033 from zope.interface import implements
00034 
00035 class FSControllerPythonScript (FSControllerBase, BaseClass):
00036     """FSControllerPythonScripts act like Controller Python Scripts but are not
00037     directly modifiable from the management interface."""
00038 
00039     meta_type = 'Filesystem Controller Python Script'
00040 
00041     implements(IControllerPythonScript)
00042 
00043     manage_options=(
00044            (
00045             {'label':'Customize', 'action':'manage_main'},
00046             {'label':'Test', 'action':'ZScriptHTML_tryForm'},
00047             {'label':'Validation','action':'manage_formValidatorsForm'},
00048             {'label':'Actions','action':'manage_formActionsForm'},
00049            ) + Cacheable.manage_options)
00050 
00051     is_validator = 0
00052 
00053     # Use declarative security
00054     security = ClassSecurityInfo()
00055     security.declareObjectProtected(View)
00056 
00057     def __init__(self, id, filepath, fullname=None, properties=None):
00058         BaseClass.__init__(self, id, filepath, fullname, properties)
00059         self.filepath = self._filepath = filepath  # add _filepath for compatibility with pending CMF patch
00060         try:
00061             self._read_action_metadata(self.getId(), filepath)
00062             self._read_validator_metadata(self.getId(), self.filepath)
00063         except (ValueError, CompilerError), e:
00064             log(summary='metadata error', text='file = %s' % filepath)
00065             raise
00066 
00067 
00068     def _updateFromFS(self):
00069         # workaround for Python 2.1 multiple inheritance lameness
00070         return self._baseUpdateFromFS()
00071 
00072     
00073     def _readMetadata(self):
00074         # workaround for Python 2.1 multiple inheritance lameness
00075         return self._baseReadMetadata()
00076 
00077     
00078     def _readFile(self, reparse):
00079         BaseClass._readFile(self, reparse)
00080         self._readMetadata()
00081 
00082 
00083     def __call__(self, *args, **kwargs):
00084         REQUEST = self.REQUEST
00085         controller = getToolByName(self, 'portal_form_controller')
00086         controller_state = controller.getState(self, is_validator=0)
00087         controller_state = self.getButton(controller_state, REQUEST)
00088         validators = self.getValidators(controller_state, REQUEST).getValidators()
00089 
00090         # put all arguments into a dict
00091         c = self.func_code
00092         param_names = c.co_varnames[:c.co_argcount]
00093         argdict = {}
00094         index = 0
00095         # grab the names for positional arguments out of the function code
00096         for a in args:
00097             argdict[param_names[index]] = a
00098             index += 1
00099         argdict.update(kwargs)
00100 
00101         controller_state = controller.validate(controller_state, REQUEST, validators, argdict)
00102 
00103         if controller_state.getStatus() == 'success':
00104             result = FSControllerPythonScript.inheritedAttribute('__call__')(self, *args, **kwargs)
00105             if getattr(result, '__class__', None) == ControllerState and not result._isValidating():
00106                 return self.getNext(result, self.REQUEST)
00107             return result
00108         else:
00109             return self.getNext(controller_state, self.REQUEST)
00110 
00111 
00112     security.declarePrivate('manage_afterAdd')
00113     def manage_afterAdd(self, object, container):
00114         try:
00115             BaseClass.manage_afterAdd(self, object, container)
00116             # Re-read .metadata after adding so that we can do validation checks
00117             # using information in portal_form_controller.  Since manage_afterAdd
00118             # is not guaranteed to run, we also call these in __init__
00119             self._read_action_metadata(self.getId(), self.filepath)
00120             self._read_validator_metadata(self.getId(), self.filepath)
00121         except:
00122             log(summary='metadata error', text='file = %s' % self.filepath)
00123             logException()
00124             raise
00125 
00126     def _createZODBClone(self):
00127         """Create a ZODB (editable) equivalent of this object."""
00128         obj = ControllerPythonScript(self.getId(), filepath=self.filepath)
00129         obj.write(self.read())
00130         obj.validators = copy.copy(Acquisition.aq_base(self.validators))  # XXX - don't forget to enable this
00131         obj.actions = copy.copy(Acquisition.aq_base(self.actions))
00132         return obj
00133 
00134 
00135     security.declarePublic('writableDefaults')
00136     def writableDefaults(self):
00137         """Can default actions and validators be modified?"""
00138         return 0
00139 
00140     def _getState(self):
00141         return getToolByName(self, 'portal_form_controller').getState(self, is_validator=0)
00142 
00143 
00144 Globals.InitializeClass(FSControllerPythonScript)
00145 
00146 
00147 registerFileExtension('cpy', FSControllerPythonScript)
00148 registerMetaType('Controller Python Script', FSControllerPythonScript)