1 | import traceback |
---|
2 | |
---|
3 | from zope.interface import classProvides, implements |
---|
4 | from zope import event |
---|
5 | |
---|
6 | from ZODB.POSException import ConflictError |
---|
7 | |
---|
8 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
9 | from collective.transmogrifier.utils import defaultMatcher |
---|
10 | |
---|
11 | from Products.CMFCore import utils |
---|
12 | from Products.Marshall import registry |
---|
13 | from Products.Archetypes.interfaces import IBaseObject |
---|
14 | from Products.Archetypes.event import ObjectInitializedEvent |
---|
15 | from Products.Archetypes.event import ObjectEditedEvent |
---|
16 | |
---|
17 | class MarshallerSection(object): |
---|
18 | classProvides(ISectionBlueprint) |
---|
19 | implements(ISection) |
---|
20 | |
---|
21 | def __init__(self, transmogrifier, name, options, previous): |
---|
22 | self.previous = previous |
---|
23 | self.context = transmogrifier.context |
---|
24 | |
---|
25 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
26 | self.fileskey = options.get('files-key', '_files').strip() |
---|
27 | |
---|
28 | self.excludekey = defaultMatcher(options, 'exclude-key', name, 'excluded_fields') |
---|
29 | self.exclude = filter(None, [i.strip() for i in |
---|
30 | options.get('exclude', '').splitlines()]) |
---|
31 | |
---|
32 | self.atxml = registry.getComponent("atxml") |
---|
33 | |
---|
34 | def __iter__(self): |
---|
35 | for item in self.previous: |
---|
36 | pathkey = self.pathkey(*item.keys())[0] |
---|
37 | |
---|
38 | if not pathkey: |
---|
39 | yield item; continue |
---|
40 | |
---|
41 | path = item[pathkey] |
---|
42 | obj = self.context.unrestrictedTraverse(path, None) |
---|
43 | if obj is None: # path doesn't exist |
---|
44 | yield item; continue |
---|
45 | |
---|
46 | if IBaseObject.providedBy(obj): |
---|
47 | # get list of excluded fields given in options and in item |
---|
48 | excludekey = self.excludekey(*item.keys())[0] |
---|
49 | atns_exclude = tuple(self.exclude) |
---|
50 | if excludekey: |
---|
51 | atns_exclude = tuple(set(item[excludekey]) | set(atns_exclude)) |
---|
52 | |
---|
53 | try: |
---|
54 | content_type, length, data = self.atxml.marshall(obj, atns_exclude=atns_exclude) |
---|
55 | except ConflictError: |
---|
56 | raise |
---|
57 | except: |
---|
58 | data = None |
---|
59 | |
---|
60 | if data or data is None: |
---|
61 | # None value has special meaning for IExportDataCorrector adapter for topic criterias |
---|
62 | files = item.setdefault(self.fileskey, {}) |
---|
63 | item[self.fileskey]['marshall'] = { |
---|
64 | 'name': '.marshall.xml', |
---|
65 | 'data': data, |
---|
66 | } |
---|
67 | |
---|
68 | yield item |
---|
69 | |
---|
70 | class DemarshallerSection(object): |
---|
71 | classProvides(ISectionBlueprint) |
---|
72 | implements(ISection) |
---|
73 | |
---|
74 | def __init__(self, transmogrifier, name, options, previous): |
---|
75 | self.previous = previous |
---|
76 | self.context = transmogrifier.context |
---|
77 | |
---|
78 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
79 | self.fileskey = defaultMatcher(options, 'files-key', name, 'files') |
---|
80 | |
---|
81 | # Marshall doesn't support excluding fields on demarshalling, |
---|
82 | # we can do this with xml.dom.minodom, if it'll be needed in the future |
---|
83 | # self.excludekey = defaultMatcher(options, 'exclude-key', name, 'excluded_fields') |
---|
84 | |
---|
85 | # self.exclude = filter(None, [i.strip() for i in |
---|
86 | # options.get('exclude', '').splitlines()]) |
---|
87 | |
---|
88 | self.atxml = registry.getComponent("atxml") |
---|
89 | |
---|
90 | def __iter__(self): |
---|
91 | for item in self.previous: |
---|
92 | pathkey = self.pathkey(*item.keys())[0] |
---|
93 | fileskey = self.fileskey(*item.keys())[0] |
---|
94 | |
---|
95 | if not (pathkey and fileskey): |
---|
96 | yield item; continue |
---|
97 | if 'marshall' not in item[fileskey]: |
---|
98 | yield item; continue |
---|
99 | |
---|
100 | path = item[pathkey] |
---|
101 | obj = self.context.unrestrictedTraverse(path, None) |
---|
102 | if obj is None: # path doesn't exist |
---|
103 | yield item; continue |
---|
104 | |
---|
105 | if IBaseObject.providedBy(obj): |
---|
106 | try: |
---|
107 | data = item[fileskey]['marshall']['data'] |
---|
108 | self.atxml.demarshall(obj, data) |
---|
109 | # we don't want to call reindexObject because modification_date |
---|
110 | # will be updated, so we call only indexObject (reindexObject does |
---|
111 | # some things with uid catalog too) |
---|
112 | is_new_object = obj.checkCreationFlag() |
---|
113 | obj.indexObject() |
---|
114 | # firing of events |
---|
115 | obj.unmarkCreationFlag() |
---|
116 | if is_new_object: |
---|
117 | event.notify(ObjectInitializedEvent(obj)) |
---|
118 | obj.at_post_create_script() |
---|
119 | else: |
---|
120 | event.notify(ObjectEditedEvent(obj)) |
---|
121 | obj.at_post_edit_script() |
---|
122 | except ConflictError: |
---|
123 | raise |
---|
124 | except Exception, e: |
---|
125 | print 'Exception in demarshaller section:' |
---|
126 | print '-'*60 |
---|
127 | traceback.print_exc() |
---|
128 | print '-'*60 |
---|
129 | |
---|
130 | yield item |
---|
131 | |
---|
132 | # updating security settings on demarshalled content |
---|
133 | wtool = utils.getToolByName(self.context, 'portal_workflow') |
---|
134 | wtool.updateRoleMappings() |
---|
135 | catalog = utils.getToolByName(self.context, 'portal_catalog') |
---|
136 | catalog.reindexIndex('allowedRolesAndUsers', None) |
---|