[200] | 1 | #-*- mode: python; mode: fold -*- |
---|
[199] | 2 | # $Id: exportimport.py 31640 2006-01-15 19:22:29Z ogrisel $ |
---|
| 3 | """WAeUP Tool XML Adapter. |
---|
| 4 | |
---|
| 5 | An XML adapter tells the GenericSetup machinery howto im- / export persistent |
---|
| 6 | configuration that is relative to a specific CMF component such as our WAeUP |
---|
| 7 | Tool. |
---|
| 8 | |
---|
| 9 | GenericSetup uses the Zope3 interfaces and components machinery to find the |
---|
| 10 | right XML adapter for the right object. This is why we flagged our waeup tool |
---|
| 11 | with the `implements(IWAeUPTool)` class attribute and made an adapter |
---|
| 12 | association in the `configure.zcml` file. |
---|
| 13 | """ |
---|
| 14 | |
---|
| 15 | # Zope3 component architecture |
---|
| 16 | from zope.component import adapts |
---|
| 17 | from zope.interface import implements |
---|
| 18 | |
---|
| 19 | # Standard GenericSetup base classes and functions |
---|
| 20 | from Products.GenericSetup.utils import exportObjects |
---|
| 21 | from Products.GenericSetup.utils import importObjects |
---|
| 22 | from Products.GenericSetup.utils import XMLAdapterBase |
---|
| 23 | from Products.GenericSetup.utils import PropertyManagerHelpers |
---|
| 24 | |
---|
| 25 | from Products.CMFCore.utils import getToolByName |
---|
| 26 | |
---|
| 27 | # GenericSetup multi adapts a class that implement IWAeUP and a particular |
---|
| 28 | # ISetupEnvironment to and IBody (piece of XML configuration). |
---|
| 29 | from Products.GenericSetup.interfaces import IBody |
---|
| 30 | from Products.GenericSetup.interfaces import ISetupEnviron |
---|
| 31 | from Products.WAeUP.interfaces import IWAeUPTool |
---|
| 32 | |
---|
| 33 | TOOL = 'portal_waeup' |
---|
| 34 | NAME = 'waeup' |
---|
| 35 | |
---|
| 36 | # The exportWAeUP and importWAeUP methods are called by the specific |
---|
| 37 | # im- / export steps defined in the corresponding XML files in the |
---|
| 38 | # WAeUP/profiles/default/ directory. |
---|
| 39 | |
---|
[200] | 40 | |
---|
| 41 | def setupStructure(context): ###( |
---|
| 42 | sections = getattr(context,'sections') |
---|
| 43 | portal = getattr(sections,'uniportal',None) |
---|
| 44 | if portal is None: |
---|
| 45 | sections.invokeFactory('University','uniportal') |
---|
| 46 | portal = getattr(context,'uniportal',None) |
---|
| 47 | portal.getContent().edit(mapping={'Title':SRPP_TITLE}) |
---|
| 48 | students = getattr(portal,'students',None) |
---|
| 49 | if students is None: |
---|
| 50 | portal.invokeFactory('StudentsFolder','students') |
---|
| 51 | students = getattr(portal,'students').getContent() |
---|
| 52 | students.edit(mapping={'Title':'Students'}) |
---|
| 53 | academics = getattr(portal,'academics',None) |
---|
| 54 | if academics is None: |
---|
| 55 | portal.invokeFactory('AcademicsFolder','academics') |
---|
| 56 | academics = getattr(portal,'academics') |
---|
| 57 | academics.getContent().edit(mapping={'Title':'Academics'}) |
---|
| 58 | installFaculties(academics) |
---|
| 59 | if not hasattr(portal,'accommodation'): |
---|
| 60 | portal.invokeFactory('AccoFolder','accommodation') |
---|
| 61 | accommodation = getattr(portal,'accommodation').getContent() |
---|
| 62 | accommodation.edit(mapping={'Title':'Accommodation'}) |
---|
| 63 | ###) |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | def installFaculties( academics): ###( |
---|
| 67 | """install Universityspecific Faculies with Departments""" |
---|
[210] | 68 | faculties = [ ###( |
---|
[206] | 69 | {'id': 'agriculture', ###( |
---|
| 70 | 'Title': 'Agriculture', |
---|
| 71 | 'departments': [ |
---|
[200] | 72 | ## { 'id': 'dep1', ###( |
---|
| 73 | ## 'Title': 'One', |
---|
| 74 | ## }, |
---|
[206] | 75 | ], |
---|
| 76 | },###) |
---|
[210] | 77 | ###) |
---|
[206] | 78 | {'id': 'arts', ###( |
---|
| 79 | 'Title': 'Arts', |
---|
| 80 | 'departments': [ |
---|
| 81 | ## { 'id': 'dep1', ###( |
---|
| 82 | ## 'Title': 'One', |
---|
| 83 | ## }, |
---|
| 84 | ], |
---|
| 85 | },###) |
---|
[210] | 86 | ###) |
---|
| 87 | {'id': 'BasicMedicalSciences', ###( |
---|
| 88 | 'Title': 'Basic Medical Sciences', |
---|
| 89 | 'departments': [ |
---|
| 90 | ## { 'id': 'dep1', ###( |
---|
| 91 | ## 'Title': 'One', |
---|
| 92 | ## }, |
---|
| 93 | ], |
---|
| 94 | },###) |
---|
| 95 | ###) |
---|
| 96 | {'id': 'science', ###( |
---|
[200] | 97 | 'Title': 'Science', |
---|
| 98 | 'departments': [ |
---|
| 99 | { 'id': 'bio', ###( |
---|
| 100 | 'Title': 'Biochemistry', |
---|
| 101 | }, |
---|
| 102 | { 'id': 'bot', |
---|
| 103 | 'Title': 'Botany', |
---|
| 104 | }, |
---|
| 105 | { 'id': 'che', |
---|
| 106 | 'Title': 'Chemistry', |
---|
| 107 | }, |
---|
| 108 | { 'id': 'com', |
---|
| 109 | 'Title': 'Computer Science', |
---|
| 110 | }, |
---|
| 111 | { 'id': 'geo', |
---|
| 112 | 'Title': 'Geologie', |
---|
| 113 | }, |
---|
| 114 | { 'id': 'mat', |
---|
| 115 | 'Title': 'Mathematics', |
---|
| 116 | }, |
---|
| 117 | { 'id': 'mic', |
---|
| 118 | 'Title': 'Microbiology', |
---|
| 119 | }, |
---|
| 120 | { 'id': 'opt', |
---|
| 121 | 'Title': 'Optometry', |
---|
| 122 | }, |
---|
| 123 | { 'id': 'phy', |
---|
| 124 | 'Title': 'Physics', |
---|
| 125 | }, |
---|
| 126 | { 'id': 'zoo', |
---|
| 127 | 'Title': 'Zoology', |
---|
| 128 | }, |
---|
| 129 | ], |
---|
| 130 | },###) |
---|
| 131 | ]###) |
---|
[210] | 132 | |
---|
| 133 | ###) |
---|
| 134 | |
---|
[200] | 135 | for faculty in faculties: |
---|
| 136 | fid = faculty['id'] |
---|
| 137 | f = getattr(academics,fid,None) |
---|
| 138 | if f is None: |
---|
| 139 | #self.log('Creating Faculty %(id)s = %(Title)s' % faculty) |
---|
| 140 | academics.invokeFactory('Faculty', fid) |
---|
| 141 | f = getattr(academics,fid) |
---|
| 142 | f.getContent().edit(mapping=faculty) |
---|
| 143 | for department in faculty['departments']: |
---|
| 144 | #self.log('Checking Department %(id)s = %(Title)s' % department) |
---|
| 145 | did = department['id'] |
---|
| 146 | d = getattr(f,did,None) |
---|
| 147 | if d is None: |
---|
| 148 | #self.log('Creating Department %(id)s = %(Title)s' % department) |
---|
| 149 | f.invokeFactory('Department', did) |
---|
| 150 | d = getattr(f,did) |
---|
| 151 | d.getContent().edit(mapping=department) |
---|
| 152 | ###) |
---|
| 153 | |
---|
| 154 | |
---|
[199] | 155 | def exportWAeUP(context): |
---|
| 156 | """Export our WAeUP tool configuration |
---|
| 157 | """ |
---|
| 158 | site = context.getSite() |
---|
| 159 | tool = getToolByName(site, TOOL, None) |
---|
| 160 | if tool is None: |
---|
| 161 | logger = context.getLogger(NAME) |
---|
| 162 | logger.info("Nothing to export.") |
---|
| 163 | return |
---|
| 164 | exportObjects(tool, '', context) |
---|
| 165 | |
---|
| 166 | def importWAeUP(context): |
---|
| 167 | """Import WAeUP tool configuration |
---|
| 168 | """ |
---|
| 169 | site = context.getSite() |
---|
[211] | 170 | #import pdb; pdb.set_trace() |
---|
[200] | 171 | setupStructure(site) |
---|
| 172 | pass |
---|
| 173 | #site = context.getSite() |
---|
| 174 | #tool = getToolByName(site, TOOL) |
---|
| 175 | #importObjects(tool, '', context) |
---|
[199] | 176 | |
---|
| 177 | |
---|
| 178 | # This the XMLAdapter itself. It encodes the im- / export logic that is specific |
---|
| 179 | # to our tool. `im- / exportObjects` functions will find it thanks to the zope |
---|
| 180 | # components machinery and the associations made in the configure.zcml file. |
---|
| 181 | |
---|
| 182 | class WAeUPXMLAdapter(XMLAdapterBase, PropertyManagerHelpers): |
---|
| 183 | """XML importer and exporter for the WAeUP tool. |
---|
| 184 | |
---|
| 185 | Hence this XMLAdapter is really simple. To get more complete examples of |
---|
| 186 | what XMLAdapters are meant to do, please have a look at the |
---|
| 187 | CPSSkins.exportimport.py or CPSDirectory.exportimport.py files, for |
---|
| 188 | instance. |
---|
| 189 | """ |
---|
| 190 | |
---|
| 191 | adapts(IWAeUPTool, ISetupEnviron) |
---|
| 192 | implements(IBody) |
---|
| 193 | |
---|
| 194 | _LOGGER_ID = NAME |
---|
| 195 | name = NAME |
---|
| 196 | |
---|
| 197 | def _exportNode(self): |
---|
| 198 | """Export the object as a DOM node. |
---|
| 199 | """ |
---|
| 200 | node = self._getObjectNode('object') |
---|
| 201 | node.appendChild(self._extractProperties()) |
---|
| 202 | self._logger.info("WAeUP tool exported.") |
---|
| 203 | return node |
---|
| 204 | |
---|
| 205 | def _importNode(self, node): |
---|
| 206 | """Import the object from the DOM node. |
---|
| 207 | """ |
---|
| 208 | if self.environ.shouldPurge(): |
---|
| 209 | self._purgeProperties() |
---|
| 210 | self._initProperties(node) |
---|
| 211 | self._logger.info("WAeUP tool imported.") |
---|
| 212 | |
---|