source: products/quintagroup.transmogrify.pfm2pfg/trunk/quintagroup/transmogrify/pfm2pfg/exporting.py @ 1498

Last change on this file since 1498 was 350, checked in by piv, 14 years ago

added tags folder

File size: 3.1 KB
Line 
1from xml.dom import minidom
2
3from zope.interface import Interface, implements
4
5from Products.Archetypes import atapi
6from Products.Marshall.config import AT_NS
7from Products.PageTemplates.TALES import CompilerError
8
9from quintagroup.transmogrifier.interfaces import IExportDataCorrector
10from quintagroup.transmogrifier.adapters.exporting import ReferenceExporter
11
12class IPloneFormMailer(Interface):
13    """ Marker interface for PloneFormMailer content type
14    """
15
16class PloneFormMailerExporter(ReferenceExporter):
17    """ Marshalls PloneFormMailer to XML.
18    """
19    implements(IExportDataCorrector)
20
21    def __init__(self, context):
22        self.context = context
23        self.tales_fnames = ['recipient_name', 'bcc_recipients', 'cc_recipients']
24        self.mail_subject_field = 'subject'
25
26    def __call__(self, data):
27        xml = data['data']
28        xml = self.exportReferences(xml)
29        data['data'] = self.exportFormFields(xml)
30        return data
31
32    def exportFormFields(self, data):
33        """ Reformat xml file and add Formulator fields.
34        """
35        # reformat xml tree
36        doc = minidom.parseString(data)
37        root = doc.documentElement
38
39        # some TALES fields must be exported as value of executed expression
40        schema = self.context.Schema()
41        for fname in self.tales_fnames:
42            nodes = [i for i in root.getElementsByTagName('field') if i.getAttribute('name') == fname]
43            for node in nodes:
44                root.removeChild(node)
45            # trying to get value of TALES field
46            # if expression can't be evaluated skip this field
47            try:
48                values = schema[fname].getAccessor(self.context)()
49            except CompilerError:
50                continue
51            if not isinstance(values, (list, tuple)):
52                values = [values]
53            values = filter(None, values)
54            for value in values:
55                node = doc.createElementNS(AT_NS, "field")
56                name_attr = doc.createAttribute("name")
57                name_attr.value = fname
58                node.setAttributeNode(name_attr)
59                value_node = doc.createTextNode(str(value))
60                node.appendChild(value_node)
61                node.normalize()
62                root.appendChild(node)
63
64        # PloneFormMailer overrides standard 'subject' schema field with it's own,
65        # which is a subject of a message. That's why it isn't exported as '<dc:subject />'
66        # element (it is concerned with current implementation of Marshall's CMF namespaces
67        value = atapi.BaseObject.__getitem__(self.context, self.mail_subject_field)
68        if value:
69            node = doc.createElementNS(AT_NS, "field")
70            name_attr = doc.createAttribute("name")
71            name_attr.value = self.mail_subject_field
72            node.setAttributeNode(name_attr)
73            value_node = doc.createTextNode(str(value))
74            node.appendChild(value_node)
75            node.normalize()
76            root.appendChild(node)
77
78        # append form fields
79        fm_doc = minidom.parseString(self.context.form.get_xml())
80        root.appendChild(fm_doc.documentElement)
81
82        return doc.toxml('utf-8')
Note: See TracBrowser for help on using the repository browser.