[1755] | 1 | import logging, types |
---|
[1764] | 2 | import transaction |
---|
[1754] | 3 | from zope.interface import implements |
---|
[1865] | 4 | from zope.component import getMultiAdapter |
---|
[1754] | 5 | from zope.component import queryMultiAdapter |
---|
| 6 | |
---|
| 7 | from Missing import MV |
---|
| 8 | from Acquisition import aq_inner |
---|
| 9 | from Acquisition import aq_parent |
---|
| 10 | |
---|
| 11 | from Products.CMFCore.utils import getToolByName |
---|
| 12 | from Products.ZCatalog.Catalog import safe_callable |
---|
| 13 | |
---|
[1865] | 14 | try: |
---|
| 15 | from plone.indexer.interfaces import IIndexableObject |
---|
| 16 | except ImportError: |
---|
| 17 | from plone.app.content.interfaces import IIndexableObjectWrapper \ |
---|
| 18 | as _old_IIndexableObjectWrapper |
---|
| 19 | IS_NEW = False |
---|
| 20 | else: |
---|
| 21 | IS_NEW = True |
---|
| 22 | |
---|
| 23 | |
---|
[1754] | 24 | from quintagroup.catalogupdater.interfaces import ICatalogUpdater |
---|
| 25 | |
---|
| 26 | LOG = logging.getLogger('quintagroup.catalogupdater') |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | class CatalogUpdaterUtility(object): |
---|
| 30 | |
---|
| 31 | implements(ICatalogUpdater) |
---|
| 32 | |
---|
[1755] | 33 | def validate(self, cat, cols): |
---|
[1754] | 34 | # Validate catalog and column name |
---|
[1755] | 35 | AVAIL_COLTYPES = list(types.StringTypes) + [types.ListType, types.TupleType] |
---|
| 36 | |
---|
[1754] | 37 | _cat = getattr(cat, '_catalog', None) |
---|
| 38 | if _cat is None: |
---|
| 39 | raise AttributeError("%s - is not ZCatalog based catalog" % cat) |
---|
| 40 | |
---|
[1755] | 41 | if not type(cols) in AVAIL_COLTYPES: |
---|
| 42 | raise TypeError("'columns' parameter must be one of the following " \ |
---|
| 43 | "types: %s" % AVAIL_COLTYPES) |
---|
| 44 | # Normalize columns |
---|
| 45 | if type(cols) in types.StringTypes: |
---|
| 46 | cols = [cols,] |
---|
| 47 | # Check is every column present in the catalog |
---|
| 48 | for col in cols: |
---|
| 49 | if not _cat.schema.has_key(col): |
---|
| 50 | raise AttributeError("'%s' - not presented column in %s catalog " % (col, cat)) |
---|
[1754] | 51 | |
---|
[1755] | 52 | return _cat, cols |
---|
| 53 | |
---|
[1754] | 54 | |
---|
[1865] | 55 | def getWrappedObjectNew(self, obj, portal, catalog): |
---|
| 56 | # Returned wrapped 'obj' object with IIndexable wrapper |
---|
[1981] | 57 | wrapper = None |
---|
[1865] | 58 | if not IIndexableObject.providedBy(obj): |
---|
[1981] | 59 | # This is the CMF 2.2 compatible approach, which should be used going forward |
---|
| 60 | wrapper = queryMultiAdapter((obj, catalog), IIndexableObject) |
---|
| 61 | return wrapper and wrapper or obj |
---|
[1754] | 62 | |
---|
[1865] | 63 | def getWrappedObjectOld(self, obj, portal, catalog): |
---|
| 64 | # Returned wrapped 'obj' object with IIndexable wrapper |
---|
| 65 | wf = getattr(self, 'portal_workflow', None) |
---|
| 66 | # A comment for all the frustrated developers which aren't able to pin |
---|
| 67 | # point the code which adds the review_state to the catalog. :) |
---|
| 68 | # The review_state var and some other workflow vars are added to the |
---|
| 69 | # indexable object wrapper throught the code in the following lines |
---|
| 70 | if wf is not None: |
---|
| 71 | vars = wf.getCatalogVariablesFor(obj) |
---|
| 72 | else: |
---|
| 73 | vars = {} |
---|
| 74 | |
---|
| 75 | w = getMultiAdapter((obj, portal), _old_IIndexableObjectWrapper) |
---|
| 76 | w.update(vars) |
---|
| 77 | |
---|
| 78 | return w |
---|
| 79 | |
---|
| 80 | |
---|
[1755] | 81 | def updateMetadata4All(self, catalog, columns): |
---|
[1754] | 82 | """ Look into appropriate method of ICatalogUpdate interface |
---|
| 83 | """ |
---|
| 84 | |
---|
[1755] | 85 | _catalog, columns = self.validate(catalog, columns) |
---|
| 86 | |
---|
[1754] | 87 | portal = getToolByName(catalog, 'portal_url').getPortalObject() |
---|
| 88 | root = aq_parent(portal) |
---|
| 89 | |
---|
| 90 | data = _catalog.data |
---|
| 91 | schema = _catalog.schema |
---|
| 92 | paths = _catalog.paths |
---|
[1865] | 93 | getWrappedObject = IS_NEW and self.getWrappedObjectNew or self.getWrappedObjectOld |
---|
[1764] | 94 | # For subtransaction support |
---|
| 95 | threshold = getattr(catalog, 'threshold', 10000) |
---|
| 96 | _v_total = 0 |
---|
| 97 | _v_transaction = None |
---|
[1754] | 98 | |
---|
| 99 | # For each catalog record update metadata |
---|
| 100 | for rid, md in data.items(): |
---|
| 101 | # get an object |
---|
| 102 | obj_uid = paths[rid] |
---|
| 103 | try: |
---|
| 104 | obj = root.unrestrictedTraverse(obj_uid) |
---|
[1865] | 105 | obj = getWrappedObject(obj, portal, catalog) |
---|
[1754] | 106 | except: |
---|
[1755] | 107 | LOG.error('updateMetadata4All could not resolve ' |
---|
[1754] | 108 | 'an object from the uid %r.' % obj_uid) |
---|
| 109 | continue |
---|
| 110 | |
---|
| 111 | mdlist = list(md) |
---|
[1755] | 112 | for column in columns: |
---|
| 113 | # calculate the column value |
---|
| 114 | attr=getattr(obj, column, MV) |
---|
| 115 | if(attr is not MV and safe_callable(attr)): attr=attr() |
---|
| 116 | # Update metadata value |
---|
| 117 | indx = schema[column] |
---|
| 118 | mdlist[indx] = attr |
---|
[1754] | 119 | |
---|
[1755] | 120 | # Update catalog record |
---|
[1754] | 121 | data[rid] = tuple(mdlist) |
---|
| 122 | |
---|
[1764] | 123 | # Steeled from ZCatalog |
---|
| 124 | if threshold is not None: |
---|
| 125 | # figure out whether or not to commit a subtransaction. |
---|
| 126 | t = id(transaction.get()) |
---|
| 127 | if t != _v_transaction: |
---|
| 128 | _v_total = 0 |
---|
| 129 | _v_transaction = t |
---|
| 130 | _v_total = _v_total + 1 |
---|
| 131 | if _v_total > threshold: |
---|
| 132 | transaction.savepoint(optimistic=True) |
---|
| 133 | catalog._p_jar.cacheGC() |
---|
| 134 | _v_total = 0 |
---|
| 135 | LOG.info('commiting subtransaction') |
---|
| 136 | |
---|