X-Git-Url: https://scm.cri.ensmp.fr/git/Plinn.git/blobdiff_plain/a4cb4d96face514924387d34746b3148848ac092..37ead613364e686ff8d931ff78424a87f37848cc:/catalog.py?ds=sidebyside diff --git a/catalog.py b/catalog.py index 0a457f8..69bb701 100644 --- a/catalog.py +++ b/catalog.py @@ -5,7 +5,7 @@ from Products.CMFCore.interfaces import IIndexableObject from Products.CMFCore.CatalogTool import CatalogTool as BaseCatalogTool from Products.CMFCore.CatalogTool import IndexableObjectWrapper from Products.PageTemplates.PageTemplateFile import PageTemplateFile -from Products.CMFCore.permissions import ModifyPortalContent +from Products.CMFCore.permissions import ModifyPortalContent, ManagePortal from zope.component import queryMultiAdapter from Products.ZCatalog.Catalog import Catalog import transaction @@ -43,7 +43,8 @@ class CatalogTool(BaseCatalogTool) : manage_options = (BaseCatalogTool.manage_options[:5] + ({'label' : 'Solr', 'action' : 'manage_solr'},) + BaseCatalogTool.manage_options[5:]) - manage_solr = PageTemplateFile('www/manage_solr', globals()) + manage_solr = PageTemplateFile('www/manage_solr.pt', globals(), __name__='manage_solr') + def __init__(self, idxs=[]) : @@ -52,6 +53,18 @@ class CatalogTool(BaseCatalogTool) : self.solr_url = 'http://localhost:8983/solr' self.delegatedIndexes = ('Title', 'Description', 'SearchableText') + security.declarePublic('getDelegatedIndexes') + def getDelegatedIndexes(self) : + """ read the method name """ + return self.delegatedIndexes + + security.declareProtected(ManagePortal, 'setDelegatedIndexes') + def setDelegatedIndexes(self, indexes, REQUEST=None) : + """setDelegatedIndexes documentation""" + self.delegatedIndexes = tuple([i.strip() for i in indexes if i.strip()]) + if REQUEST : + REQUEST.RESPONSE.redirect(self.absolute_url() + '/manage_solr?manage_tabs_message=Saved changes.') + def _getSolrConnection(self) : if not hasattr(self, _VOLATILE_SOLR_NAME) : con = SolrConnection(self.solr_url) @@ -61,17 +74,10 @@ class CatalogTool(BaseCatalogTool) : return getattr(self, _VOLATILE_SOLR_NAME) security.declarePrivate('solrAdd') - def solrAdd(self, object, idxs=[], uid=None) : - if IIndexableObject.providedBy(object): - w = object - else: - w = queryMultiAdapter( (object, self), IIndexableObject ) - if w is None: - # BBB - w = IndexableObjectWrapper(object, self) - - uid = uid if uid else self.__url(object) - idxs = idxs if idxs !=[] else self.delegatedIndexes + def solrAdd(self, w, uid, idxs) : + idxs = idxs if idxs else self.delegatedIndexes + # Filter out delegated indexes + idxs = [i for i in idxs if i in self.delegatedIndexes] data = {'id' : uid} for name in idxs : attr = getattr(w, name, '') @@ -79,29 +85,45 @@ class CatalogTool(BaseCatalogTool) : c = self._getSolrConnection() c.add(**data) - # PortalCatalog api overloads - security.declareProtected(ModifyPortalContent, 'indexObject') - def indexObject(self, object) : - """ Add to catalog and send to Solr """ - super(CatalogTool, self).indexObject(object) - self.solrAdd(object) - - security.declarePrivate('reindexObject') - def reindexObject(self, object, idxs=[], update_metadata=1, uid=None): - super(CatalogTool, self).reindexObject(object, - idxs=idxs, - update_metadata=update_metadata, - uid=uid) - if idxs != []: + def catalog_object(self, obj, uid=None, idxs=None, update_metadata=1, + pghandler=None): + # Wraps the object with workflow and accessibility + # information just before cataloging. + if IIndexableObject.providedBy(obj): + w = obj + else: + w = queryMultiAdapter( (obj, self), IIndexableObject ) + if w is None: + # BBB + w = IndexableObjectWrapper(obj, self) + + idxs_ = idxs + if idxs: # Filter out invalid indexes. valid_indexes = self._catalog.indexes.keys() - idxs = [i for i in idxs if i in valid_indexes and i in self.delegatedIndexes] - else : - idxs = self.delegatedIndexes + idxs_ = [i for i in idxs if i in valid_indexes] + + super(CatalogTool, self).catalog_object(w, uid, idxs_, update_metadata, pghandler) + self.solrAdd(w, uid, idxs) + + security.declarePrivate('reindexObject') + def reindexObject(self, object, idxs=[], update_metadata=1, uid=None): + """Update catalog after object data has changed. + + The optional idxs argument is a list of specific indexes + to update (all of them by default). + + The update_metadata flag controls whether the object's + metadata record is updated as well. + + If a non-None uid is passed, it will be used as the catalog uid + for the object instead of its physical path. + """ + if uid is None: + uid = self.__url(object) - if idxs : - self.solrAdd(object, idxs=idxs, uid=uid) + self.catalog_object(object, uid, idxs, update_metadata) security.declarePrivate('unindexObject') def unindexObject(self, object):