[275] | 1 | from xml.dom import minidom |
---|
| 2 | |
---|
| 3 | from zope.interface import implements |
---|
| 4 | from zope.component import adapts |
---|
| 5 | |
---|
| 6 | from Products.Archetypes import atapi |
---|
| 7 | from Products.Archetypes.interfaces import IBaseObject |
---|
| 8 | from Products.ATContentTypes.interface import IATTopicCriterion |
---|
| 9 | from Products.Marshall.registry import getComponent |
---|
| 10 | from Products.Archetypes import config as atcfg |
---|
| 11 | |
---|
[1446] | 12 | from collective.transmogrifier.interfaces import ITransmogrifier |
---|
| 13 | |
---|
[275] | 14 | from quintagroup.transmogrifier.interfaces import IExportDataCorrector |
---|
| 15 | |
---|
| 16 | class ReferenceExporter(object): |
---|
| 17 | """ Add reference fields to XML, generated by Marshall product. |
---|
| 18 | """ |
---|
| 19 | implements(IExportDataCorrector) |
---|
[1446] | 20 | adapts(IBaseObject, ITransmogrifier) |
---|
[275] | 21 | |
---|
[1446] | 22 | def __init__(self, context, transmogrifier): |
---|
[275] | 23 | self.context = context |
---|
[1446] | 24 | self.transmogrifier = transmogrifier |
---|
[275] | 25 | |
---|
| 26 | def __call__(self, data): |
---|
| 27 | data['data'] = self.exportReferences(data['data']) |
---|
| 28 | return data |
---|
| 29 | |
---|
| 30 | def exportReferences(self, xml): |
---|
| 31 | """ Marshall 1.0.0 doesn't export references, do it manually. |
---|
| 32 | """ |
---|
| 33 | doc = minidom.parseString(xml) |
---|
| 34 | root = doc.documentElement |
---|
| 35 | for fname in self.context.Schema().keys(): |
---|
| 36 | if not isinstance(self.context.Schema()[fname], atapi.ReferenceField): |
---|
| 37 | continue |
---|
[460] | 38 | values = self.context[fname] |
---|
| 39 | if not values: |
---|
| 40 | continue |
---|
[275] | 41 | elem = doc.createElement("field") |
---|
| 42 | attr = doc.createAttribute("name") |
---|
| 43 | attr.value = fname |
---|
| 44 | elem.setAttributeNode(attr) |
---|
[460] | 45 | if type(values) not in (tuple, list): |
---|
| 46 | values = [values,] |
---|
| 47 | for value in values: |
---|
[275] | 48 | ref = doc.createElement('reference') |
---|
| 49 | uid = doc.createElement('uid') |
---|
| 50 | value = doc.createTextNode(str(value)) |
---|
| 51 | uid.appendChild(value) |
---|
| 52 | ref.appendChild(uid) |
---|
| 53 | elem.appendChild(ref) |
---|
| 54 | root.appendChild(elem) |
---|
| 55 | return doc.toxml('utf-8') |
---|
| 56 | |
---|
| 57 | class CriterionExporter(ReferenceExporter): |
---|
| 58 | """ Special marshalling adapter for topic criterias. |
---|
| 59 | """ |
---|
| 60 | |
---|
| 61 | implements(IExportDataCorrector) |
---|
[1446] | 62 | adapts(IATTopicCriterion, ITransmogrifier) |
---|
[275] | 63 | |
---|
[1446] | 64 | def __init__(self, context, transmogrifier): |
---|
[275] | 65 | self.context = context |
---|
[1446] | 66 | self.transmogrifier = transmogrifier |
---|
[275] | 67 | self.marshaller = getComponent('atxml') |
---|
| 68 | |
---|
| 69 | def __call__(self, data): |
---|
| 70 | if data['data'] is not None: |
---|
| 71 | return super(CriterionExporter, self).__call__(data) |
---|
| 72 | # Marshall fails when object has UID that is equal to None |
---|
| 73 | # fix it here by setting it to empty string and after marshalling setting back to old value |
---|
| 74 | old_uid_attr = getattr(self.context, atcfg.UUID_ATTR) |
---|
| 75 | setattr(self.context, atcfg.UUID_ATTR, "") |
---|
| 76 | |
---|
| 77 | ct, length, xml = self.marshaller.marshall(self.context) |
---|
| 78 | xml = self.exportReferences(xml) |
---|
| 79 | data['data'] = xml |
---|
| 80 | |
---|
| 81 | setattr(self.context, atcfg.UUID_ATTR, old_uid_attr) |
---|
| 82 | return data |
---|