1 | from zope.interface import classProvides, implements |
---|
2 | from collective.transmogrifier.interfaces import ISectionBlueprint |
---|
3 | from collective.transmogrifier.interfaces import ISection |
---|
4 | from collective.transmogrifier.utils import defaultMatcher |
---|
5 | |
---|
6 | from Acquisition import aq_base |
---|
7 | from Products.CMFCore.utils import getToolByName |
---|
8 | from Products.CMFCore.interfaces import IFolderish |
---|
9 | from Products.Archetypes.interfaces import IBaseFolder |
---|
10 | |
---|
11 | ALLWAYS_EXISTING_ATTRIBUTES = ('index_html', ) |
---|
12 | |
---|
13 | class ConstructorSection(object): |
---|
14 | classProvides(ISectionBlueprint) |
---|
15 | implements(ISection) |
---|
16 | |
---|
17 | def __init__(self, transmogrifier, name, options, previous): |
---|
18 | self.previous = previous |
---|
19 | self.context = transmogrifier.context |
---|
20 | self.ttool = getToolByName(self.context, 'portal_types') |
---|
21 | |
---|
22 | self.typekey = defaultMatcher(options, 'type-key', name, 'type', |
---|
23 | ('portal_type', 'Type')) |
---|
24 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
25 | |
---|
26 | def __iter__(self): |
---|
27 | for item in self.previous: |
---|
28 | keys = item.keys() |
---|
29 | typekey = self.typekey(*keys)[0] |
---|
30 | pathkey = self.pathkey(*keys)[0] |
---|
31 | |
---|
32 | if not (typekey and pathkey): # not enough info |
---|
33 | yield item; continue |
---|
34 | |
---|
35 | type_, path = item[typekey], item[pathkey] |
---|
36 | |
---|
37 | fti = self.ttool.getTypeInfo(type_) |
---|
38 | if fti is None: # not an existing type |
---|
39 | yield item; continue |
---|
40 | |
---|
41 | elems = path.strip('/').rsplit('/', 1) |
---|
42 | container, id = (len(elems) == 1 and ('', elems[0]) or elems) |
---|
43 | context = self.context.unrestrictedTraverse(container, None) |
---|
44 | if context is None: # container doesn't exist |
---|
45 | yield item; continue |
---|
46 | |
---|
47 | # check if context is container, if we didn't do this AttributeError |
---|
48 | # will be raised when calling fti._constructInstance(context, id) |
---|
49 | if not (IFolderish.providedBy(context) or IBaseFolder.providedBy(context)): |
---|
50 | # we can't construct this content item, so remove it from pipeline |
---|
51 | continue |
---|
52 | |
---|
53 | # content object always have some default attributes, but sometimes |
---|
54 | # these attributes can be also used as content ids |
---|
55 | if id in ALLWAYS_EXISTING_ATTRIBUTES: |
---|
56 | # 'getattr' function always return ComputedAttribute object for 'index_html' |
---|
57 | # whether 'index_html' object is in context or no, but 'get' method |
---|
58 | # returns correct value |
---|
59 | if aq_base(context).get(id, None) is not None: |
---|
60 | yield item; continue |
---|
61 | elif getattr(aq_base(context), id, None) is not None: # item exists |
---|
62 | yield item; continue |
---|
63 | |
---|
64 | obj = fti._constructInstance(context, id) |
---|
65 | obj = fti._finishConstruction(obj) |
---|
66 | if obj.getId() != id: |
---|
67 | item[pathkey] = '%s/%s' % (container, obj.getId()) |
---|
68 | |
---|
69 | yield item |
---|