# HG changeset patch # User Julien Cristau # Date 1394197424 -3600 # Node ID 790956c311eff68c44d96f7f9c1d1920cf2f70e1 # Parent 59c582ce68c88be8371b0268652be52ffa8ac53a EditRelationMixIn: stop using user_rql_callback Add generic add_relation/delete_relation ajax functions and use those. Related to #3567793 diff -r 59c582ce68c8 -r 790956c311ef web/component.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'[%s] %s' % ( - xml_escape(url), label, etarget.view('incontext')) + jscall = unicode(js.cw.utils.callAddOrDeleteThenReload(fname, + self.rtype, + args['subject'], + args['object'])) + return u'[%s] %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): diff -r 59c582ce68c8 -r 790956c311ef web/data/cubicweb.js --- 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 ************************************************************/ diff -r 59c582ce68c8 -r 790956c311ef web/views/ajaxcontroller.py --- 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})