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 """ GenericSetup export / import support for topics / criteria.
00014 
00015 $Id: exportimport.py 71021 2006-11-01 16:23:56Z yuppie $
00016 """
00017 
00018 from xml.dom.minidom import parseString
00019 
00020 from zope.interface import implements
00021 
00022 from Products.GenericSetup.interfaces import IFilesystemExporter
00023 from Products.GenericSetup.interfaces import IFilesystemImporter
00024 from Products.GenericSetup.content import FolderishExporterImporter
00025 
00026 try:
00027     from Products.GenericSetup.utils import PageTemplateResource
00028 except ImportError: # BBB:  no egg support
00029     from Products.PageTemplates.PageTemplateFile \
00030         import PageTemplateFile as PageTemplateResource
00031 
00032 
00033 from Topic import Topic
00034 
00035 class TopicExportImport(FolderishExporterImporter):
00036     """ Dump topic criteria to / from an XML file.
00037     """
00038     implements(IFilesystemExporter, IFilesystemImporter)
00039 
00040     encoding = None
00041     _FILENAME = 'criteria.xml'
00042     _ROOT_TAGNAME = 'criteria'
00043 
00044     def __init__(self, context):
00045         self.context = context
00046 
00047     def listExportableItems(self):
00048         """ See IFilesystemExporter.
00049         """
00050         criteria_metatypes = self.context._criteria_metatype_ids()
00051         return [x for x in FolderishExporterImporter.listExportableItems(self)
00052                    if x[1].meta_type not in criteria_metatypes]
00053 
00054     def export(self, export_context, subdir, root=False):
00055         """ See IFilesystemExporter.
00056         """
00057         FolderishExporterImporter.export(self, export_context, subdir, root)
00058         template = PageTemplateResource('xml/%s' % self._FILENAME,
00059                                         globals()).__of__(self.context)
00060         export_context.writeDataFile('%s/criteria.xml' % self.context.getId(),
00061                                      template(info=self._getExportInfo()),
00062                                      'text/xml',
00063                                      subdir,
00064                                     )
00065 
00066     def import_(self, import_context, subdir, root=False):
00067         """ See IFilesystemImporter
00068         """
00069         FolderishExporterImporter.import_(self, import_context, subdir, root)
00070 
00071         self.encoding = import_context.getEncoding()
00072 
00073         if import_context.shouldPurge():
00074             self._purgeContext()
00075 
00076         data = import_context.readDataFile('%s/criteria.xml'
00077                                                 % self.context.getId(),
00078                                            subdir)
00079 
00080         if data is not None:
00081             dom = parseString(data)
00082             root = dom.firstChild
00083             assert root.tagName == self._ROOT_TAGNAME
00084             self._updateFromDOM(root)
00085 
00086     def _getNodeAttr(self, node, attrname, default=None):
00087         attr = node.attributes.get(attrname)
00088         if attr is None:
00089             return default
00090         value = attr.value
00091         if isinstance(value, unicode) and self.encoding is not None:
00092             value = value.encode(self.encoding)
00093         return value
00094 
00095     def _purgeContext(self):
00096         context = self.context
00097         criterion_ids = context.objectIds(context._criteria_metatype_ids())
00098         for criterion_id in criterion_ids:
00099             self.context._delObject(criterion_id)
00100 
00101     def _updateFromDOM(self, root):
00102         for criterion in root.getElementsByTagName('criterion'):
00103             c_type = self._getNodeAttr(criterion, 'type', None)
00104             field = self._getNodeAttr(criterion, 'field', None)
00105             attributes = {}
00106             for attribute in criterion.getElementsByTagName('attribute'):
00107                 name = self._getNodeAttr(attribute, 'name', None)
00108                 value = self._getNodeAttr(attribute, 'value', None)
00109                 if name == 'reversed':
00110                     value = value in ('True', 'true', '1')
00111                 attributes[name] = value
00112 
00113             self.context.addCriterion(field, c_type)
00114             added = self.context.getCriterion(field)
00115             added.edit(**attributes)
00116 
00117     def _getExportInfo(self):
00118         context = self.context
00119         criterion_info = []
00120 
00121         for criterion_id, criterion in context.objectItems(
00122                                         context._criteria_metatype_ids()):
00123 
00124             # SortCriterion stashes the 'field' as 'index'.
00125             field = getattr(criterion, 'index', criterion.field)
00126 
00127             info = {'criterion_id': criterion_id,
00128                     'type': criterion.meta_type,
00129                     'field': field,
00130                     'attributes': []
00131                    }
00132 
00133             attributes = info['attributes']
00134             for attrname in criterion.editableAttributes():
00135                 value = getattr(criterion, attrname)
00136                 if type(value) in (tuple, list):
00137                     value = ','.join(value)
00138                 attributes.append((attrname, value))
00139 
00140             criterion_info.append(info)
00141 
00142         return {'criteria': criterion_info,
00143                }
00144 
00145     def _mustPreserve(self):
00146         context = self.context
00147         keepers = FolderishExporterImporter._mustPreserve(self)
00148         keepers.extend(context.objectItems(context._criteria_metatype_ids()))
00149         return keepers
00150 
00151 
00152 class SubtopicFactory(object):
00153 
00154     def __init__(self, context):
00155         self.context = context
00156 
00157     def __call__(self, id):
00158         topic = Topic(id)
00159         topic.portal_type = 'Topic'
00160         return topic