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

Last change on this file since 1604 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
RevLine 
[1598]1from zope.interface import classProvides, implements
[1604]2from zope.component import queryUtility
[1598]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
[1604]10from quintagroup.transmogrifier.interfaces import IFTIConstructor4MetaType
11
[1598]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')
[1604]24        self.meta_types = filter(None, [i.strip() for i in
25                                         options.get('meta-types', '').splitlines()])
[1598]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
[1604]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
[1598]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
[1604]52
[1598]53            if getattr(aq_base(context), id, None) is not None: # item exists
54                yield item; continue
[1604]55
[1598]56            obj = fti._constructInstance(context, id)
57            obj = fti._finishConstruction(obj)
58            if obj.getId() != id:
59                item[pathkey] = '%s/%s' % (container, obj.getId())
[1604]60
[1598]61            yield item
Note: See TracBrowser for help on using the repository browser.