Back to index

plone3  3.1.7
difftool.py
Go to the documentation of this file.
00001 from Products.CMFDiffTool.interfaces import IDiffTool
00002 from Products.GenericSetup.utils import XMLAdapterBase
00003 from Products.GenericSetup.utils import importObjects
00004 from Products.GenericSetup.utils import exportObjects
00005 from Products.CMFCore.utils import getToolByName
00006 
00007 from zope.interface import implements
00008 
00009 class DiffToolXMLAdapter(XMLAdapterBase):
00010     """In- and exporter for DiffTool.
00011     """
00012     implements(IDiffTool)
00013 
00014     name = 'diff_tool'
00015 
00016     def _exportNode(self):
00017         """Export the object as a DOM node.
00018         """
00019         node=self._doc.createElement("object")
00020         node.appendChild(self._extractDiffToolSettings())
00021 
00022         self._logger.info("DiffTool settings exported.")
00023         return node
00024 
00025     def _importNode(self, node):
00026         if self.environ.shouldPurge():
00027             self._purgeDiffToolSettings()
00028 
00029         self._initDiffToolSettings(node)
00030         self._logger.info("DiffTool settings imported.")
00031 
00032     def _purgeDiffToolSettings(self):
00033         self.context.manage_editDiffFields({})
00034 
00035     def _initDiffToolSettings(self, node):
00036         for child in node.childNodes:
00037             if child.nodeName == "difftypes":
00038                 for type_entry in child.getElementsByTagName("type"):
00039                     ptype = type_entry.getAttribute("portal_type")
00040                     fields = {}
00041                     for field in type_entry.getElementsByTagName("field"):
00042                         name = field.getAttribute("name")
00043                         diff = field.getAttribute("difftype")
00044                         fields[name] = diff
00045                         self.context.setDiffForPortalType(ptype, fields)
00046 
00047 
00048     def _extractDiffToolSettings(self):
00049         node=self._doc.createElement("difftypes")
00050         ttool = getToolByName(self.context, "portal_types")
00051         for ptype in ttool.listContentTypes():
00052             diffs = self.context.getDiffForPortalType(ptype)
00053             if diffs:
00054                 child=self._doc.createElement("type")
00055                 child.setAttribute("portal_type", ptype)
00056                 node.appendChild(child)
00057             for field_name, diff in diffs.items():
00058                 field=self._doc.createElement("field")
00059                 field.setAttribute("name", field_name)
00060                 field.setAttribute("difftype", diff)
00061                 child.appendChild(field)
00062         return node
00063 
00064 
00065 def importDiffTool(context):
00066     """Import Factory Tool configuration.
00067     """
00068     site = context.getSite()
00069     tool = getToolByName(site, 'portal_diff', None)
00070 
00071     if tool is not None:
00072         importObjects(tool, '', context)
00073 
00074 
00075 def exportDiffTool(context):
00076     """Export Factory Tool configuration.
00077     """
00078     site = context.getSite()
00079     tool = getToolByName(site, 'portal_diff', None)
00080     if tool is None:
00081         logger = context.getLogger("difftool")
00082         logger.info("Nothing to export.")
00083         return
00084 
00085     exportObjects(tool, '', context)