[entity url] rest path fallback should be the url used as cwuri. Closes #2186039 stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 13 Feb 2012 17:44:44 +0100
branchstable
changeset 8286 8b0146e31baa
parent 8285 32098d98bf2f
child 8287 2d660890d3e8
[entity url] rest path fallback should be the url used as cwuri. Closes #2186039
entity.py
test/unittest_entity.py
--- a/entity.py	Thu Mar 01 13:39:43 2012 +0100
+++ b/entity.py	Mon Feb 13 17:44:44 2012 +0100
@@ -635,11 +635,12 @@
         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 @@
                     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):
--- a/test/unittest_entity.py	Thu Mar 01 13:39:43 2012 +0100
+++ b/test/unittest_entity.py	Mon Feb 13 17:44:44 2012 +0100
@@ -651,18 +651,18 @@
         # 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 @@
         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()