Back to index

plone3  3.1.7
ControllerValidator.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 
00015 """Controller Python Scripts Product
00016 
00017 This product provides support for Script objects containing restricted
00018 Python code.
00019 """
00020 
00021 import os
00022 from Globals import package_home
00023 import AccessControl
00024 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00025 from OFS.SimpleItem import SimpleItem
00026 from urllib import quote
00027 from Shared.DC.Scripts.Script import BindingsUI
00028 from OFS.History import Historical
00029 from OFS.Cache import Cacheable
00030 from Products.CMFCore.utils import getToolByName
00031 from Script import PythonScript as BaseClass
00032 from ControllerBase import ControllerBase
00033 from interfaces import IControllerValidator
00034 
00035 from zope.interface import implements
00036 
00037 # Track the Python bytecode version
00038 import imp
00039 Python_magic = imp.get_magic()
00040 del imp
00041 
00042 # This should only be incremented to force recompilation.
00043 Script_magic = 3
00044 _log_complaint = (
00045     'Some of your Scripts have stale code cached.  Since Zope cannot'
00046     ' use this code, startup will be slightly slower until these Scripts'
00047     ' are edited. You can automatically recompile all Scripts that have'
00048     ' this problem by visiting /manage_addProduct/PythonScripts/recompile'
00049     ' of your server in a browser.')
00050 
00051 _default_file = os.path.join(package_home(globals()),
00052                              'www', 'default_vpy')
00053 
00054 _marker = []  # Create a new marker object
00055 
00056 # ###########################################################################
00057 # Product registration and Add support
00058 manage_addControllerValidatorForm = PageTemplateFile('www/vpyAdd', globals())
00059 manage_addControllerValidatorForm.__name__='manage_addControllerValidatorForm'
00060 
00061 def manage_addControllerValidator(self, id, REQUEST=None, submit=None):
00062     """Add a Python script to a folder.
00063     """
00064     id = str(id)
00065     id = self._setObject(id, ControllerValidator(id))
00066     if REQUEST is not None:
00067         file = REQUEST.form.get('file', '')
00068         if not isinstance(file, str):
00069             file = file.read()
00070         if not file:
00071             file = open(_default_file).read()
00072         self._getOb(id).write(file)
00073         try: u = self.DestinationURL()
00074         except: u = REQUEST['URL1']
00075         if submit==" Add and Edit ": u="%s/%s" % (u,quote(id))
00076         REQUEST.RESPONSE.redirect(u+'/manage_main')
00077     return ''
00078 
00079 
00080 class ControllerValidator(BaseClass, ControllerBase):
00081     """Web-callable scripts written in a safe subset of Python.
00082 
00083     The function may include standard python code, so long as it does
00084     not attempt to use the "exec" statement or certain restricted builtins.
00085     """
00086 
00087     meta_type='Controller Validator'
00088 
00089     implements(IControllerValidator)
00090 
00091     manage_options = (
00092         {'label':'Edit',
00093          'action':'ZPythonScriptHTML_editForm',
00094          'help': ('PythonScripts', 'PythonScript_edit.stx')},
00095         ) + BindingsUI.manage_options + (
00096         {'label':'Test',
00097          'action':'ZScriptHTML_tryForm',
00098          'help': ('PythonScripts', 'PythonScript_test.stx')},
00099 #        {'label':'Actions','action':'manage_formActionsForm'},
00100         {'label':'Proxy',
00101          'action':'manage_proxyForm',
00102          'help': ('OFSP','DTML-DocumentOrMethod_Proxy.stx')},
00103         ) + Historical.manage_options + SimpleItem.manage_options + \
00104         Cacheable.manage_options
00105 
00106     is_validator = 1
00107 
00108     security = AccessControl.ClassSecurityInfo()
00109     security.declareObjectProtected('View')
00110 
00111     security.declareProtected('View', '__call__')
00112 
00113     security.declareProtected('View management screens',
00114       'ZPythonScriptHTML_editForm', 'manage_main', 'read',
00115       'ZScriptHTML_tryForm', 'PrincipiaSearchSource',
00116       'document_src', 'params', 'body')
00117 
00118     security.declareProtected('Change Python Scripts',
00119       'ZPythonScriptHTML_editAction',
00120       'ZPythonScript_setTitle', 'ZPythonScript_edit',
00121       'ZPythonScriptHTML_upload', 'ZPythonScriptHTML_changePrefs')
00122 
00123 
00124     def __init__(self, *args, **kwargs):
00125 #        self.actions = FormActionContainer()
00126         return ControllerValidator.inheritedAttribute('__init__')(self, *args, **kwargs)
00127 
00128 
00129     def __call__(self, *args, **kwargs):
00130         result = ControllerValidator.inheritedAttribute('__call__')(self, *args, **kwargs)
00131 #        if getattr(result, '__class__', None) == ControllerState and not result._isValidating():
00132 #            return self.getNext(result, self.REQUEST)
00133         return result
00134 
00135     def _getState(self):
00136         return getToolByName(self, 'portal_form_controller').getState(self, is_validator=1)
00137 
00138     def _notifyOfCopyTo(self, container, op=0):
00139         # BaseClass.inheritedAttribute('notifyOfCopyTo')(self, container, op)
00140         self._base_notifyOfCopyTo(container, op)
00141 
00142     def manage_afterAdd(self, object, container):
00143         BaseClass.inheritedAttribute('manage_afterAdd')(self, object, container)
00144         self._base_manage_afterAdd(object, container)
00145 
00146     def manage_afterClone(self, object):
00147         BaseClass.inheritedAttribute('manage_afterClone')(self, object)
00148         self._base_manage_afterClone(object)