EditRelationMixIn: stop using user_rql_callback
authorJulien Cristau <julien.cristau@logilab.fr>
Fri, 07 Mar 2014 14:03:44 +0100
changeset 9784 790956c311ef
parent 9783 59c582ce68c8
child 9785 8125ba3f4f6d
EditRelationMixIn: stop using user_rql_callback Add generic add_relation/delete_relation ajax functions and use those. Related to #3567793
web/component.py
web/data/cubicweb.js
web/views/ajaxcontroller.py
--- a/web/component.py	Wed May 28 17:57:40 2014 +0200
+++ b/web/component.py	Fri Mar 07 14:03:44 2014 +0100
@@ -1,4 +1,4 @@
-# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -508,25 +508,27 @@
 
 
 class EditRelationMixIn(ReloadableMixIn):
-    def box_item(self, entity, etarget, rql, label):
+
+    def box_item(self, entity, etarget, fname, label):
         """builds HTML link to edit relation between `entity` and `etarget`"""
-        args = {role(self)[0] : entity.eid, target(self)[0] : etarget.eid}
-        url = self._cw.user_rql_callback((rql, args))
+        args = {role(self) : entity.eid, target(self): etarget.eid}
         # for each target, provide a link to edit the relation
-        return u'[<a href="%s" class="action">%s</a>] %s' % (
-            xml_escape(url), label, etarget.view('incontext'))
+        jscall = unicode(js.cw.utils.callAddOrDeleteThenReload(fname,
+                                                               self.rtype,
+                                                               args['subject'],
+                                                               args['object']))
+        return u'[<a href="javascript: %s" class="action">%s</a>] %s' % (
+            xml_escape(jscall), label, etarget.view('incontext'))
 
     def related_boxitems(self, entity):
-        rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype
-        return [self.box_item(entity, etarget, rql, u'-')
+        return [self.box_item(entity, etarget, 'delete_relation', u'-')
                 for etarget in self.related_entities(entity)]
 
     def related_entities(self, entity):
         return entity.related(self.rtype, role(self), entities=True)
 
     def unrelated_boxitems(self, entity):
-        rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % self.rtype
-        return [self.box_item(entity, etarget, rql, u'+')
+        return [self.box_item(entity, etarget, 'add_relation', u'+')
                 for etarget in self.unrelated_entities(entity)]
 
     def unrelated_entities(self, entity):
--- a/web/data/cubicweb.js	Wed May 28 17:57:40 2014 +0200
+++ b/web/data/cubicweb.js	Fri Mar 07 14:03:44 2014 +0100
@@ -100,7 +100,8 @@
             return $node.text();
         }
         return cw.evalJSON(sortvalue);
-    }
+    },
+
 });
 
 
@@ -387,8 +388,16 @@
 		    $.map(cw.utils.sliceList(arguments, 1), JSON.stringify).join(',')
 		    + ')'
 		    );
+    },
+
+    callAddOrDeleteThenReload: function (add_or_delete, rtype, subjeid, objeid) {
+        var d = asyncRemoteExec(add_or_delete, rtype, subjeid, objeid);
+        d.addCallback(function() {
+            window.location.reload();
+        });
     }
 
+
 });
 
 /** DOM factories ************************************************************/
--- a/web/views/ajaxcontroller.py	Wed May 28 17:57:40 2014 +0200
+++ b/web/views/ajaxcontroller.py	Fri Mar 07 14:03:44 2014 +0100
@@ -460,3 +460,15 @@
     """
     cookiename, cookievalue = str(cookiename), str(cookievalue)
     self._cw.set_cookie(cookiename, cookievalue)
+
+
+
+@ajaxfunc
+def delete_relation(self, rtype, subjeid, objeid):
+    rql = 'DELETE S %s O WHERE S eid %%(s)s, O eid %%(o)s' % rtype
+    self._cw.execute(rql, {'s': subjeid, 'o': objeid})
+
+@ajaxfunc
+def add_relation(self, rtype, subjeid, objeid):
+    rql = 'SET S %s O WHERE S eid %%(s)s, O eid %%(o)s' % rtype
+    self._cw.execute(rql, {'s': subjeid, 'o': objeid})