[Checkins] SVN: Zope/branches/andig-catalog-report/src/Products/ZCatalog/ Make sure we actually do what queryplan did and support all the weird ways in which one can pass query restrictions to the catalog

Hanno Schlichting hannosch at hannosch.eu
Sat Jul 24 13:50:26 EDT 2010


Log message for revision 115039:
  Make sure we actually do what queryplan did and support all the weird ways in which one can pass query restrictions to the catalog
  

Changed:
  U   Zope/branches/andig-catalog-report/src/Products/ZCatalog/CatalogReport.py
  U   Zope/branches/andig-catalog-report/src/Products/ZCatalog/tests/testCatalog.py

-=-
Modified: Zope/branches/andig-catalog-report/src/Products/ZCatalog/CatalogReport.py
===================================================================
--- Zope/branches/andig-catalog-report/src/Products/ZCatalog/CatalogReport.py	2010-07-24 17:34:36 UTC (rev 115038)
+++ Zope/branches/andig-catalog-report/src/Products/ZCatalog/CatalogReport.py	2010-07-24 17:50:26 UTC (rev 115039)
@@ -27,7 +27,7 @@
 MAX_DISTINCT_VALUES = 10
 
 
-def determine_value_indexes(catalog):
+def determine_value_indexes(indexes):
     # This function determines all indexes whose values should be respected
     # in the report key. The number of unique values for the index needs to be
     # lower than the MAX_DISTINCT_VALUES watermark.
@@ -45,7 +45,7 @@
         return value_indexes
 
     new_value_indexes = set()
-    for name, index in catalog.indexes.items():
+    for name, index in indexes.items():
         if IUniqueValueIndex.providedBy(index):
             values = index.uniqueValues()
             if values and len(values) < MAX_DISTINCT_VALUES:
@@ -75,18 +75,39 @@
 del addCleanUp
 
 
-def make_key(catalog, request):
-    valueindexes = determine_value_indexes(catalog)
-
+def make_query(indexes, request):
+    # This is a bit of a mess, but the ZCatalog API supports passing
+    # in query restrictions in almost arbitary ways
     if isinstance(request, dict):
-        keydict = request.copy()
+        query = request.copy()
     else:
-        keydict = {}
-        keydict.update(request.keywords)
-        if isinstance(request.request, dict):
-            keydict.update(request.request)
-    key = keys = keydict.keys()
+        query = {}
+        query.update(request.keywords)
+        real_req = request.request
+        if isinstance(real_req, dict):
+            query.update(real_req)
 
+        known_keys = query.keys()
+        # The request has too many places where an index restriction might be
+        # specified. Putting all of request.form, request.other, ... into the
+        # key isn't what we want either, so we iterate over all known indexes
+        # instead and see if they are in the request.
+        for iid in indexes.keys():
+            if iid in known_keys:
+                continue
+            value = real_req.get(iid)
+            if value:
+                query[iid] = value
+    return query
+
+
+def make_key(catalog, request):
+    indexes = catalog.indexes
+    valueindexes = determine_value_indexes(indexes)
+
+    query = make_query(indexes, request)
+    key = keys = query.keys()
+
     values = [name for name in keys if name in valueindexes]
     if values:
         # If we have indexes whose values should be considered, we first
@@ -95,7 +116,7 @@
         key = [name for name in keys if name not in values]
         for name in values:
 
-            v = keydict.get(name, [])
+            v = query.get(name, [])
             if isinstance(v, (tuple, list)):
                 v = list(v)
                 v.sort()

Modified: Zope/branches/andig-catalog-report/src/Products/ZCatalog/tests/testCatalog.py
===================================================================
--- Zope/branches/andig-catalog-report/src/Products/ZCatalog/tests/testCatalog.py	2010-07-24 17:34:36 UTC (rev 115038)
+++ Zope/branches/andig-catalog-report/src/Products/ZCatalog/tests/testCatalog.py	2010-07-24 17:50:26 UTC (rev 115039)
@@ -899,7 +899,7 @@
 
          self.assertEqual(4, len(self.zcat.getCatalogReport()))
 
-     def test_ReportCounter(self):
+    def test_ReportCounter(self):
          """ tests the counter of equal queries """
 
          self.zcat.manage_resetCatalogReport()
@@ -913,7 +913,7 @@
          self.assertEqual(r['counter'],3)
 
 
-     def test_ReportKey(self):
+    def test_ReportKey(self):
          """ tests the query keys for uniqueness """
 
          # query key 1



More information about the checkins mailing list