source: waeup_product/trunk/exportimport.py @ 208

Last change on this file since 208 was 206, checked in by joachim, 18 years ago

=new

File size: 7.4 KB
Line 
1#-*- mode: python; mode: fold -*-
2# $Id: exportimport.py 31640 2006-01-15 19:22:29Z ogrisel $
3"""WAeUP Tool XML Adapter.
4
5An XML adapter tells the GenericSetup machinery howto im- / export persistent
6configuration that is relative to a specific CMF component such as our WAeUP
7Tool.
8
9GenericSetup uses the Zope3 interfaces and components machinery to find the
10right XML adapter for the right object. This is why we flagged our waeup tool
11with the `implements(IWAeUPTool)` class attribute and made an adapter
12association in the `configure.zcml` file.
13"""
14
15# Zope3 component architecture
16from zope.component import adapts
17from zope.interface import implements
18
19# Standard GenericSetup base classes and functions
20from Products.GenericSetup.utils import exportObjects
21from Products.GenericSetup.utils import importObjects
22from Products.GenericSetup.utils import XMLAdapterBase
23from Products.GenericSetup.utils import PropertyManagerHelpers
24
25from 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).
29from Products.GenericSetup.interfaces import IBody
30from Products.GenericSetup.interfaces import ISetupEnviron
31from Products.WAeUP.interfaces import IWAeUPTool
32
33TOOL = 'portal_waeup'
34NAME = '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
40
41def 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
66def installFaculties( academics): ###(
67    """install Universityspecific Faculies with Departments"""
68    faculties = [
69                  {'id': 'agriculture', ###(
70                      'Title': 'Agriculture',
71                      'departments': [
72##                                     { 'id': 'dep1', ###(
73##                                       'Title': 'One',
74##                                     },
75                                     ],
76                      },###)
77                  {'id': 'arts', ###(
78                      'Title': 'Arts',
79                      'departments': [
80##                                     { 'id': 'dep1', ###(
81##                                       'Title': 'One',
82##                                     },
83                                     ],
84                      },###)
85                  { 'id': 'science',
86                  'Title': 'Science',
87                  'departments': [
88                                 { 'id': 'bio', ###(
89                                   'Title': 'Biochemistry',
90                                 },
91                                 { 'id': 'bot',
92                                   'Title': 'Botany',
93                                 },
94                                 { 'id': 'che',
95                                   'Title': 'Chemistry',
96                                 },
97                                 { 'id': 'com',
98                                   'Title': 'Computer Science',
99                                 },
100                                 { 'id': 'geo',
101                                   'Title': 'Geologie',
102                                 },
103                                 { 'id': 'mat',
104                                   'Title': 'Mathematics',
105                                 },
106                                 { 'id': 'mic',
107                                   'Title': 'Microbiology',
108                                 },
109                                 { 'id': 'opt',
110                                   'Title': 'Optometry',
111                                 },
112                                 { 'id': 'phy',
113                                   'Title': 'Physics',
114                                 },
115                                 { 'id': 'zoo',
116                                   'Title': 'Zoology',
117                                 },
118                                 ],
119                  },###)
120                 ]###)
121    for faculty in faculties:
122        fid = faculty['id']
123        f = getattr(academics,fid,None)
124        if f is None:
125            #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
126            academics.invokeFactory('Faculty', fid)
127            f = getattr(academics,fid)
128            f.getContent().edit(mapping=faculty)
129        for department in faculty['departments']:
130            #self.log('Checking Department %(id)s = %(Title)s' % department)
131            did = department['id']
132            d = getattr(f,did,None)
133            if d is None:
134                #self.log('Creating Department %(id)s = %(Title)s' % department)
135                f.invokeFactory('Department', did)
136                d = getattr(f,did)
137                d.getContent().edit(mapping=department)
138###)
139
140
141def exportWAeUP(context):
142    """Export our WAeUP tool configuration
143    """
144    site = context.getSite()
145    tool = getToolByName(site, TOOL, None)
146    if tool is None:
147        logger = context.getLogger(NAME)
148        logger.info("Nothing to export.")
149        return
150    exportObjects(tool, '', context)
151
152def importWAeUP(context):
153    """Import WAeUP tool configuration
154    """
155    site = context.getSite()
156    setupStructure(site)
157    pass
158    #site = context.getSite()
159    #tool = getToolByName(site, TOOL)
160    #importObjects(tool, '', context)
161
162
163# This the XMLAdapter itself. It encodes the im- / export logic that is specific
164# to our tool. `im- / exportObjects` functions will find it thanks to the zope
165# components machinery and the associations made in the configure.zcml file.
166
167class WAeUPXMLAdapter(XMLAdapterBase, PropertyManagerHelpers):
168    """XML importer and exporter for the WAeUP tool.
169
170    Hence this XMLAdapter is really simple. To get more complete examples of
171    what XMLAdapters are meant to do, please have a look at the
172    CPSSkins.exportimport.py or CPSDirectory.exportimport.py files, for
173    instance.
174    """
175
176    adapts(IWAeUPTool, ISetupEnviron)
177    implements(IBody)
178
179    _LOGGER_ID = NAME
180    name = NAME
181
182    def _exportNode(self):
183        """Export the object as a DOM node.
184        """
185        node = self._getObjectNode('object')
186        node.appendChild(self._extractProperties())
187        self._logger.info("WAeUP tool exported.")
188        return node
189
190    def _importNode(self, node):
191        """Import the object from the DOM node.
192        """
193        if self.environ.shouldPurge():
194            self._purgeProperties()
195        self._initProperties(node)
196        self._logger.info("WAeUP tool imported.")
197
Note: See TracBrowser for help on using the repository browser.