Commit 05ae1948 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[entity url] rest path fallback should be the url used as cwuri. Closes #2186039

--HG--
branch : stable
parent 32098d98bf2f
......@@ -635,11 +635,12 @@ class Entity(AppObject):
mainattr, needcheck = self.cw_rest_attr_info()
etype = str(self.e_schema)
path = etype.lower()
fallback = False
if mainattr != 'eid':
value = getattr(self, mainattr)
if not can_use_rest_path(value):
mainattr = 'eid'
path += '/eid'
path = None
elif needcheck:
# make sure url is not ambiguous
try:
......@@ -650,12 +651,16 @@ class Entity(AppObject):
nbresults = self.__unique = self._cw.execute(rql, {'value' : value})[0][0]
if nbresults != 1: # ambiguity?
mainattr = 'eid'
path += '/eid'
path = None
if mainattr == 'eid':
if use_ext_eid:
value = self.cw_metainformation()['extid']
else:
value = self.eid
if path is None:
# fallback url: <base-url>/<eid> url is used as cw entities uri,
# prefer it to <base-url>/<etype>/eid/<eid>
return unicode(value)
return '%s/%s' % (path, self._cw.url_quote(value))
def cw_attr_metadata(self, attr, metadata):
......
......@@ -651,18 +651,18 @@ du :eid:`1:*ReST*`'''
# ambiguity test
person2 = req.create_entity('Personne', prenom=u'remi', nom=u'doe')
person.cw_clear_all_caches()
self.assertEqual(person.rest_path(), 'personne/eid/%s' % person.eid)
self.assertEqual(person2.rest_path(), 'personne/eid/%s' % person2.eid)
self.assertEqual(person.rest_path(), unicode(person.eid))
self.assertEqual(person2.rest_path(), unicode(person2.eid))
# unique attr with None value (wikiid in this case)
card1 = req.create_entity('Card', title=u'hop')
self.assertEqual(card1.rest_path(), 'card/eid/%s' % card1.eid)
self.assertEqual(card1.rest_path(), unicode(card1.eid))
# don't use rest if we have /, ? or & in the path (breaks mod_proxy)
card2 = req.create_entity('Card', title=u'pod', wikiid=u'zo/bi')
self.assertEqual(card2.rest_path(), 'card/eid/%d' % card2.eid)
self.assertEqual(card2.rest_path(), unicode(card2.eid))
card3 = req.create_entity('Card', title=u'pod', wikiid=u'zo&bi')
self.assertEqual(card3.rest_path(), 'card/eid/%d' % card3.eid)
self.assertEqual(card3.rest_path(), unicode(card3.eid))
card4 = req.create_entity('Card', title=u'pod', wikiid=u'zo?bi')
self.assertEqual(card4.rest_path(), 'card/eid/%d' % card4.eid)
self.assertEqual(card4.rest_path(), unicode(card4.eid))
def test_set_attributes(self):
......@@ -701,7 +701,7 @@ du :eid:`1:*ReST*`'''
req = self.request()
card = req.create_entity('Card', wikiid=u'', title=u'test')
self.assertEqual(card.absolute_url(),
'http://testing.fr/cubicweb/card/eid/%s' % card.eid)
'http://testing.fr/cubicweb/%s' % card.eid)
def test_create_entity(self):
req = self.request()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment