1 | import logging |
---|
2 | from zope.interface import implements |
---|
3 | from zope.component import queryMultiAdapter |
---|
4 | from plone.indexer.interfaces import IIndexableObject |
---|
5 | |
---|
6 | from Missing import MV |
---|
7 | from Acquisition import aq_inner |
---|
8 | from Acquisition import aq_parent |
---|
9 | |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | from Products.ZCatalog.Catalog import safe_callable |
---|
12 | from Products.CMFPlone.CatalogTool import register_bbb_indexers |
---|
13 | from Products.CMFPlone.CatalogTool import _old_IIndexableObjectWrapper |
---|
14 | |
---|
15 | from quintagroup.catalogupdater.interfaces import ICatalogUpdater |
---|
16 | |
---|
17 | LOG = logging.getLogger('quintagroup.catalogupdater') |
---|
18 | |
---|
19 | |
---|
20 | class CatalogUpdaterUtility(object): |
---|
21 | |
---|
22 | implements(ICatalogUpdater) |
---|
23 | |
---|
24 | def validate(self, cat, col): |
---|
25 | # Validate catalog and column name |
---|
26 | _cat = getattr(cat, '_catalog', None) |
---|
27 | |
---|
28 | if _cat is None: |
---|
29 | raise AttributeError("%s - is not ZCatalog based catalog" % cat) |
---|
30 | |
---|
31 | if not _cat.schema.has_key(col): |
---|
32 | raise AttributeError("'%s' - not presented column in %s catalog " % (col, cat)) |
---|
33 | |
---|
34 | def getWrapedObject(self, obj, portal, catalog): |
---|
35 | # Returned wrapped 'obj' object with IIndexable wrapper |
---|
36 | w = obj |
---|
37 | if not IIndexableObject.providedBy(obj): |
---|
38 | # BBB: Compatibility wrapper lookup. Should be removed in Plone 4. |
---|
39 | register_bbb_indexers() |
---|
40 | wrapper = queryMultiAdapter((obj, portal), _old_IIndexableObjectWrapper) |
---|
41 | if wrapper is not None: |
---|
42 | w = wrapper |
---|
43 | else: |
---|
44 | # This is the CMF 2.2 compatible approach, which should be used going forward |
---|
45 | wrapper = queryMultiAdapter((obj, catalog), IIndexableObject) |
---|
46 | if wrapper is not None: |
---|
47 | w = wrapper |
---|
48 | return w |
---|
49 | |
---|
50 | |
---|
51 | def updateMetadata4All(self, catalog, column): |
---|
52 | """ Look into appropriate method of ICatalogUpdate interface |
---|
53 | """ |
---|
54 | self.validate(catalog, column) |
---|
55 | |
---|
56 | _catalog = catalog._catalog |
---|
57 | portal = getToolByName(catalog, 'portal_url').getPortalObject() |
---|
58 | root = aq_parent(portal) |
---|
59 | |
---|
60 | data = _catalog.data |
---|
61 | schema = _catalog.schema |
---|
62 | indx = schema[column] |
---|
63 | paths = _catalog.paths |
---|
64 | |
---|
65 | # For each catalog record update metadata |
---|
66 | for rid, md in data.items(): |
---|
67 | # get an object |
---|
68 | obj_uid = paths[rid] |
---|
69 | try: |
---|
70 | obj = root.unrestrictedTraverse(obj_uid) |
---|
71 | obj = self.getWrapedObject(obj, portal, catalog) |
---|
72 | except: |
---|
73 | LOG.error('update_metadata_column could not resolve ' |
---|
74 | 'an object from the uid %r.' % obj_uid) |
---|
75 | continue |
---|
76 | |
---|
77 | # calculate the column value |
---|
78 | mdlist = list(md) |
---|
79 | attr=getattr(obj, column, MV) |
---|
80 | if(attr is not MV and safe_callable(attr)): attr=attr() |
---|
81 | |
---|
82 | # update metadata value |
---|
83 | mdlist[indx] = attr |
---|
84 | data[rid] = tuple(mdlist) |
---|
85 | |
---|