[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Fri, 25 Sep 2009 09:09:02 +0200
changeset 3469 1e28876c4b55
parent 3468 b02fa4db2868
child 3471 8c57c71b859c
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
web/test/unittest_magicsearch.py
web/views/magicsearch.py
--- a/web/test/unittest_magicsearch.py	Thu Sep 24 21:20:15 2009 +0200
+++ b/web/test/unittest_magicsearch.py	Fri Sep 25 09:09:02 2009 +0200
@@ -49,16 +49,16 @@
     def test_basic_translations(self):
         """tests basic translations (no ambiguities)"""
         rql = "Any C WHERE C is Adresse, P adel C, C adresse 'Logilab'"
-        rql, = self.proc.preprocess_query(rql, self.req)
+        rql, = self.proc.preprocess_query(rql)
         self.assertEquals(rql, "Any C WHERE C is EmailAddress, P use_email C, C address 'Logilab'")
 
     def test_ambiguous_translations(self):
         """tests possibly ambiguous translations"""
         rql = "Any P WHERE P adel C, C is EmailAddress, C nom 'Logilab'"
-        rql, = self.proc.preprocess_query(rql, self.req)
+        rql, = self.proc.preprocess_query(rql)
         self.assertEquals(rql, "Any P WHERE P use_email C, C is EmailAddress, C alias 'Logilab'")
         rql = "Any P WHERE P is Utilisateur, P adel C, P nom 'Smith'"
-        rql, = self.proc.preprocess_query(rql, self.req)
+        rql, = self.proc.preprocess_query(rql)
         self.assertEquals(rql, "Any P WHERE P is CWUser, P use_email C, P surname 'Smith'")
 
 
@@ -168,9 +168,9 @@
             (u'CWUser prénom cubicweb', (u'CWUser C WHERE C firstname %(text)s', {'text': 'cubicweb'},)),
             ]
         for query, expected in queries:
-            self.assertEquals(self.proc.preprocess_query(query, self.req), expected)
+            self.assertEquals(self.proc.preprocess_query(query), expected)
         self.assertRaises(BadRQLQuery,
-                          self.proc.preprocess_query, 'Any X WHERE X is Something', self.req)
+                          self.proc.preprocess_query, 'Any X WHERE X is Something')
 
 
 
@@ -201,21 +201,21 @@
              ('Any P WHERE P is CWUser, P surname "Smith"', None)),
             ]
         for query, expected in queries:
-            rset = self.proc.process_query(query, self.req)
+            rset = self.proc.process_query(query)
             self.assertEquals((rset.rql, rset.args), expected)
 
     def test_iso88591_fulltext(self):
         """we must be able to type accentuated characters in the search field"""
-        rset = self.proc.process_query(u'écrire', self.req)
+        rset = self.proc.process_query(u'écrire')
         self.assertEquals(rset.rql, "Any X WHERE X has_text %(text)s")
         self.assertEquals(rset.args, {'text': u'écrire'})
 
     def test_explicit_component(self):
         self.assertRaises(RQLSyntaxError,
-                          self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith",', self.req)
+                          self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith",')
         self.assertRaises(BadRQLQuery,
-                          self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith"', self.req)
-        rset = self.proc.process_query(u'text: utilisateur Smith', self.req)
+                          self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith"')
+        rset = self.proc.process_query(u'text: utilisateur Smith')
         self.assertEquals(rset.rql, 'Any X WHERE X has_text %(text)s')
         self.assertEquals(rset.args, {'text': u'utilisateur Smith'})
 
--- a/web/views/magicsearch.py	Thu Sep 24 21:20:15 2009 +0200
+++ b/web/views/magicsearch.py	Fri Sep 25 09:09:02 2009 +0200
@@ -11,6 +11,7 @@
 
 import re
 from logging import getLogger
+from warnings import warn
 
 from rql import RQLSyntaxError, BadRQLQuery, parse
 from rql.nodes import Relation
@@ -140,15 +141,15 @@
     # component
     name = None
 
-    def process_query(self, uquery, req):
-        args = self.preprocess_query(uquery, req)
+    def process_query(self, uquery):
+        args = self.preprocess_query(uquery)
         try:
-            return req.execute(*args)
+            return self._cw.execute(*args)
         finally:
             # rollback necessary to avoid leaving the connection in a bad state
-            req.cnx.rollback()
+            self._cw.cnx.rollback()
 
-    def preprocess_query(self, uquery, req):
+    def preprocess_query(self, uquery):
         raise NotImplementedError()
 
 
@@ -160,7 +161,7 @@
     """
     name = 'rql'
     priority = 0
-    def preprocess_query(self, uquery, req):
+    def preprocess_query(self, uquery):
         return uquery,
 
 
@@ -169,11 +170,12 @@
     and attributes
     """
     priority = 2
-    def preprocess_query(self, uquery, req):
+    def preprocess_query(self, uquery):
         rqlst = parse(uquery, print_errors=False)
-        schema = self._cw.vreg.schema
+        schema = self._cw.schema
         # rql syntax tree will be modified in place if necessary
-        translate_rql_tree(rqlst, trmap(self._cw.config, schema, req.lang), schema)
+        translate_rql_tree(rqlst, trmap(self._cw.config, schema, self._cw.lang),
+                           schema)
         return rqlst.as_string(),
 
 
@@ -184,10 +186,9 @@
     """
     priority = 4
 
-    def preprocess_query(self, uquery, req):
+    def preprocess_query(self, uquery):
         """try to get rql from an unicode query string"""
         args = None
-        self._cw = req
         try:
             # Process as if there was a quoted part
             args = self._quoted_words_query(uquery)
@@ -336,7 +337,7 @@
     priority = 10
     name = 'text'
 
-    def preprocess_query(self, uquery, req):
+    def preprocess_query(self, uquery):
         """suppose it's a plain text query"""
         return 'Any X WHERE X has_text %(text)s', {'text': uquery}
 
@@ -357,7 +358,7 @@
                 self.by_name[processor.name.lower()] = processor
         self.processors = sorted(processors, key=lambda x: x.priority)
 
-    def process_query(self, uquery, req):
+    def process_query(self, uquery):
         assert isinstance(uquery, unicode)
         try:
             procname, query = uquery.split(':', 1)
@@ -368,7 +369,15 @@
             unauthorized = None
             for proc in self.processors:
                 try:
-                    return proc.process_query(uquery, req)
+                    try:
+                        return proc.process_query(uquery)
+                    except TypeError, exc: # cw 3.5 compat
+                        print "EXC", exc
+                        warn("[3.6] %s.%s.process_query() should now accept uquery "
+                             "as unique argument, use self._cw instead of req"
+                             % (proc.__module__, proc.__class__.__name__),
+                             DeprecationWarning)
+                        return proc.process_query(uquery, self._cw)
                 # FIXME : we don't want to catch any exception type here !
                 except (RQLSyntaxError, BadRQLQuery):
                     pass
@@ -381,6 +390,6 @@
             if unauthorized:
                 raise unauthorized
         else:
-            # let exception propagate
-            return proc.process_query(uquery, req)
-        raise BadRQLQuery(req._('sorry, the server is unable to handle this query'))
+            # explicitly specified processor: don't try to catch the exception
+            return proc.process_query(uquery)
+        raise BadRQLQuery(self._cw._('sorry, the server is unable to handle this query'))