Back to index

plone3  3.1.7
formcontroller.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (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 ##############################################################################
00013 """CMFFormController setup handlers.
00014 
00015 $Id: formcontroller.py 41269 2007-04-28 19:03:13Z wichert $
00016 """
00017 
00018 import os
00019 from AccessControl import ClassSecurityInfo
00020 from Globals import InitializeClass
00021 from Globals import package_home
00022 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00023 
00024 from Products.CMFCore.utils import getToolByName
00025 from Products.CMFCore.permissions import ManagePortal
00026 
00027 from Products.GenericSetup.utils import DEFAULT, KEY
00028 from Products.GenericSetup.utils import ImportConfiguratorBase, ExportConfiguratorBase
00029 
00030 _pkgdir = package_home( globals() )
00031 _xmldir = os.path.join( _pkgdir, 'xml' )
00032 
00033 from Products.CMFFormController.FormAction import FormAction
00034 from Products.CMFFormController.FormValidator import FormValidator
00035 
00036 #
00037 #   Configurator entry points
00038 #
00039 _FILENAME = 'cmfformcontroller.xml'
00040 
00041 def importCMFFormController(context):
00042     """ Import CMFFormController settings from an XML file.
00043     """
00044     site = context.getSite()
00045     fc = getToolByName(site, 'portal_form_controller', None)
00046     if fc is None:
00047         return 'CMFFormController: No tool!'
00048 
00049     body = context.readDataFile(_FILENAME)
00050     if body is None:
00051         return 'CMFFormController: Nothing to import.'
00052 
00053     if context.shouldPurge():
00054         fc.__init__()
00055 
00056     # now act on the settings we've retrieved
00057     configurator = CMFFormControllerImportConfigurator(site)
00058     fc_info = configurator.parseXML(body)
00059 
00060     for validator in fc_info['validators']:
00061         fc.validators.set(FormValidator(validator['object_id'],
00062                                         validator.get('context_type', ''),
00063                                         validator.get('button', ''),
00064                                         str(validator['validators']).split(',')))
00065     for action in fc_info['actions']:
00066         fc.actions.set(FormAction(action['object_id'],
00067                                   action['status'],
00068                                   action.get('context_type', ''),
00069                                   action.get('button', ''),
00070                                   action['action_type'],
00071                                   str(action['action_arg'])))
00072 
00073     return 'CMFFormController settings imported.'
00074 
00075 def exportCMFFormController(context):
00076     """ Export CMFFormController settings as an XML file.
00077     """
00078     site = context.getSite()
00079 
00080     fc = getToolByName(site, 'portal_form_controller', None)
00081     if fc is None:
00082         return 'CMFFormController: Nothing to export.'
00083 
00084     fcc = CMFFormControllerExportConfigurator( site ).__of__( site )
00085     text = fcc.generateXML()
00086 
00087     context.writeDataFile( _FILENAME, text, 'text/xml' )
00088 
00089     return 'CMFFormController settings exported.'
00090 
00091 
00092 class CMFFormControllerExportConfigurator(ExportConfiguratorBase):
00093     """ Synthesize XML description of CMFFormController properties.
00094     """
00095     security = ClassSecurityInfo()
00096 
00097     security.declareProtected( ManagePortal, 'listValidators' )
00098     def listValidators(self):
00099         """ Return a list of mappings describing the tool's validators.
00100         """
00101         fc = getToolByName(self._site, 'portal_form_controller')
00102 
00103         for validator in fc.listFormValidators():
00104             yield {'object_id': validator.getObjectId(),
00105                    'context_type': validator.getContextType(),
00106                    'button': validator.getButton(),
00107                    'validators': validator.getValidators(),
00108                   }
00109 
00110     def listActions(self):
00111         """ Return a list of mappings describing the tool's actions.
00112         """
00113         fc = getToolByName(self._site, 'portal_form_controller')
00114 
00115         for action in fc.listFormActions():
00116             yield {'object_id': action.getObjectId(),
00117                    'status': action.getStatus(),
00118                    'context_type': action.getContextType(),
00119                    'button': action.getButton(),
00120                    'action_type': action.getActionType(),
00121                    'action_arg': action.getActionArg()
00122                   }
00123 
00124     security.declarePrivate('_getExportTemplate')
00125     def _getExportTemplate(self):
00126 
00127         return PageTemplateFile('fcExport.xml', _xmldir)
00128 
00129 InitializeClass(CMFFormControllerExportConfigurator)
00130 
00131 class CMFFormControllerImportConfigurator(ImportConfiguratorBase):
00132 
00133     def _getImportMapping(self):
00134         # Each key represents the name of an xml node
00135         # 'cmfformcontroller' is the top level node
00136         # It will contain nodes of type 'action' and 'validator';
00137         # 'action' and 'validator' nodes will be stored in the
00138         # dict generated by import in tuples with keys 'actions'
00139         # and 'validators', respectively
00140         return {
00141           'cmfformcontroller': {
00142             'action':     {KEY: 'actions', DEFAULT: ()},
00143             'validator':  {KEY: 'validators', DEFAULT: ()},
00144           },
00145           'action':
00146              { 'object_id':    {},
00147                'status':       {},
00148                'context_type': {},
00149                'button':       {},
00150                'action_type':  {},
00151                'action_arg':   {},
00152              },
00153           'validator':
00154              { 'object_id':    {},
00155                'context_type': {},
00156                'button':       {},
00157                'validators':   {},
00158              }
00159           }
00160 
00161 InitializeClass(CMFFormControllerImportConfigurator)