Back to index

plone3  3.1.7
exportimport.py
Go to the documentation of this file.
00001 from Products.GenericSetup.utils import exportObjects
00002 from Products.GenericSetup.utils import importObjects
00003 from Products.GenericSetup.utils import XMLAdapterBase
00004 
00005 from Products.CMFCore.utils import getToolByName
00006 
00007 from Products.PloneLanguageTool.interfaces import ILanguageTool
00008 
00009 class LanguageToolXMLAdapter(XMLAdapterBase):
00010 
00011     __used_for__ = ILanguageTool
00012 
00013     _LOGGER_ID = 'portal_languages'
00014 
00015     name = 'portal_languages'
00016 
00017     boolean_fields =  [ "use_path_negotiation", "use_cookie_negotiation",
00018                         "use_request_negotiation", "use_cctld_negotiation",
00019                         "use_combined_language_codes", "display_flags",
00020                         "start_neutral" ]
00021     list_fields = [ "supported_langs" ]
00022 
00023     def _exportNode(self):
00024         """Export the object as a DOM node"""
00025 
00026         node=self._extractProperties()
00027         self._logger.info('Plone language tool exported.')
00028 
00029         return node
00030 
00031     def _importNode(self, node):
00032         """Import the object from the DOM node"""
00033 
00034         if self.environ.shouldPurge():
00035             self._purgeProperties()
00036 
00037         self._initProperties(node)
00038 
00039         self._logger.info('Plone language tool imported.')
00040 
00041     def _purgeProperties(self):
00042         self.context.supported_langs = [ 'en' ]
00043         self.context.use_path_negotiation = 1
00044         self.context.use_cookie_negotiation = 1
00045         self.context.use_request_negotiation = 1
00046         self.context.use_cctld_negotiation = 0
00047         self.context.use_combined_language_codes = 0
00048         self.context.display_flags = 1
00049         self.context.start_neutral = 1
00050         self.context.setDefaultLanguage("en")
00051 
00052     def _initProperties(self, node):
00053         for child in node.childNodes:
00054             if child.nodeName in self.boolean_fields:
00055                 value = self._convertToBoolean(child.getAttribute('value'))
00056                 setattr(self.context, child.nodeName, value)
00057             elif child.nodeName in self.list_fields:
00058                 purge = self._convertToBoolean(child.getAttribute('purge') or 'True') 
00059                 if purge:
00060                     value = []
00061                 else:
00062                     value = getattr(self.context, child.nodeName)
00063                 for element in child.childNodes:
00064                     if element.nodeName=='element':
00065                         value.append(str(element.getAttribute('value')))
00066                 setattr(self.context, child.nodeName, value)
00067             elif child.nodeName=='default_language':
00068                 self.context.setDefaultLanguage(str(child.getAttribute('value')))
00069 
00070     def _extractProperties(self):
00071         node=self._doc.createElement('object')
00072         child=self._doc.createElement('default_language')
00073         child.setAttribute('value', self.context.getDefaultLanguage())
00074         node.appendChild(child)
00075 
00076         for field in self.boolean_fields:
00077             child=self._doc.createElement(field)
00078             child.setAttribute('value', str(bool(getattr(self.context, field))))
00079             node.appendChild(child)
00080 
00081         for field in self.list_fields:
00082             child=self._doc.createElement(field)
00083             value=getattr(self.context, field)
00084             for item in value:
00085                 element = self._doc.createElement('element')
00086                 element.setAttribute('value', item)
00087                 child.appendChild(element)
00088             node.appendChild(child)
00089 
00090         return node
00091 
00092 
00093 
00094 def importLanguageTool(context):
00095     """Import Plone language tool settings from an XML file.
00096     """
00097     site = context.getSite()
00098     tool = getToolByName(site, 'portal_languages')
00099 
00100     importObjects(tool, '', context)
00101 
00102 def exportLanguageTool(context):
00103     """Export Plone language tool settings as an XML file.
00104     """
00105     site = context.getSite()
00106     tool = getToolByName(site, 'portal_languages', None)
00107     if tool is None:
00108         logger = context.getLogger('portal_languages')
00109         logger.info('Nothing to export.')
00110         return
00111 
00112     exportObjects(tool, '', context)
00113 
00114