source: products/quintagroup.transmogrifier/trunk/quintagroup/transmogrifier/marshall.py @ 333

Last change on this file since 333 was 333, checked in by piv, 18 years ago

fixed bug related with kupu

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