1 | import logging |
---|
2 | from zope.interface import classProvides, implements |
---|
3 | |
---|
4 | from collective.transmogrifier.interfaces import ISectionBlueprint |
---|
5 | from collective.transmogrifier.interfaces import ISection |
---|
6 | |
---|
7 | from Acquisition import aq_inner, aq_parent |
---|
8 | from Products.CMFCore import utils |
---|
9 | |
---|
10 | log = logging.getLogger('quintagroup.transmogrifier.simpleblog2quills.blogentrycleaner') |
---|
11 | |
---|
12 | class BlogEntryCleaner(object): |
---|
13 | classProvides(ISectionBlueprint) |
---|
14 | implements(ISection) |
---|
15 | |
---|
16 | def __init__(self, transmogrifier, name, options, previous): |
---|
17 | self.transmogrifier = transmogrifier |
---|
18 | self.context = transmogrifier.context |
---|
19 | self.previous = previous |
---|
20 | |
---|
21 | def __iter__(self): |
---|
22 | for entry in self.getNotEmptyEntries(): |
---|
23 | self.moveContainedUp(entry) |
---|
24 | |
---|
25 | for item in self.previous: |
---|
26 | yield item |
---|
27 | |
---|
28 | def moveContainedUp(self, entry): |
---|
29 | """ Move contained object from blog entry one level up. |
---|
30 | """ |
---|
31 | entry = aq_inner(entry) |
---|
32 | ids = entry.contentValues() |
---|
33 | |
---|
34 | log.info('Moving from %s BlogEntry next object: %s' % (entry.getId(), ids)) |
---|
35 | copy_data = entry.manage_cutObjects(ids) |
---|
36 | parent = aq_parent(entry) |
---|
37 | # xxx: next method call raises exception |
---|
38 | parent.manage_pasteObjects(copy_data) |
---|
39 | |
---|
40 | def getNotEmptyEntries(self): |
---|
41 | """ Find all blog entries in the site that have contained objects. |
---|
42 | """ |
---|
43 | not_empty = [] |
---|
44 | catalog = utils.getToolByName(self.context, 'portal_catalog') |
---|
45 | for entry in catalog(portal_type='BlogEntry'): |
---|
46 | entry = entry.getObject() |
---|
47 | contained = entry.contentIds() |
---|
48 | if contained: |
---|
49 | not_empty.append(entry) |
---|
50 | return not_empty |
---|