source: products/quintagroup.catalogupdater/trunk/quintagroup/catalogupdater/utility.py @ 1754

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

Added utility

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1import logging
2from zope.interface import implements
3from zope.component import queryMultiAdapter
4from plone.indexer.interfaces import IIndexableObject
5
6from Missing import MV
7from Acquisition import aq_inner
8from Acquisition import aq_parent
9
10from Products.CMFCore.utils import getToolByName
11from Products.ZCatalog.Catalog import safe_callable
12from Products.CMFPlone.CatalogTool import register_bbb_indexers
13from Products.CMFPlone.CatalogTool import _old_IIndexableObjectWrapper
14
15from quintagroup.catalogupdater.interfaces import ICatalogUpdater
16
17LOG = logging.getLogger('quintagroup.catalogupdater')
18
19
20class 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
Note: See TracBrowser for help on using the repository browser.