[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>
changeset605f571198eb
branchdefault
phasepublic
hiddenno
parent revision#e136d392bd71 [form] refactor meta-attributes handling: hide them by default using uicfg.afs instead of returning None (or not, according to skip_meta_attr flag argument) in guess_field
child revision#04a8e48f10bc [idownloadable] refactor IDownloadable primary view to make overriding easier
files modified by this revision
dbapi.py
web/__init__.py
web/request.py
web/test/unittest_web.py
web/views/calendar.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1276172208 -7200
# Thu Jun 10 14:16:48 2010 +0200
# Node ID 605f571198eb175a02fb465839a133a6c2a78626
# Parent e136d392bd71f7cf3b6ac3b4ed7170d6d13a2112
[web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)

diff --git a/dbapi.py b/dbapi.py
@@ -18,12 +18,12 @@
1  """DB-API 2.0 compliant module
2 
3  Take a look at http://www.python.org/peps/pep-0249.html
4 
5  (most parts of this document are reported here in docstrings)
6 +"""
7 
8 -"""
9  __docformat__ = "restructuredtext en"
10 
11  from logging import getLogger
12  from time import time, clock
13  from itertools import count
@@ -571,10 +571,11 @@
14 
15          You should call `load_appobjects` at some point to register those views.
16          """
17          from cubicweb.web.request import CubicWebRequestBase as cwrb
18          DBAPIRequest.build_ajax_replace_url = cwrb.build_ajax_replace_url.im_func
19 +        DBAPIRequest.ajax_replace_url = cwrb.ajax_replace_url.im_func
20          DBAPIRequest.list_form_param = cwrb.list_form_param.im_func
21          DBAPIRequest.property_value = _fake_property_value
22          DBAPIRequest.next_tabindex = count().next
23          DBAPIRequest.form = {}
24          DBAPIRequest.data = {}
diff --git a/web/__init__.py b/web/__init__.py
@@ -74,11 +74,11 @@
25              return json_dumps(value)
26          except TypeError:
27              return json_dumps(repr(value))
28      return newfunc
29 
30 -@deprecated('[3.4] use req.build_ajax_replace_url() instead')
31 +@deprecated('[3.4] use req.ajax_replace_url() instead')
32  def ajax_replace_url(nodeid, rql, vid=None, swap=False, **extraparams):
33      """builds a replacePageChunk-like url
34      >>> ajax_replace_url('foo', 'Person P')
35      "javascript: replacePageChunk('foo', 'Person%20P');"
36      >>> ajax_replace_url('foo', 'Person P', 'oneline')
diff --git a/web/request.py b/web/request.py
@@ -567,28 +567,34 @@
37          for cssfile in cssfiles:
38              if localfile:
39                  cssfile = self.datadir_url + cssfile
40              add_css(cssfile, media, *extraargs)
41 
42 +    @deprecated('[3.9] use ajax_replace_url() instead, naming rql and vid arguments')
43      def build_ajax_replace_url(self, nodeid, rql, vid, replacemode='replace',
44                                 **extraparams):
45 +        return self.ajax_replace_url(nodeid, replacemode, rql=rql, vid=vid,
46 +                                     **extraparams)
47 +
48 +    def ajax_replace_url(self, nodeid, replacemode='replace', **extraparams):
49          """builds an ajax url that will replace nodeid's content
50 
51          :param nodeid: the dom id of the node to replace
52 -        :param rql: rql to execute
53 -        :param vid: the view to apply on the resultset
54          :param replacemode: defines how the replacement should be done.
55 
56 -        Possible values are :
57 -        - 'replace' to replace the node's content with the generated HTML
58 -        - 'swap' to replace the node itself with the generated HTML
59 -        - 'append' to append the generated HTML to the node's content
60 +          Possible values are :
61 +          - 'replace' to replace the node's content with the generated HTML
62 +          - 'swap' to replace the node itself with the generated HTML
63 +          - 'append' to append the generated HTML to the node's content
64 +
65 +        Arbitrary extra named arguments may be given, they will be included as
66 +        parameters of the generated url.
67          """
68 -        url = self.build_url('view', rql=rql, vid=vid, __notemplate=1,
69 -                             **extraparams)
70 -        return "javascript: loadxhtml('%s', '%s', '%s')" % (
71 -            nodeid, xml_escape(url), replacemode)
72 +        extraparams.setdefault('fname', 'view')
73 +        url = self.build_url('json', **extraparams)
74 +        return "javascript: $('#%s').loadxhtml(%s, null, 'get', '%s'); noop()" % (
75 +                nodeid, dumps(url), replacemode)
76 
77      # urls/path management ####################################################
78 
79      def url(self, includeparams=True):
80          """return currently accessed url"""
diff --git a/web/test/unittest_web.py b/web/test/unittest_web.py
@@ -13,24 +13,23 @@
81  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
82  # details.
83  #
84  # You should have received a copy of the GNU Lesser General Public License along
85  # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
86 -"""
87 
88 -"""
89  from logilab.common.testlib import TestCase, unittest_main
90  from cubicweb.devtools.fake import FakeRequest
91 +
92  class AjaxReplaceUrlTC(TestCase):
93 
94      def test_ajax_replace_url(self):
95          req = FakeRequest()
96 -        arurl = req.build_ajax_replace_url
97 +        arurl = req.ajax_replace_url
98          # NOTE: for the simplest use cases, we could use doctest
99 -        self.assertEquals(arurl('foo', 'Person P', 'list'),
100 -                          "javascript: loadxhtml('foo', 'http://testing.fr/cubicweb/view?rql=Person%20P&amp;__notemplate=1&amp;vid=list', 'replace')")
101 -        self.assertEquals(arurl('foo', 'Person P', 'oneline', name='bar', age=12),
102 -                          '''javascript: loadxhtml('foo', 'http://testing.fr/cubicweb/view?age=12&amp;rql=Person%20P&amp;__notemplate=1&amp;vid=oneline&amp;name=bar', 'replace')''')
103 +        self.assertEquals(arurl('foo', rql='Person P', vid='list'),
104 +                          """javascript: $('#foo').loadxhtml("http://testing.fr/cubicweb/json?rql=Person%20P&fname=view&vid=list", null, 'get', 'replace'); noop()""")
105 +        self.assertEquals(arurl('foo', rql='Person P', vid='oneline', name='bar', age=12),
106 +                          """javascript: $('#foo').loadxhtml("http://testing.fr/cubicweb/json?name=bar&age=12&rql=Person%20P&fname=view&vid=oneline", null, 'get', 'replace'); noop()""")
107 
108 
109  if __name__ == '__main__':
110      unittest_main()
diff --git a/web/views/calendar.py b/web/views/calendar.py
@@ -307,16 +307,18 @@
111 
112      def _prevnext_links(self, curdate):
113          prevdate = curdate - timedelta(31)
114          nextdate = curdate + timedelta(31)
115          rql = self.cw_rset.printable_rql()
116 -        prevlink = self._cw.build_ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
117 -                                                   year=prevdate.year,
118 -                                                   month=prevdate.month)
119 -        nextlink = self._cw.build_ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
120 -                                                   year=nextdate.year,
121 -                                                   month=nextdate.month)
122 +        prevlink = self._cw.ajax_replace_url('onemonthcalid', rql=rql,
123 +                                             vid='onemonthcal',
124 +                                             year=prevdate.year,
125 +                                             month=prevdate.month)
126 +        nextlink = self._cw.ajax_replace_url('onemonthcalid', rql=rql,
127 +                                             vid='onemonthcal',
128 +                                             year=nextdate.year,
129 +                                             month=nextdate.month)
130          return prevlink, nextlink
131 
132      def _build_calendar_cell(self, celldate, rows, curdate):
133          curmonth = curdate.month
134          classes = ""
@@ -566,12 +568,14 @@
135 
136      def _prevnext_links(self, curdate):
137          prevdate = curdate - timedelta(7)
138          nextdate = curdate + timedelta(7)
139          rql = self.cw_rset.printable_rql()
140 -        prevlink = self._cw.build_ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
141 -                                                   year=prevdate.year,
142 -                                                   week=prevdate.isocalendar()[1])
143 -        nextlink = self._cw.build_ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
144 -                                                   year=nextdate.year,
145 -                                                   week=nextdate.isocalendar()[1])
146 +        prevlink = self._cw.ajax_replace_url('oneweekcalid', rql=rql,
147 +                                             vid='oneweekcal',
148 +                                             year=prevdate.year,
149 +                                             week=prevdate.isocalendar()[1])
150 +        nextlink = self._cw.ajax_replace_url('oneweekcalid', rql=rql,
151 +                                             vid='oneweekcal',
152 +                                             year=nextdate.year,
153 +                                             week=nextdate.isocalendar()[1])
154          return prevlink, nextlink