[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 10 Jun 2010 14:16:48 +0200
changeset 5713 605f571198eb
parent 5712 e136d392bd71
child 5714 04a8e48f10bc
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
dbapi.py
web/__init__.py
web/request.py
web/test/unittest_web.py
web/views/calendar.py
--- a/dbapi.py	Thu Jun 10 14:16:47 2010 +0200
+++ b/dbapi.py	Thu Jun 10 14:16:48 2010 +0200
@@ -20,8 +20,8 @@
 Take a look at http://www.python.org/peps/pep-0249.html
 
 (most parts of this document are reported here in docstrings)
+"""
 
-"""
 __docformat__ = "restructuredtext en"
 
 from logging import getLogger
@@ -573,6 +573,7 @@
         """
         from cubicweb.web.request import CubicWebRequestBase as cwrb
         DBAPIRequest.build_ajax_replace_url = cwrb.build_ajax_replace_url.im_func
+        DBAPIRequest.ajax_replace_url = cwrb.ajax_replace_url.im_func
         DBAPIRequest.list_form_param = cwrb.list_form_param.im_func
         DBAPIRequest.property_value = _fake_property_value
         DBAPIRequest.next_tabindex = count().next
--- a/web/__init__.py	Thu Jun 10 14:16:47 2010 +0200
+++ b/web/__init__.py	Thu Jun 10 14:16:48 2010 +0200
@@ -76,7 +76,7 @@
             return json_dumps(repr(value))
     return newfunc
 
-@deprecated('[3.4] use req.build_ajax_replace_url() instead')
+@deprecated('[3.4] use req.ajax_replace_url() instead')
 def ajax_replace_url(nodeid, rql, vid=None, swap=False, **extraparams):
     """builds a replacePageChunk-like url
     >>> ajax_replace_url('foo', 'Person P')
--- a/web/request.py	Thu Jun 10 14:16:47 2010 +0200
+++ b/web/request.py	Thu Jun 10 14:16:48 2010 +0200
@@ -569,24 +569,30 @@
                 cssfile = self.datadir_url + cssfile
             add_css(cssfile, media, *extraargs)
 
+    @deprecated('[3.9] use ajax_replace_url() instead, naming rql and vid arguments')
     def build_ajax_replace_url(self, nodeid, rql, vid, replacemode='replace',
                                **extraparams):
+        return self.ajax_replace_url(nodeid, replacemode, rql=rql, vid=vid,
+                                     **extraparams)
+
+    def ajax_replace_url(self, nodeid, replacemode='replace', **extraparams):
         """builds an ajax url that will replace nodeid's content
 
         :param nodeid: the dom id of the node to replace
-        :param rql: rql to execute
-        :param vid: the view to apply on the resultset
         :param replacemode: defines how the replacement should be done.
 
-        Possible values are :
-        - 'replace' to replace the node's content with the generated HTML
-        - 'swap' to replace the node itself with the generated HTML
-        - 'append' to append the generated HTML to the node's content
+          Possible values are :
+          - 'replace' to replace the node's content with the generated HTML
+          - 'swap' to replace the node itself with the generated HTML
+          - 'append' to append the generated HTML to the node's content
+
+        Arbitrary extra named arguments may be given, they will be included as
+        parameters of the generated url.
         """
-        url = self.build_url('view', rql=rql, vid=vid, __notemplate=1,
-                             **extraparams)
-        return "javascript: loadxhtml('%s', '%s', '%s')" % (
-            nodeid, xml_escape(url), replacemode)
+        extraparams.setdefault('fname', 'view')
+        url = self.build_url('json', **extraparams)
+        return "javascript: $('#%s').loadxhtml(%s, null, 'get', '%s'); noop()" % (
+                nodeid, dumps(url), replacemode)
 
     # urls/path management ####################################################
 
--- a/web/test/unittest_web.py	Thu Jun 10 14:16:47 2010 +0200
+++ b/web/test/unittest_web.py	Thu Jun 10 14:16:48 2010 +0200
@@ -15,21 +15,20 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""
 
-"""
 from logilab.common.testlib import TestCase, unittest_main
 from cubicweb.devtools.fake import FakeRequest
+
 class AjaxReplaceUrlTC(TestCase):
 
     def test_ajax_replace_url(self):
         req = FakeRequest()
-        arurl = req.build_ajax_replace_url
+        arurl = req.ajax_replace_url
         # NOTE: for the simplest use cases, we could use doctest
-        self.assertEquals(arurl('foo', 'Person P', 'list'),
-                          "javascript: loadxhtml('foo', 'http://testing.fr/cubicweb/view?rql=Person%20P&amp;__notemplate=1&amp;vid=list', 'replace')")
-        self.assertEquals(arurl('foo', 'Person P', 'oneline', name='bar', age=12),
-                          '''javascript: loadxhtml('foo', 'http://testing.fr/cubicweb/view?age=12&amp;rql=Person%20P&amp;__notemplate=1&amp;vid=oneline&amp;name=bar', 'replace')''')
+        self.assertEquals(arurl('foo', rql='Person P', vid='list'),
+                          """javascript: $('#foo').loadxhtml("http://testing.fr/cubicweb/json?rql=Person%20P&fname=view&vid=list", null, 'get', 'replace'); noop()""")
+        self.assertEquals(arurl('foo', rql='Person P', vid='oneline', name='bar', age=12),
+                          """javascript: $('#foo').loadxhtml("http://testing.fr/cubicweb/json?name=bar&age=12&rql=Person%20P&fname=view&vid=oneline", null, 'get', 'replace'); noop()""")
 
 
 if __name__ == '__main__':
--- a/web/views/calendar.py	Thu Jun 10 14:16:47 2010 +0200
+++ b/web/views/calendar.py	Thu Jun 10 14:16:48 2010 +0200
@@ -309,12 +309,14 @@
         prevdate = curdate - timedelta(31)
         nextdate = curdate + timedelta(31)
         rql = self.cw_rset.printable_rql()
-        prevlink = self._cw.build_ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
-                                                   year=prevdate.year,
-                                                   month=prevdate.month)
-        nextlink = self._cw.build_ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
-                                                   year=nextdate.year,
-                                                   month=nextdate.month)
+        prevlink = self._cw.ajax_replace_url('onemonthcalid', rql=rql,
+                                             vid='onemonthcal',
+                                             year=prevdate.year,
+                                             month=prevdate.month)
+        nextlink = self._cw.ajax_replace_url('onemonthcalid', rql=rql,
+                                             vid='onemonthcal',
+                                             year=nextdate.year,
+                                             month=nextdate.month)
         return prevlink, nextlink
 
     def _build_calendar_cell(self, celldate, rows, curdate):
@@ -568,10 +570,12 @@
         prevdate = curdate - timedelta(7)
         nextdate = curdate + timedelta(7)
         rql = self.cw_rset.printable_rql()
-        prevlink = self._cw.build_ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
-                                                   year=prevdate.year,
-                                                   week=prevdate.isocalendar()[1])
-        nextlink = self._cw.build_ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
-                                                   year=nextdate.year,
-                                                   week=nextdate.isocalendar()[1])
+        prevlink = self._cw.ajax_replace_url('oneweekcalid', rql=rql,
+                                             vid='oneweekcal',
+                                             year=prevdate.year,
+                                             week=prevdate.isocalendar()[1])
+        nextlink = self._cw.ajax_replace_url('oneweekcalid', rql=rql,
+                                             vid='oneweekcal',
+                                             year=nextdate.year,
+                                             week=nextdate.isocalendar()[1])
         return prevlink, nextlink