# HG changeset patch # User Denis Laxalde # Date 1460703593 -7200 # Node ID fd177546bbea9fbf34f6b8639fd3f35d68b9d7fe # Parent a9dc97b87cedd0004dc136e64010faec9fc89595 [entity] Add a cw_related_rqlst method returning the RQL select node of cw_related method This is useful if one wants to modify the query before execution. A bit a PEP8 style fix in tests along the way. Related to #12306543. diff -r a9dc97b87ced -r fd177546bbea cubicweb/entity.py --- a/cubicweb/entity.py Fri May 20 17:05:39 2016 +0200 +++ b/cubicweb/entity.py Fri Apr 15 08:59:53 2016 +0200 @@ -1009,6 +1009,10 @@ return rset def cw_related_rql(self, rtype, role='subject', targettypes=None, limit=None): + return self.cw_related_rqlst( + rtype, role=role, targettypes=targettypes, limit=limit).as_string() + + def cw_related_rqlst(self, rtype, role='subject', targettypes=None, limit=None): vreg = self._cw.vreg rschema = vreg.schema[rtype] select = Select() @@ -1065,7 +1069,7 @@ (mdvar,), VariableRef) select.add_restriction(rel) select.add_sort_var(mdvar, asc=False) - return select.as_string() + return select # generic vocabulary methods ############################################## diff -r a9dc97b87ced -r fd177546bbea cubicweb/test/unittest_entity.py --- a/cubicweb/test/unittest_entity.py Fri May 20 17:05:39 2016 +0200 +++ b/cubicweb/test/unittest_entity.py Fri Apr 15 08:59:53 2016 +0200 @@ -290,17 +290,18 @@ Personne.fetch_attrs, Personne.cw_fetch_order = fetch_config(('nom', )) # XXX self.assertEqual(p.cw_related_rql('evaluee'), - 'Any X,AA ORDERBY AA DESC ' - 'WHERE E eid %(x)s, E evaluee X, X modification_date AA') + 'Any X,AA ORDERBY AA DESC ' + 'WHERE E eid %(x)s, E evaluee X, X modification_date AA') tag = self.vreg['etypes'].etype_class('Tag')(req) - self.assertEqual(tag.cw_related_rql('tags', 'subject'), - 'Any X,AA ORDERBY AA DESC ' - 'WHERE E eid %(x)s, E tags X, X modification_date AA') + select = tag.cw_related_rqlst('tags', 'subject') + self.assertEqual(select.as_string(), + 'Any X,AA ORDERBY AA DESC ' + 'WHERE E eid %(x)s, E tags X, X modification_date AA') self.assertEqual(tag.cw_related_rql('tags', 'subject', ('Personne',)), - 'Any X,AA,AB ORDERBY AB ' - 'WHERE E eid %(x)s, E tags X, X is Personne, X modification_date AA, ' - 'X nom AB') + 'Any X,AA,AB ORDERBY AB ' + 'WHERE E eid %(x)s, E tags X, X is Personne, X modification_date AA, ' + 'X nom AB') def test_related_rql_ambiguous_cant_use_fetch_order(self): with self.admin_access.web_request() as req: