source: products/quintagroup.transmogrifier/branches/ofs/quintagroup/transmogrifier/constructor.py @ 1598

Last change on this file since 1598 was 1598, checked in by mylan, 14 years ago

Added customization of collection.transmogrifier.constructor section

File size: 2.1 KB
Line 
1from zope.interface import classProvides, implements
2from collective.transmogrifier.interfaces import ISectionBlueprint
3from collective.transmogrifier.interfaces import ISection
4from collective.transmogrifier.utils import defaultMatcher
5
6from Acquisition import aq_base
7from Products.CMFCore.utils import getToolByName
8
9class ConstructorSection(object):
10    classProvides(ISectionBlueprint)
11    implements(ISection)
12   
13    def __init__(self, transmogrifier, name, options, previous):
14        self.previous = previous
15        self.context = transmogrifier.context
16        self.ttool = getToolByName(self.context, 'portal_types')
17       
18        self.typekey = defaultMatcher(options, 'type-key', name, 'type', 
19                                      ('portal_type', 'Type'))
20        self.pathkey = defaultMatcher(options, 'path-key', name, 'path')
21   
22    def __iter__(self):
23        for item in self.previous:
24            keys = item.keys()
25            typekey = self.typekey(*keys)[0]
26            pathkey = self.pathkey(*keys)[0]
27           
28            if not (typekey and pathkey):             # not enough info
29                yield item; continue
30           
31            type_, path = item[typekey], item[pathkey]
32           
33            fti = self.ttool.getTypeInfo(type_)
34            if fti is None:                           # not an existing type
35                yield item; continue
36           
37            elems = path.strip('/').rsplit('/', 1)
38            container, id = (len(elems) == 1 and ('', elems[0]) or elems)
39            context = self.context.unrestrictedTraverse(container, None)
40            if context is None:                       # container doesn't exist
41                yield item; continue
42           
43            if getattr(aq_base(context), id, None) is not None: # item exists
44                yield item; continue
45           
46            obj = fti._constructInstance(context, id)
47            obj = fti._finishConstruction(obj)
48            if obj.getId() != id:
49                item[pathkey] = '%s/%s' % (container, obj.getId())
50           
51            yield item
Note: See TracBrowser for help on using the repository browser.