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

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changeset8b0146e31baa
branchstable
phasepublic
hiddenno
parent revision#32098d98bf2f [migration] fix add_relation_type in case it already exists, broken by 67660d5f1916
child revision#2d660890d3e8 [js] translate calendar in french (closes #2167866)
files modified by this revision
entity.py
test/unittest_entity.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1329151484 -3600
# Mon Feb 13 17:44:44 2012 +0100
# Branch stable
# Node ID 8b0146e31baaa7650c3c49b666c8c39226c029ca
# Parent 32098d98bf2f7f912e8a23c7dc23a3c805a0f03b
[entity url] rest path fallback should be the url used as cwuri. Closes #2186039

diff --git a/entity.py b/entity.py
@@ -633,31 +633,36 @@
1      def rest_path(self, use_ext_eid=False): # XXX cw_rest_path
2          """returns a REST-like (relative) path for this entity"""
3          mainattr, needcheck = self.cw_rest_attr_info()
4          etype = str(self.e_schema)
5          path = etype.lower()
6 +        fallback = False
7          if mainattr != 'eid':
8              value = getattr(self, mainattr)
9              if not can_use_rest_path(value):
10                  mainattr = 'eid'
11 -                path += '/eid'
12 +                path = None
13              elif needcheck:
14                  # make sure url is not ambiguous
15                  try:
16                      nbresults = self.__unique
17                  except AttributeError:
18                      rql = 'Any COUNT(X) WHERE X is %s, X %s %%(value)s' % (
19                          etype, mainattr)
20                      nbresults = self.__unique = self._cw.execute(rql, {'value' : value})[0][0]
21                  if nbresults != 1: # ambiguity?
22                      mainattr = 'eid'
23 -                    path += '/eid'
24 +                    path = None
25          if mainattr == 'eid':
26              if use_ext_eid:
27                  value = self.cw_metainformation()['extid']
28              else:
29                  value = self.eid
30 +        if path is None:
31 +            # fallback url: <base-url>/<eid> url is used as cw entities uri,
32 +            # prefer it to <base-url>/<etype>/eid/<eid>
33 +            return unicode(value)
34          return '%s/%s' % (path, self._cw.url_quote(value))
35 
36      def cw_attr_metadata(self, attr, metadata):
37          """return a metadata for an attribute (None if unspecified)"""
38          value = getattr(self, '%s_%s' % (attr, metadata), None)
diff --git a/test/unittest_entity.py b/test/unittest_entity.py
@@ -649,22 +649,22 @@
39          person = req.create_entity('Personne', prenom=u'john', nom=u'doe')
40          self.assertEqual(person.rest_path(), 'personne/doe')
41          # ambiguity test
42          person2 = req.create_entity('Personne', prenom=u'remi', nom=u'doe')
43          person.cw_clear_all_caches()
44 -        self.assertEqual(person.rest_path(), 'personne/eid/%s' % person.eid)
45 -        self.assertEqual(person2.rest_path(), 'personne/eid/%s' % person2.eid)
46 +        self.assertEqual(person.rest_path(), unicode(person.eid))
47 +        self.assertEqual(person2.rest_path(), unicode(person2.eid))
48          # unique attr with None value (wikiid in this case)
49          card1 = req.create_entity('Card', title=u'hop')
50 -        self.assertEqual(card1.rest_path(), 'card/eid/%s' % card1.eid)
51 +        self.assertEqual(card1.rest_path(), unicode(card1.eid))
52          # don't use rest if we have /, ? or & in the path (breaks mod_proxy)
53          card2 = req.create_entity('Card', title=u'pod', wikiid=u'zo/bi')
54 -        self.assertEqual(card2.rest_path(), 'card/eid/%d' % card2.eid)
55 +        self.assertEqual(card2.rest_path(), unicode(card2.eid))
56          card3 = req.create_entity('Card', title=u'pod', wikiid=u'zo&bi')
57 -        self.assertEqual(card3.rest_path(), 'card/eid/%d' % card3.eid)
58 +        self.assertEqual(card3.rest_path(), unicode(card3.eid))
59          card4 = req.create_entity('Card', title=u'pod', wikiid=u'zo?bi')
60 -        self.assertEqual(card4.rest_path(), 'card/eid/%d' % card4.eid)
61 +        self.assertEqual(card4.rest_path(), unicode(card4.eid))
62 
63 
64      def test_set_attributes(self):
65          req = self.request()
66          person = req.create_entity('Personne', nom=u'di mascio', prenom=u'adrien')
@@ -699,11 +699,11 @@
67 
68      def test_absolute_url_empty_field(self):
69          req = self.request()
70          card = req.create_entity('Card', wikiid=u'', title=u'test')
71          self.assertEqual(card.absolute_url(),
72 -                          'http://testing.fr/cubicweb/card/eid/%s' % card.eid)
73 +                          'http://testing.fr/cubicweb/%s' % card.eid)
74 
75      def test_create_entity(self):
76          req = self.request()
77          p1 = req.create_entity('Personne', nom=u'fayolle', prenom=u'alexandre')
78          p2 = req.create_entity('Personne', nom=u'campeas', prenom=u'aurelien')