[200] | 1 | #-*- mode: python; mode: fold -*- |
---|
[486] | 2 | # $Id: exportimport.py 926 2006-11-23 11:54:49Z joachim $ |
---|
[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) |
---|
| 45 | |
---|
| 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 |
---|
[902] | 76 | campus = site.portal_url.getPortalObject().campus |
---|
[926] | 77 | campus.academics.manage_setLocalGroupRoles(groupid='Authenticated', |
---|
| 78 | roles=('SectionReader',)) |
---|
[905] | 79 | pm.setLocalGroupRoles(campus.students,['group:ClearanceOfficers', |
---|
| 80 | 'role:Student',],'SectionReader') |
---|
[926] | 81 | ## pm.setLocalGroupRoles(campus.academics,['role:Authenticated', |
---|
| 82 | ## ],'SectionReader') |
---|
[502] | 83 | importWAeUPTable(site.portal_accommodation, '', context,'accommodation') |
---|
| 84 | importWAeUPTable(site.portal_pins, '', context,'pins') |
---|
[363] | 85 | |
---|
[502] | 86 | from Products.CPSCore.exportimport.catalog import CatalogToolXMLAdapter |
---|
| 87 | #from Products.GenericSetup.ZCatalog.exportimport import ZCatalogXMLAdapter |
---|
[363] | 88 | |
---|
| 89 | from interfaces import IWAeUPTable |
---|
| 90 | |
---|
[502] | 91 | class WAeUPTableXMLAdapter(CatalogToolXMLAdapter): |
---|
| 92 | #class WAeUPTableXMLAdapter(ZCatalogXMLAdapter): |
---|
[363] | 93 | __used_for__ = IWAeUPTable |
---|
| 94 | _LOGGER_ID = 'waeup_table' |
---|
| 95 | |
---|
[502] | 96 | ## def _importNode(self, node): |
---|
| 97 | ## """Import the object from the DOM node. |
---|
| 98 | ## """ |
---|
| 99 | ## if self.environ.shouldPurge(): |
---|
| 100 | ## self._purgeProperties() |
---|
| 101 | ## self._purgeObjects() |
---|
| 102 | ## self._purgeIndexes() |
---|
| 103 | ## self._purgeColumns() |
---|
| 104 | ## |
---|
| 105 | ## self._logger.info('Catalog imported.') |
---|
[199] | 106 | # This the XMLAdapter itself. It encodes the im- / export logic that is specific |
---|
| 107 | # to our tool. `im- / exportObjects` functions will find it thanks to the zope |
---|
| 108 | # components machinery and the associations made in the configure.zcml file. |
---|
| 109 | |
---|
[282] | 110 | ##class WAeUPXMLAdapter(XMLAdapterBase, PropertyManagerHelpers): |
---|
| 111 | ## """XML importer and exporter for the WAeUP tool. |
---|
| 112 | ## |
---|
| 113 | ## Hence this XMLAdapter is really simple. To get more complete examples of |
---|
| 114 | ## what XMLAdapters are meant to do, please have a look at the |
---|
| 115 | ## CPSSkins.exportimport.py or CPSDirectory.exportimport.py files, for |
---|
| 116 | ## instance. |
---|
| 117 | ## """ |
---|
| 118 | ## |
---|
| 119 | ## adapts(IWAeUPTool, ISetupEnviron) |
---|
| 120 | ## implements(IBody) |
---|
| 121 | ## |
---|
| 122 | ## _LOGGER_ID = NAME |
---|
| 123 | ## name = NAME |
---|
| 124 | ## |
---|
| 125 | ## def _exportNode(self): |
---|
| 126 | ## """Export the object as a DOM node. |
---|
| 127 | ## """ |
---|
| 128 | ## node = self._getObjectNode('object') |
---|
| 129 | ## node.appendChild(self._extractProperties()) |
---|
| 130 | ## self._logger.info("WAeUP tool exported.") |
---|
| 131 | ## return node |
---|
| 132 | ## |
---|
| 133 | ## def _importNode(self, node): |
---|
| 134 | ## """Import the object from the DOM node. |
---|
| 135 | ## """ |
---|
| 136 | ## if self.environ.shouldPurge(): |
---|
| 137 | ## self._purgeProperties() |
---|
| 138 | ## self._initProperties(node) |
---|
| 139 | ## self._logger.info("WAeUP tool imported.") |
---|
| 140 | ## |
---|