| [200] | 1 | #-*- mode: python; mode: fold -*- |
|---|
| [486] | 2 | # $Id: exportimport.py 5614 2010-12-27 07:25:11Z henrik $ |
|---|
| [199] | 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 | """ |
|---|
| [238] | 14 | import csv,re |
|---|
| [199] | 15 | |
|---|
| 16 | # Zope3 component architecture |
|---|
| [502] | 17 | from zope.app import zapi |
|---|
| [199] | 18 | from zope.component import adapts |
|---|
| 19 | from zope.interface import implements |
|---|
| 20 | |
|---|
| 21 | # Standard GenericSetup base classes and functions |
|---|
| 22 | from Products.GenericSetup.utils import exportObjects |
|---|
| 23 | from Products.GenericSetup.utils import importObjects |
|---|
| 24 | from Products.GenericSetup.utils import XMLAdapterBase |
|---|
| 25 | from Products.GenericSetup.utils import PropertyManagerHelpers |
|---|
| 26 | |
|---|
| 27 | from Products.CMFCore.utils import getToolByName |
|---|
| 28 | |
|---|
| 29 | # GenericSetup multi adapts a class that implement IWAeUP and a particular |
|---|
| 30 | # ISetupEnvironment to and IBody (piece of XML configuration). |
|---|
| 31 | from Products.GenericSetup.interfaces import IBody |
|---|
| 32 | from Products.GenericSetup.interfaces import ISetupEnviron |
|---|
| [274] | 33 | from Products.WAeUP_SRP.interfaces import IWAeUPTool |
|---|
| [238] | 34 | import Globals |
|---|
| 35 | p_home = Globals.package_home(globals()) |
|---|
| 36 | i_home = Globals.INSTANCE_HOME |
|---|
| [199] | 37 | |
|---|
| 38 | TOOL = 'portal_waeup' |
|---|
| 39 | NAME = 'waeup' |
|---|
| 40 | |
|---|
| [502] | 41 | def importWAeUPTable(obj, parent_path, context, name): |
|---|
| 42 | """ Import subobjects recursively. |
|---|
| 43 | """ |
|---|
| 44 | importer = zapi.queryMultiAdapter((obj, context), IBody) |
|---|
| [950] | 45 | |
|---|
| [502] | 46 | path = '%s%s' % (parent_path, obj.getId().replace(' ', '_')) |
|---|
| 47 | __traceback_info__ = path |
|---|
| 48 | if importer: |
|---|
| 49 | path = '%s%s' % (parent_path, name) |
|---|
| 50 | filename = '%s%s' % (path, importer.suffix) |
|---|
| 51 | body = context.readDataFile(filename) |
|---|
| 52 | if body is not None: |
|---|
| 53 | importer.filename = filename # for error reporting |
|---|
| 54 | importer.body = body |
|---|
| 55 | |
|---|
| 56 | if getattr(obj, 'objectValues', False): |
|---|
| 57 | for sub in obj.objectValues(): |
|---|
| 58 | importObjects(sub, path+'/', context) |
|---|
| 59 | |
|---|
| [199] | 60 | def exportWAeUP(context): |
|---|
| 61 | """Export our WAeUP tool configuration |
|---|
| 62 | """ |
|---|
| 63 | site = context.getSite() |
|---|
| [282] | 64 | tool = getattr(context,"campus",None) |
|---|
| [199] | 65 | if tool is None: |
|---|
| 66 | logger = context.getLogger(NAME) |
|---|
| 67 | logger.info("Nothing to export.") |
|---|
| 68 | return |
|---|
| 69 | exportObjects(tool, '', context) |
|---|
| 70 | |
|---|
| 71 | def importWAeUP(context): |
|---|
| 72 | """Import WAeUP tool configuration |
|---|
| 73 | """ |
|---|
| 74 | site = context.getSite() |
|---|
| [350] | 75 | pm = site.portal_membership |
|---|
| [940] | 76 | campus = getattr(site.portal_url.getPortalObject(),'campus',None) |
|---|
| 77 | if campus is not None: |
|---|
| [949] | 78 | academics = getattr(campus,'academics',None) |
|---|
| 79 | students = getattr(campus,'students',None) |
|---|
| [963] | 80 | accommodation = getattr(campus,'accommodation',None) |
|---|
| [3299] | 81 | uploads = getattr(campus,'uploads',None) |
|---|
| [5591] | 82 | pins = getattr(campus,'pins',None) |
|---|
| [1414] | 83 | campus.manage_setLocalGroupRoles(groupid='CampusOfficers', |
|---|
| [1432] | 84 | roles=('SectionOfficer',)) |
|---|
| [949] | 85 | if academics is not None: |
|---|
| [955] | 86 | academics.manage_setLocalGroupRoles(groupid='role:Authenticated', |
|---|
| [926] | 87 | roles=('SectionReader',)) |
|---|
| [1414] | 88 | academics.manage_setLocalGroupRoles(groupid='AcademicsOfficers', |
|---|
| [1432] | 89 | roles=('SectionOfficer',)) |
|---|
| [949] | 90 | if students is not None: |
|---|
| [2304] | 91 | students.manage_setLocalGroupRoles(groupid = 'StudentsReaders', |
|---|
| 92 | roles=('SectionReader',)) |
|---|
| [962] | 93 | students.manage_setLocalGroupRoles(groupid = 'ClearanceOfficers', |
|---|
| [955] | 94 | roles=('SectionReader',)) |
|---|
| [1515] | 95 | students.manage_setLocalGroupRoles(groupid = 'CourseAdvisers', |
|---|
| [1625] | 96 | roles=('SectionReader',)) |
|---|
| [1221] | 97 | students.manage_setLocalGroupRoles(groupid='MemberAdmins', |
|---|
| [1414] | 98 | roles=('SectionOfficer',)) |
|---|
| 99 | students.manage_setLocalGroupRoles(groupid='StudentsOfficers', |
|---|
| [1432] | 100 | roles=('SectionOfficer',)) |
|---|
| [962] | 101 | students.manage_setLocalGroupRoles(groupid = 'role:Student', |
|---|
| 102 | roles=('SectionReader',)) |
|---|
| [963] | 103 | if accommodation is not None: |
|---|
| 104 | accommodation.manage_setLocalGroupRoles(groupid='role:Student', |
|---|
| [1221] | 105 | roles=('SectionReader',)) |
|---|
| [1414] | 106 | accommodation.manage_setLocalGroupRoles(groupid='AccommodationOfficers', |
|---|
| [1432] | 107 | roles=('SectionOfficer',)) |
|---|
| [3299] | 108 | if uploads is not None: |
|---|
| 109 | uploads.manage_setLocalGroupRoles(groupid='UploadOfficers', |
|---|
| [3277] | 110 | roles=('SectionOfficer',)) |
|---|
| [5591] | 111 | if pins is not None: |
|---|
| 112 | uploads.manage_setLocalGroupRoles(groupid='AccessCodeOfficers', |
|---|
| 113 | roles=('SectionOfficer',)) |
|---|
| [955] | 114 | members = getattr(site.portal_directories,'members') |
|---|
| 115 | if members is not None: |
|---|
| [957] | 116 | members.manage_setLocalGroupRoles(groupid='MemberAdmins', |
|---|
| [955] | 117 | roles=('SectionOfficer',)) |
|---|
| [1432] | 118 | students = getattr(site.portal_directories,'students') |
|---|
| 119 | if students is not None: |
|---|
| 120 | students.manage_setLocalGroupRoles(groupid='MemberAdmins', |
|---|
| [1433] | 121 | roles=('SectionOfficer',)) |
|---|
| 122 | waeup_stacking_dir = getattr(site.portal_directories,'waeup_stacking_dir') |
|---|
| 123 | if waeup_stacking_dir is not None: |
|---|
| 124 | waeup_stacking_dir.manage_setLocalGroupRoles(groupid='MemberAdmins', |
|---|
| [1515] | 125 | roles=('SectionOfficer',)) |
|---|
| [956] | 126 | |
|---|
| [926] | 127 | ## pm.setLocalGroupRoles(campus.academics,['role:Authenticated', |
|---|
| 128 | ## ],'SectionReader') |
|---|
| [502] | 129 | importWAeUPTable(site.portal_accommodation, '', context,'accommodation') |
|---|
| [1146] | 130 | importWAeUPTable(site.courses_catalog, '', context,'courses_catalog') |
|---|
| [3494] | 131 | importWAeUPTable(site.certificates_catalog, '', context,'certificates_catalog') |
|---|
| [1146] | 132 | importWAeUPTable(site.payments_catalog, '', context,'payments_catalog') |
|---|
| [1625] | 133 | importWAeUPTable(site.online_payments_import, '', context,'online_payments_import') |
|---|
| [502] | 134 | importWAeUPTable(site.portal_pins, '', context,'pins') |
|---|
| [966] | 135 | importWAeUPTable(site.portal_pumeresults, '', context,'pumeresults') |
|---|
| [1151] | 136 | importWAeUPTable(site.returning_import, '', context,'returning_import') |
|---|
| [1146] | 137 | importWAeUPTable(site.results_import, '', context,'results_import') |
|---|
| [2086] | 138 | importWAeUPTable(site.students_catalog, '', context,'students_catalog') |
|---|
| [2094] | 139 | importWAeUPTable(site.applicants_catalog, '', context,'applicants_catalog') |
|---|
| [2086] | 140 | importWAeUPTable(site.course_results, '', context,'course_results') |
|---|
| [2257] | 141 | importWAeUPTable(site.portal_catalog, '', context,'portal_catalog') |
|---|
| [1935] | 142 | importWAeUPTable(site.portal_catalog_real, '', context,'portal_catalog_real') |
|---|
| [4302] | 143 | importWAeUPTable(site.removed_student_ids, '', context,'removed_student_ids') |
|---|
| [5614] | 144 | importWAeUPTable(site.accommodation_catalog, '', context,'accommodation_catalog') |
|---|
| [3299] | 145 | |
|---|
| [2651] | 146 | site.students_catalog.manage_setLocalGroupRoles(groupid='CampusOfficers', |
|---|
| 147 | roles=('SectionOfficer',)) |
|---|
| [3006] | 148 | site.payments_catalog.manage_setLocalGroupRoles(groupid='CampusOfficers', |
|---|
| 149 | roles=('SectionOfficer',)) |
|---|
| 150 | site.applicants_catalog.manage_setLocalGroupRoles(groupid='CampusOfficers', |
|---|
| [3299] | 151 | roles=('SectionOfficer',)) |
|---|
| [3006] | 152 | site.course_results.manage_setLocalGroupRoles(groupid='CampusOfficers', |
|---|
| [3299] | 153 | roles=('SectionOfficer',)) |
|---|
| [2304] | 154 | |
|---|
| [2258] | 155 | site.portal_catalog.id = 'portal_catalog' |
|---|
| 156 | site.portal_catalog_real.id = 'portal_catalog_real' |
|---|
| [950] | 157 | |
|---|
| [2304] | 158 | |
|---|
| [502] | 159 | from Products.CPSCore.exportimport.catalog import CatalogToolXMLAdapter |
|---|
| 160 | #from Products.GenericSetup.ZCatalog.exportimport import ZCatalogXMLAdapter |
|---|
| [363] | 161 | |
|---|
| 162 | from interfaces import IWAeUPTable |
|---|
| 163 | |
|---|
| [502] | 164 | class WAeUPTableXMLAdapter(CatalogToolXMLAdapter): |
|---|
| 165 | #class WAeUPTableXMLAdapter(ZCatalogXMLAdapter): |
|---|
| [363] | 166 | __used_for__ = IWAeUPTable |
|---|
| 167 | _LOGGER_ID = 'waeup_table' |
|---|
| 168 | |
|---|
| [502] | 169 | ## def _importNode(self, node): |
|---|
| 170 | ## """Import the object from the DOM node. |
|---|
| 171 | ## """ |
|---|
| 172 | ## if self.environ.shouldPurge(): |
|---|
| 173 | ## self._purgeProperties() |
|---|
| 174 | ## self._purgeObjects() |
|---|
| 175 | ## self._purgeIndexes() |
|---|
| 176 | ## self._purgeColumns() |
|---|
| 177 | ## |
|---|
| 178 | ## self._logger.info('Catalog imported.') |
|---|
| [199] | 179 | # This the XMLAdapter itself. It encodes the im- / export logic that is specific |
|---|
| 180 | # to our tool. `im- / exportObjects` functions will find it thanks to the zope |
|---|
| 181 | # components machinery and the associations made in the configure.zcml file. |
|---|
| 182 | |
|---|
| [282] | 183 | ##class WAeUPXMLAdapter(XMLAdapterBase, PropertyManagerHelpers): |
|---|
| 184 | ## """XML importer and exporter for the WAeUP tool. |
|---|
| 185 | ## |
|---|
| 186 | ## Hence this XMLAdapter is really simple. To get more complete examples of |
|---|
| 187 | ## what XMLAdapters are meant to do, please have a look at the |
|---|
| 188 | ## CPSSkins.exportimport.py or CPSDirectory.exportimport.py files, for |
|---|
| 189 | ## instance. |
|---|
| 190 | ## """ |
|---|
| 191 | ## |
|---|
| 192 | ## adapts(IWAeUPTool, ISetupEnviron) |
|---|
| 193 | ## implements(IBody) |
|---|
| 194 | ## |
|---|
| 195 | ## _LOGGER_ID = NAME |
|---|
| 196 | ## name = NAME |
|---|
| 197 | ## |
|---|
| 198 | ## def _exportNode(self): |
|---|
| 199 | ## """Export the object as a DOM node. |
|---|
| 200 | ## """ |
|---|
| 201 | ## node = self._getObjectNode('object') |
|---|
| 202 | ## node.appendChild(self._extractProperties()) |
|---|
| 203 | ## self._logger.info("WAeUP tool exported.") |
|---|
| 204 | ## return node |
|---|
| 205 | ## |
|---|
| 206 | ## def _importNode(self, node): |
|---|
| 207 | ## """Import the object from the DOM node. |
|---|
| 208 | ## """ |
|---|
| 209 | ## if self.environ.shouldPurge(): |
|---|
| 210 | ## self._purgeProperties() |
|---|
| 211 | ## self._initProperties(node) |
|---|
| 212 | ## self._logger.info("WAeUP tool imported.") |
|---|
| 213 | ## |
|---|