1 | from xml.dom import minidom |
---|
2 | |
---|
3 | from zope.interface import classProvides, implements |
---|
4 | |
---|
5 | from Acquisition import aq_base |
---|
6 | |
---|
7 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
8 | from collective.transmogrifier.utils import defaultMatcher |
---|
9 | |
---|
10 | class DiscussionContainerSection(object): |
---|
11 | classProvides(ISectionBlueprint) |
---|
12 | implements(ISection) |
---|
13 | |
---|
14 | def __init__(self, transmogrifier, name, options, previous): |
---|
15 | self.previous = previous |
---|
16 | self.context = transmogrifier.context |
---|
17 | |
---|
18 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
19 | |
---|
20 | self.doc = minidom.Document() |
---|
21 | |
---|
22 | |
---|
23 | def __iter__(self): |
---|
24 | for item in self.previous: |
---|
25 | pathkey = self.pathkey(*item.keys())[0] |
---|
26 | |
---|
27 | if not pathkey: |
---|
28 | yield item; continue |
---|
29 | |
---|
30 | path = item[pathkey] |
---|
31 | obj = self.context.unrestrictedTraverse(path, None) |
---|
32 | if obj is None: # path doesn't exist |
---|
33 | yield item; continue |
---|
34 | |
---|
35 | # check if object has comments |
---|
36 | discussion_container = getattr(aq_base(obj), 'talkback', None) |
---|
37 | if discussion_container is not None: |
---|
38 | data = self.extractComments(discussion_container) |
---|
39 | if data: |
---|
40 | files = item.setdefault('_files', {}) |
---|
41 | item['_files']['discussioncontainer'] = { |
---|
42 | 'name': '.comments.xml', |
---|
43 | 'data': data, |
---|
44 | } |
---|
45 | |
---|
46 | yield item |
---|
47 | |
---|
48 | def extractComments(self, container): |
---|
49 | doc = self.doc |
---|
50 | |
---|
51 | items = container.objectItems() |
---|
52 | if not items: |
---|
53 | return None |
---|
54 | |
---|
55 | root = doc.createElement('discussion') |
---|
56 | doc.appendChild(root) |
---|
57 | for item_id, item in items: |
---|
58 | hdrlist = item.getMetadataHeaders() |
---|
59 | # get creator (it is displayed in "Posted by") |
---|
60 | hdrlist.append(('Creator', item.Creator())) |
---|
61 | # get modification date (also is displayed) |
---|
62 | hdrlist.append(('Modification_date', item.ModificationDate())) |
---|
63 | # get relation |
---|
64 | hdrlist.append(('In_reply_to', str(item.in_reply_to))) |
---|
65 | # get comment text |
---|
66 | hdrlist.append(('Text', item.text)) |
---|
67 | |
---|
68 | item_elem = doc.createElement('item') |
---|
69 | attr = doc.createAttribute('id') |
---|
70 | attr.value = item_id |
---|
71 | item_elem.setAttributeNode(attr) |
---|
72 | |
---|
73 | for k, v in hdrlist: |
---|
74 | field = doc.createElement('field') |
---|
75 | attr = doc.createAttribute('name') |
---|
76 | attr.value = k |
---|
77 | field.setAttributeNode(attr) |
---|
78 | text = doc.createTextNode(v) |
---|
79 | field.appendChild(text) |
---|
80 | item_elem.appendChild(field) |
---|
81 | |
---|
82 | root.appendChild(item_elem) |
---|
83 | |
---|
84 | # all comments are strings encoded in 'utf-8' and they will properly |
---|
85 | # saved in xml file, but if we explicitly give 'utf-8' encoding |
---|
86 | # UnicodeDecodeError will be raised when they have non-ascii chars |
---|
87 | data = self.doc.toprettyxml(indent=' ') #, encoding='utf-8') |
---|
88 | |
---|
89 | self.doc.unlink() |
---|
90 | return data |
---|