Back to index

plone3  3.1.7
exportimport.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 """ Kupu library tool setup handlers.
00014 """
00015 import os
00016 
00017 from AccessControl import ClassSecurityInfo
00018 from AccessControl.Permission import Permission
00019 from Globals import InitializeClass
00020 from Globals import package_home
00021 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00022 
00023 from Products.CMFCore.utils import getToolByName
00024 
00025 from Products.GenericSetup.utils import CONVERTER
00026 from Products.GenericSetup.utils import DEFAULT
00027 from Products.GenericSetup.utils import ExportConfiguratorBase
00028 from Products.GenericSetup.utils import ImportConfiguratorBase
00029 from Products.GenericSetup.utils import KEY
00030 
00031 try:
00032     from Products.CMFCore.permissions import ManagePortal
00033 except ImportError:
00034     from Products.CMFCore.CMFCorePermissions import ManagePortal
00035 
00036 from Products.PythonScripts.standard import Object
00037 
00038 _pkgdir = package_home( globals() )
00039 _xmldir = os.path.join( _pkgdir, 'xml' )
00040 
00041 #
00042 #   Configurator entry points
00043 #
00044 _FILENAME = 'kupu.xml'
00045 
00046 def importKupuSettings(context):
00047     """ Import kupu settings from an XML file.
00048     """
00049     site = context.getSite()
00050     kupu = getToolByName(site, 'kupu_library_tool', None)
00051     if kupu is None:
00052         return 'Kupu: No tool!'
00053 
00054     body = context.readDataFile(_FILENAME)
00055     if body is None:
00056         return 'Kupu: Nothing to import.'
00057 
00058     # now act on the settings we've retrieved
00059     configurator = KupuToolImportConfigurator(site, 'utf-8')
00060     info = configurator.parseXML(body)
00061 
00062     config = info['config'][0]
00063     parastyles = config['parastyles']
00064     tableclasses = config['tableclasses']
00065     stylewhitelist = config['filterstyles']
00066     classblacklist = config['filterclasses']
00067     excludedhtml = [ (f['tags'].split(' '), f['attributes'].split(' ')) for f in config['filters']]
00068     
00069     captioning = config['captioning']
00070     userefbrowser = config['userefbrowser']
00071     linkbyuid = config['linkbyuid']
00072     filtersourceedit = config['filtersourceedit']
00073     installbeforeunload = config['installbeforeunload']
00074 
00075     kupu.configure_kupu(
00076         table_classnames = tableclasses,
00077         parastyles=parastyles,
00078         style_whitelist = stylewhitelist,
00079         class_blacklist = classblacklist,
00080         installBeforeUnload=installbeforeunload,
00081         linkbyuid=linkbyuid,
00082         captioning=captioning,
00083         )
00084     kupu.set_html_exclusions(excludedhtml)
00085 
00086     # Set up resources
00087     # XXX whitelist/blacklist handling
00088     resources = info['resources']
00089     types = kupu.zmi_get_resourcetypes()
00090     kupu.deleteResourceTypes([ t.name for t in types])
00091     for k in resources:
00092         kupu.addResourceType(k['id'], k['types'], k['mode'])
00093     kupu.setDefaultResource(info['defaultresource'][0])
00094 
00095     if info.has_key('generatepreviews'):
00096         # This code generates preview URLs automatically from the most
00097         # appropriately sized image (if you have PIL installed) or just an
00098         # image field (if you don't).
00099         #
00100         # If you have content types which don't always have an image, or if
00101         # this picks the wrong preview URL, you might want to change these
00102         # expressions.
00103         PREVIEW_EXPR = 'string:${object_url}/%s'
00104         PREVIEW = [ { 'portal_type': type,
00105             'expression': PREVIEW_EXPR % image,
00106             'normal': None,
00107             'scalefield': image.split('_',1)[0],
00108             } for (type, image) in kupu.getPreviewable() ]
00109         kupu.updatePreviewActions(PREVIEW)
00110     else:
00111         PREVIEW = []
00112 
00113     preview = PREVIEW + list(info['previews'])
00114     kupu.updatePreviewActions(preview)
00115 
00116     # Set up libraries
00117     libraries = info['libraries']
00118     deflib = info['defaultlibrary'][0]
00119     libs = kupu.zmi_get_libraries()
00120     kupu.deleteLibraries(range(len(libs)))
00121     for lib in libraries:
00122         kupu.addLibrary(**lib)
00123     kupu.zmi_set_default_library(deflib)
00124 
00125     
00126     toolbar = info['toolbar'][0]['elements']
00127     globaltoolbar = info['globaltoolbar'][0]
00128     kupu.set_toolbar_filters(toolbar, globaltoolbar)
00129     
00130     return 'Kupu settings imported.'
00131 
00132 def exportKupuSettings(context):
00133     """ Export kupu settings as an XML file.
00134     """
00135     site = context.getSite()
00136 
00137     mhc = KupuToolExportConfigurator( site ).__of__( site )
00138     if mhc.getTool() is None:
00139         return 'Kupu not installed: no settings to export'
00140 
00141     text = mhc.generateXML()
00142 
00143     context.writeDataFile( _FILENAME, text, 'text/xml' )
00144 
00145     return 'Kupu settings exported.'
00146 
00147 
00148 class KupuToolExportConfigurator(ExportConfiguratorBase):
00149     """ Synthesize XML description of cc properties.
00150     """
00151     security = ClassSecurityInfo()
00152 
00153     security.declareProtected(ManagePortal, 'getTool')
00154     def getTool(self):
00155         """Get Kupu's tool"""
00156         kupu = getToolByName(self._site, 'kupu_library_tool', None)
00157         return kupu
00158 
00159     def getPreviews(self):
00160         typetool = getToolByName(self._site, 'portal_types')
00161         kupu = self.getTool()
00162         typeinfos = typetool.listTypeInfo()
00163         previewable = [t for t in typeinfos if kupu.getPreviewForType(t.getId())]
00164 
00165         res = []
00166         for t in typeinfos:
00167             previewable = kupu.getPreviewForType(t.getId())
00168             if not previewable:
00169                 continue
00170             portal_type =  t.getId()
00171             previewaction = kupu.getPreviewForType(portal_type)
00172             normalaction = kupu.getNormalViewForType(portal_type)
00173             scalefield = kupu.getScaleFieldForType(portal_type)
00174             defscale = kupu.getDefaultScaleForType(portal_type)
00175             classes = kupu.getClassesForType(portal_type)
00176             mediatype = kupu.getMediaForType(portal_type)
00177             res.append(dict(portal_type=portal_type,
00178                 previewaction=previewaction,
00179                 normalaction=normalaction,
00180                 scalefield=scalefield,
00181                 defscale=defscale,
00182                 classes=classes,
00183                 mediatype=mediatype,
00184             ))
00185         return res
00186 
00187     security.declarePrivate('_getExportTemplate')
00188     def _getExportTemplate(self):
00189         return PageTemplateFile('kupuExport.xml', _xmldir)
00190 
00191 InitializeClass(KupuToolExportConfigurator)
00192 
00193 
00194 class KupuToolImportConfigurator(ImportConfiguratorBase):
00195 
00196     def _getImportMapping(self):
00197         return {
00198             'kupu-settings':
00199                 {
00200                     'config': {},
00201                     'library': { KEY: 'libraries' },
00202                     'defaultlibrary': { DEFAULT: ('',) },
00203                     'resource': { KEY: 'resources' },
00204                     'defaultresource': { DEFAULT: ('linkable',) },
00205                     'preview': { KEY: 'previews', DEFAULT: () },
00206                     'generatepreviews': {},
00207                     'globaltoolbar': { DEFAULT: ('',) },
00208                     'toolbar': {}
00209                 },
00210             'config':
00211                 {
00212                     'captioning': {CONVERTER: self._convertToBoolean, DEFAULT: False},
00213                     'userefbrowser': {CONVERTER: self._convertToBoolean, DEFAULT: False},
00214                     'linkbyuid': {CONVERTER: self._convertToBoolean, DEFAULT: False},
00215                     'filtersourceedit': {CONVERTER: self._convertToBoolean, DEFAULT: True},
00216                     'installbeforeunload': {CONVERTER: self._convertToBoolean, DEFAULT: False},
00217                     'table': {KEY: 'tableclasses', DEFAULT:() },
00218                     'style': {KEY: 'parastyles', DEFAULT:() },
00219                     'filter': {KEY: 'filters', DEFAULT:() },
00220                     'filterstyle': {KEY: 'filterstyles', DEFAULT:() },
00221                     'filterclass': {KEY: 'filterclasses', DEFAULT:() },
00222                 },
00223             'table': { '#text': {KEY:None}, },
00224             'style': { '#text': {KEY:None}, },
00225             'filter':
00226                 {
00227                     'attributes': {},
00228                     'tags': {},
00229                 },
00230             'filterstyle': { '#text': {KEY:None}, },
00231             'filterclass': { '#text': {KEY:None}, },
00232             'library':
00233                 {
00234                     'src': {},
00235                     'uri': {},
00236                     'id': {},
00237                     'icon': {},
00238                     'title': {},
00239                 },
00240             'defaultlibrary': { '#text': { KEY:None }, },
00241             'resource':
00242                 {
00243                     'mode': {},
00244                     'id': {},
00245                     'type': { KEY:'types' },
00246                 },
00247             'defaultresource': { '#text': { KEY:None }, },
00248             'type':
00249                 { '#text': { KEY: None },
00250                 },
00251             'preview':
00252                 {
00253                     'portaltype': { KEY: 'portal_type' },
00254                     'preview': { KEY:'expression', DEFAULT: ''},
00255                     'normal': { DEFAULT: ''},
00256                     'scalefield': { DEFAULT: 'image' },
00257                     'defscale': { DEFAULT: 'image_preview' },
00258                     'previewclass': { KEY: 'classes', DEFAULT: ()},
00259                     'mediatype': { DEFAULT: ''},
00260                 },
00261             'previewclass': { '#text': {KEY:None}, },
00262             'generatepreviews': {},
00263             'globaltoolbar': { '#text': { KEY:None, DEFAULT: '' }, },
00264             'toolbar':
00265                 {
00266                     'element': { KEY: 'elements' },
00267                 },
00268             'element':
00269                 {
00270                     'visible': {CONVERTER: self._convertToBoolean, DEFAULT: True},
00271                     'id': { '#text': None },
00272                     'expression':  { KEY: 'override', DEFAULT: '' },
00273                 },
00274             }
00275 
00276 InitializeClass(KupuToolImportConfigurator)