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

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

Updated constructor section for possibility to construct not only CMF objects, registered in fti

File size: 2.5 KB
Line 
1from zope.interface import classProvides, implements
2from zope.component import queryUtility
3from collective.transmogrifier.interfaces import ISectionBlueprint
4from collective.transmogrifier.interfaces import ISection
5from collective.transmogrifier.utils import defaultMatcher
6
7from Acquisition import aq_base
8from Products.CMFCore.utils import getToolByName
9
10from quintagroup.transmogrifier.interfaces import IFTIConstructor4MetaType
11
12class ConstructorSection(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        self.ttool = getToolByName(self.context, 'portal_types')
20       
21        self.typekey = defaultMatcher(options, 'type-key', name, 'type', 
22                                      ('portal_type', 'Type'))
23        self.pathkey = defaultMatcher(options, 'path-key', name, 'path')
24        self.meta_types = filter(None, [i.strip() for i in
25                                         options.get('meta-types', '').splitlines()])
26   
27    def __iter__(self):
28        for item in self.previous:
29            keys = item.keys()
30            typekey = self.typekey(*keys)[0]
31            pathkey = self.pathkey(*keys)[0]
32           
33            if not (typekey and pathkey):             # not enough info
34                yield item; continue
35           
36            type_, path = item[typekey], item[pathkey]
37           
38            fti = self.ttool.getTypeInfo(type_)
39            if fti is None:                           # not an existing type
40                # Look for an IFTIConstructor4MetaType adapter
41                mt = type_.startswith("meta_type:") and type_[10:] or None
42                if mt in self.meta_types:
43                    fti = queryUtility(IFTIConstructor4MetaType, name=mt)
44                if fti is None:
45                    yield item; continue
46
47            elems = path.strip('/').rsplit('/', 1)
48            container, id = (len(elems) == 1 and ('', elems[0]) or elems)
49            context = self.context.unrestrictedTraverse(container, None)
50            if context is None:                       # container doesn't exist
51                yield item; continue
52
53            if getattr(aq_base(context), id, None) is not None: # item exists
54                yield item; continue
55
56            obj = fti._constructInstance(context, id)
57            obj = fti._finishConstruction(obj)
58            if obj.getId() != id:
59                item[pathkey] = '%s/%s' % (container, obj.getId())
60
61            yield item
Note: See TracBrowser for help on using the repository browser.