1 | from xml.dom import minidom |
---|
2 | |
---|
3 | from zope.interface import classProvides, implements, providedBy |
---|
4 | from zope.component import getUtilitiesFor, queryMultiAdapter |
---|
5 | |
---|
6 | from plone.portlets.interfaces import ILocalPortletAssignable, IPortletManager,\ |
---|
7 | IPortletAssignmentMapping |
---|
8 | from plone.portlets.constants import CONTEXT_CATEGORY |
---|
9 | from plone.app.portlets.interfaces import IPortletTypeInterface |
---|
10 | from plone.app.portlets.exportimport.interfaces import IPortletAssignmentExportImportHandler |
---|
11 | |
---|
12 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
13 | from collective.transmogrifier.utils import defaultMatcher |
---|
14 | |
---|
15 | class PortletsExporterSection(object): |
---|
16 | classProvides(ISectionBlueprint) |
---|
17 | implements(ISection) |
---|
18 | |
---|
19 | def __init__(self, transmogrifier, name, options, previous): |
---|
20 | self.previous = previous |
---|
21 | self.context = transmogrifier.context |
---|
22 | |
---|
23 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
24 | self.fileskey = options.get('files-key', '_files').strip() |
---|
25 | |
---|
26 | self.doc = minidom.Document() |
---|
27 | |
---|
28 | def __iter__(self): |
---|
29 | self.portlet_schemata = dict([(iface, name,) for name, iface in |
---|
30 | getUtilitiesFor(IPortletTypeInterface)]) |
---|
31 | self.portlet_managers = getUtilitiesFor(IPortletManager) |
---|
32 | |
---|
33 | for item in self.previous: |
---|
34 | pathkey = self.pathkey(*item.keys())[0] |
---|
35 | |
---|
36 | if not pathkey: |
---|
37 | yield item; continue |
---|
38 | |
---|
39 | path = item[pathkey] |
---|
40 | obj = self.context.unrestrictedTraverse(path, None) |
---|
41 | if obj is None: # path doesn't exist |
---|
42 | yield item; continue |
---|
43 | |
---|
44 | if ILocalPortletAssignable.providedBy(obj): |
---|
45 | data = None |
---|
46 | |
---|
47 | root = self.doc.createElement('portlets') |
---|
48 | for elem in self.exportAssignments(obj): |
---|
49 | root.appendChild(elem) |
---|
50 | #for elem in self.exportBlacklists(obj) |
---|
51 | #root.appendChild(elem) |
---|
52 | if root.hasChildNodes(): |
---|
53 | self.doc.appendChild(root) |
---|
54 | data = self.doc.toprettyxml(indent=' ', encoding='utf-8') |
---|
55 | self.doc.unlink() |
---|
56 | |
---|
57 | if data: |
---|
58 | files = item.setdefault(self.fileskey, {}) |
---|
59 | item[self.fileskey]['portlets'] = { |
---|
60 | 'name': '.portlets.xml', |
---|
61 | 'data': data, |
---|
62 | } |
---|
63 | yield item |
---|
64 | |
---|
65 | def exportAssignments(self, obj): |
---|
66 | assignments = [] |
---|
67 | for manager_name, manager in self.portlet_managers: |
---|
68 | mapping = queryMultiAdapter((obj, manager), IPortletAssignmentMapping) |
---|
69 | mapping = mapping.__of__(obj) |
---|
70 | |
---|
71 | for name, assignment in mapping.items(): |
---|
72 | type_ = None |
---|
73 | for schema in providedBy(assignment).flattened(): |
---|
74 | type_ = self.portlet_schemata.get(schema, None) |
---|
75 | if type_ is not None: |
---|
76 | break |
---|
77 | |
---|
78 | if type_ is not None: |
---|
79 | child = self.doc.createElement('assignment') |
---|
80 | child.setAttribute('manager', manager_name) |
---|
81 | child.setAttribute('category', CONTEXT_CATEGORY) |
---|
82 | child.setAttribute('key', '/'.join(obj.getPhysicalPath())) |
---|
83 | child.setAttribute('type', type_) |
---|
84 | child.setAttribute('name', name) |
---|
85 | |
---|
86 | assignment = assignment.__of__(mapping) |
---|
87 | # use existing adapter for exporting a portlet assignment |
---|
88 | handler = IPortletAssignmentExportImportHandler(assignment) |
---|
89 | handler.export_assignment(schema, self.doc, child) |
---|
90 | |
---|
91 | assignments.append(child) |
---|
92 | |
---|
93 | return assignments |
---|
94 | |
---|
95 | class PortletsImporterSection(object): |
---|
96 | classProvides(ISectionBlueprint) |
---|
97 | implements(ISection) |
---|
98 | |
---|
99 | def __init__(self, transmogrifier, name, options, previous): |
---|
100 | self.previous = previous |
---|
101 | self.context = transmogrifier.context |
---|
102 | |
---|
103 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
104 | self.fileskey = defaultMatcher(options, 'files-key', name, 'files') |
---|
105 | |
---|
106 | def __iter__(self): |
---|
107 | |
---|
108 | for item in self.previous: |
---|
109 | pathkey = self.pathkey(*item.keys())[0] |
---|
110 | fileskey = self.fileskey(*item.keys())[0] |
---|
111 | |
---|
112 | if not (pathkey and fileskey): |
---|
113 | yield item; continue |
---|
114 | if 'portlets' not in item[fileskey]: |
---|
115 | yield item; continue |
---|
116 | |
---|
117 | path = item[pathkey] |
---|
118 | obj = self.context.unrestrictedTraverse(path, None) |
---|
119 | if obj is None: # path doesn't exist |
---|
120 | yield item; continue |
---|
121 | |
---|
122 | #if ILocalPortletAssignable.providedBy(obj): |
---|
123 | #data = None |
---|
124 | #data = item[fileskey]['portlets']['data'] |
---|
125 | #doc = minidom.parseString(data) |
---|
126 | #root = doc.documentElement |
---|
127 | #children = [k for k in root.childNodes] |
---|
128 | #for child in children: |
---|
129 | #if child.nodeName != 'assignment': |
---|
130 | #continue |
---|
131 | #self.importPortlet(child) |
---|
132 | |
---|
133 | yield item |
---|