#-*- mode: python; mode: fold -*- # $Id: exportimport.py 31640 2006-01-15 19:22:29Z ogrisel $ """WAeUP Tool XML Adapter. An XML adapter tells the GenericSetup machinery howto im- / export persistent configuration that is relative to a specific CMF component such as our WAeUP Tool. GenericSetup uses the Zope3 interfaces and components machinery to find the right XML adapter for the right object. This is why we flagged our waeup tool with the `implements(IWAeUPTool)` class attribute and made an adapter association in the `configure.zcml` file. """ # Zope3 component architecture from zope.component import adapts from zope.interface import implements # Standard GenericSetup base classes and functions from Products.GenericSetup.utils import exportObjects from Products.GenericSetup.utils import importObjects from Products.GenericSetup.utils import XMLAdapterBase from Products.GenericSetup.utils import PropertyManagerHelpers from Products.CMFCore.utils import getToolByName # GenericSetup multi adapts a class that implement IWAeUP and a particular # ISetupEnvironment to and IBody (piece of XML configuration). from Products.GenericSetup.interfaces import IBody from Products.GenericSetup.interfaces import ISetupEnviron from Products.WAeUP.interfaces import IWAeUPTool TOOL = 'portal_waeup' NAME = 'waeup' # The exportWAeUP and importWAeUP methods are called by the specific # im- / export steps defined in the corresponding XML files in the # WAeUP/profiles/default/ directory. def setupStructure(context): ###( sections = getattr(context,'sections') portal = getattr(sections,'uniportal',None) if portal is None: sections.invokeFactory('University','uniportal') portal = getattr(context,'uniportal',None) portal.getContent().edit(mapping={'Title':SRPP_TITLE}) students = getattr(portal,'students',None) if students is None: portal.invokeFactory('StudentsFolder','students') students = getattr(portal,'students').getContent() students.edit(mapping={'Title':'Students'}) academics = getattr(portal,'academics',None) if academics is None: portal.invokeFactory('AcademicsFolder','academics') academics = getattr(portal,'academics') academics.getContent().edit(mapping={'Title':'Academics'}) installFaculties(academics) if not hasattr(portal,'accommodation'): portal.invokeFactory('AccoFolder','accommodation') accommodation = getattr(portal,'accommodation').getContent() accommodation.edit(mapping={'Title':'Accommodation'}) ###) def installFaculties( academics): ###( """install Universityspecific Faculies with Departments""" faculties = [ {'id': 'agriculture', ###( 'Title': 'Agriculture', 'departments': [ ## { 'id': 'dep1', ###( ## 'Title': 'One', ## }, ], },###) {'id': 'arts', ###( 'Title': 'Arts', 'departments': [ ## { 'id': 'dep1', ###( ## 'Title': 'One', ## }, ], },###) { 'id': 'science', 'Title': 'Science', 'departments': [ { 'id': 'bio', ###( 'Title': 'Biochemistry', }, { 'id': 'bot', 'Title': 'Botany', }, { 'id': 'che', 'Title': 'Chemistry', }, { 'id': 'com', 'Title': 'Computer Science', }, { 'id': 'geo', 'Title': 'Geologie', }, { 'id': 'mat', 'Title': 'Mathematics', }, { 'id': 'mic', 'Title': 'Microbiology', }, { 'id': 'opt', 'Title': 'Optometry', }, { 'id': 'phy', 'Title': 'Physics', }, { 'id': 'zoo', 'Title': 'Zoology', }, ], },###) ]###) for faculty in faculties: fid = faculty['id'] f = getattr(academics,fid,None) if f is None: #self.log('Creating Faculty %(id)s = %(Title)s' % faculty) academics.invokeFactory('Faculty', fid) f = getattr(academics,fid) f.getContent().edit(mapping=faculty) for department in faculty['departments']: #self.log('Checking Department %(id)s = %(Title)s' % department) did = department['id'] d = getattr(f,did,None) if d is None: #self.log('Creating Department %(id)s = %(Title)s' % department) f.invokeFactory('Department', did) d = getattr(f,did) d.getContent().edit(mapping=department) ###) def exportWAeUP(context): """Export our WAeUP tool configuration """ site = context.getSite() tool = getToolByName(site, TOOL, None) if tool is None: logger = context.getLogger(NAME) logger.info("Nothing to export.") return exportObjects(tool, '', context) def importWAeUP(context): """Import WAeUP tool configuration """ site = context.getSite() setupStructure(site) pass #site = context.getSite() #tool = getToolByName(site, TOOL) #importObjects(tool, '', context) # This the XMLAdapter itself. It encodes the im- / export logic that is specific # to our tool. `im- / exportObjects` functions will find it thanks to the zope # components machinery and the associations made in the configure.zcml file. class WAeUPXMLAdapter(XMLAdapterBase, PropertyManagerHelpers): """XML importer and exporter for the WAeUP tool. Hence this XMLAdapter is really simple. To get more complete examples of what XMLAdapters are meant to do, please have a look at the CPSSkins.exportimport.py or CPSDirectory.exportimport.py files, for instance. """ adapts(IWAeUPTool, ISetupEnviron) implements(IBody) _LOGGER_ID = NAME name = NAME def _exportNode(self): """Export the object as a DOM node. """ node = self._getObjectNode('object') node.appendChild(self._extractProperties()) self._logger.info("WAeUP tool exported.") return node def _importNode(self, node): """Import the object from the DOM node. """ if self.environ.shouldPurge(): self._purgeProperties() self._initProperties(node) self._logger.info("WAeUP tool imported.")