# HG changeset patch # User Nicolas Chauvat # Date 1304584703 -7200 # Node ID ff8d6ae076c476406dcc954d8155c5dc3aafa3bd # Parent a650e6267e978c6fab0a802749f94952a3ddf46c [entity] make set_relations() accept entities and eids (closes #1631391) diff -r a650e6267e97 -r ff8d6ae076c4 entity.py --- a/entity.py Thu May 05 11:54:41 2011 +0200 +++ b/entity.py Thu May 05 10:38:23 2011 +0200 @@ -958,8 +958,9 @@ """add relations to the given object. To set a relation where this entity is the object of the relation, use 'reverse_' as argument name. - Values may be an entity, a list of entities, or None (meaning that all - relations of the given type from or to this object should be deleted). + Values may be an entity or eid, a list of entities or eids, or None + (meaning that all relations of the given type from or to this object + should be deleted). """ # XXX update cache _check_cw_unsafe(kwargs) @@ -974,9 +975,17 @@ continue if not isinstance(values, (tuple, list, set, frozenset)): values = (values,) + eids = [] + for val in values: + try: + eids.append(str(val.eid)) + except AttributeError: + try: + eids.append(str(typed_eid(val))) + except (ValueError, TypeError): + raise Exception('expected an Entity or eid, got %s' % val) self._cw.execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % ( - restr, ','.join(str(r.eid) for r in values)), - {'x': self.eid}) + restr, ','.join(eids)), {'x': self.eid}) def cw_delete(self, **kwargs): assert self.has_eid(), self.eid diff -r a650e6267e97 -r ff8d6ae076c4 test/unittest_entity.py --- a/test/unittest_entity.py Thu May 05 11:54:41 2011 +0200 +++ b/test/unittest_entity.py Thu May 05 10:38:23 2011 +0200 @@ -564,6 +564,15 @@ self.assertEqual(person.prenom, u'sylvain') self.assertEqual(person.nom, u'thénault') + def test_set_relations(self): + req = self.request() + person = req.create_entity('Personne', nom=u'chauvat', prenom=u'nicolas') + note = req.create_entity('Note', type=u'x') + note.set_relations(ecrit_par=person) + note = req.create_entity('Note', type=u'y') + note.set_relations(ecrit_par=person.eid) + self.assertEqual(len(person.reverse_ecrit_par), 2) + def test_metainformation_and_external_absolute_url(self): req = self.request() note = req.create_entity('Note', type=u'z')