[CMF-checkins] SVN: CMF/branches/1.5/C Refactored reindexObjectSecurity a bit. It now always reindexes the

Florent Guillaume fg at nuxeo.com
Mon Aug 1 14:01:18 EDT 2005


Log message for revision 37626:
  Refactored reindexObjectSecurity a bit. It now always reindexes the
  catalog objects without changing their catalog uid. This is useful for
  third-party code that indexes objects with special uids.
  
  

Changed:
  U   CMF/branches/1.5/CHANGES.txt
  U   CMF/branches/1.5/CMFCore/CMFCatalogAware.py
  U   CMF/branches/1.5/CMFCore/CatalogTool.py
  U   CMF/branches/1.5/CMFCore/tests/test_CMFCatalogAware.py

-=-
Modified: CMF/branches/1.5/CHANGES.txt
===================================================================
--- CMF/branches/1.5/CHANGES.txt	2005-08-01 16:02:49 UTC (rev 37625)
+++ CMF/branches/1.5/CHANGES.txt	2005-08-01 18:01:18 UTC (rev 37626)
@@ -33,7 +33,12 @@
       keywargs support to reflect DCWorkflowDefinition changes. Add a
       test case for this definition as well.
 
+  Others
 
+    - CMFCatalogAware: reindexObjectSecurity() now always reindexes the
+      catalog objects without changing their catalog uid. This is useful
+      for third-party code that indexes objects with special uids.
+
 CMF 1.5.2 (2005/07/17)
 
   Bugs Fixed

Modified: CMF/branches/1.5/CMFCore/CMFCatalogAware.py
===================================================================
--- CMF/branches/1.5/CMFCore/CMFCatalogAware.py	2005-08-01 16:02:49 UTC (rev 37625)
+++ CMF/branches/1.5/CMFCore/CMFCatalogAware.py	2005-08-01 18:01:18 UTC (rev 37626)
@@ -80,39 +80,40 @@
 
     security.declareProtected(ModifyPortalContent, 'reindexObjectSecurity')
     def reindexObjectSecurity(self, skip_self=False):
+        """Reindex security-related indexes on the object.
+
+        Recurses in the children to reindex them too.
+
+        If skip_self is True, only the children will be reindexed. This
+        is a useful optimization if the object itself has just been
+        fully reindexed, as there's no need to reindex its security twice.
         """
-            Reindex security-related indexes on the object
-            (and its descendants).
-        """
         catalog = getToolByName(self, 'portal_catalog', None)
-        if catalog is not None:
-            path = '/'.join(self.getPhysicalPath())
-            for brain in catalog.unrestrictedSearchResults(path=path):
-                brain_path = brain.getPath()
-                # self is treated at the end of the method
-                # Optimization in case of an indexable container
-                if brain_path == path:
-                    continue
-                # Get the object
-                if hasattr(aq_base(brain), '_unrestrictedGetObject'):
-                    ob = brain._unrestrictedGetObject()
-                else:
-                    # BBB older Zope
-                    ob = self.unrestrictedTraverse(brain_path, None)
-                if ob is None:
-                    # Ignore old references to deleted objects.
-                    LOG('reindexObjectSecurity', PROBLEM,
-                        "Cannot get %s from catalog" % brain_path)
-                    continue
-                s = getattr(ob, '_p_changed', 0)
-                catalog.reindexObject(ob, idxs=self._cmf_security_indexes,
-                                      update_metadata=0)
-                if s is None: ob._p_deactivate()
-            # Reindex the object itself in here if not explicitly
-            # asked to not to
-            if not skip_self:
-                catalog.reindexObject(self, idxs=self._cmf_security_indexes,
-                                      update_metadata=0)
+        if catalog is None:
+            return
+        path = '/'.join(self.getPhysicalPath())
+        for brain in catalog.unrestrictedSearchResults(path=path):
+            brain_path = brain.getPath()
+            if brain_path == path and skip_self:
+                continue
+            # Get the object
+            if hasattr(aq_base(brain), '_unrestrictedGetObject'):
+                ob = brain._unrestrictedGetObject()
+            else:
+                # BBB: Zope 2.7
+                ob = self.unrestrictedTraverse(brain_path, None)
+            if ob is None:
+                # BBB: Ignore old references to deleted objects.
+                # Can happen only in Zope 2.7, or when using
+                # catalog-getObject-raises off in Zope 2.8
+                LOG('reindexObjectSecurity', PROBLEM,
+                    "Cannot get %s from catalog" % brain_path)
+                continue
+            # Recatalog with the same catalog uid.
+            s = getattr(ob, '_p_changed', 0)
+            catalog.reindexObject(ob, idxs=self._cmf_security_indexes,
+                                  update_metadata=0, uid=brain_path)
+            if s is None: ob._p_deactivate()
 
     # Workflow methods
     # ----------------

Modified: CMF/branches/1.5/CMFCore/CatalogTool.py
===================================================================
--- CMF/branches/1.5/CMFCore/CatalogTool.py	2005-08-01 16:02:49 UTC (rev 37625)
+++ CMF/branches/1.5/CMFCore/CatalogTool.py	2005-08-01 18:01:18 UTC (rev 37626)
@@ -326,17 +326,25 @@
         self.uncatalog_object(url)
 
     security.declarePrivate('reindexObject')
-    def reindexObject(self, object, idxs=[], update_metadata=1):
-        '''Update catalog after object data has changed.
+    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).
-        '''
-        url = self.__url(object)
+
+        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 != []:
             # Filter out invalid indexes.
             valid_indexes = self._catalog.indexes.keys()
             idxs = [i for i in idxs if i in valid_indexes]
-        self.catalog_object(object, url, idxs, update_metadata)
+        self.catalog_object(object, uid, idxs, update_metadata)
 
     # BBB: for Zope 2.8.0
     # copied from revision 31005 of ZCatalog.py

Modified: CMF/branches/1.5/CMFCore/tests/test_CMFCatalogAware.py
===================================================================
--- CMF/branches/1.5/CMFCore/tests/test_CMFCatalogAware.py	2005-08-01 16:02:49 UTC (rev 37625)
+++ CMF/branches/1.5/CMFCore/tests/test_CMFCatalogAware.py	2005-08-01 18:01:18 UTC (rev 37626)
@@ -76,7 +76,7 @@
         self.obs = []
     def indexObject(self, ob):
         self.log.append('index %s' % physicalpath(ob))
-    def reindexObject(self, ob, idxs=[], update_metadata=0):
+    def reindexObject(self, ob, idxs=[], update_metadata=0, uid=None):
         self.log.append('reindex %s %s' % (physicalpath(ob), idxs))
     def unindexObject(self, ob):
         self.log.append('unindex %s' % physicalpath(ob))
@@ -87,9 +87,6 @@
         for ob, obpath in self.obs:
             if not (obpath+'/').startswith(path+'/'):
                 continue
-            if obpath == path:
-                # Normal PathIndex skips initial value
-                continue
             res.append(self.brain_class(ob, obpath))
         return res
 



More information about the CMF-checkins mailing list