[pyramid] Try "eid" first when retrieving a subresource of ETypeResource 3.24
authorDenis Laxalde <denis.laxalde@logilab.fr>
Tue, 15 Nov 2016 11:44:05 +0100
branch3.24
changeset 11820 ec612abc2e2e
parent 11819 a85b7a898e13
child 11821 7534b32c45e3
[pyramid] Try "eid" first when retrieving a subresource of ETypeResource So that Pyramid traversal works as existing CubicWeb URL publishing.
cubicweb/pyramid/resources.py
--- a/cubicweb/pyramid/resources.py	Tue Nov 15 12:20:02 2016 +0100
+++ b/cubicweb/pyramid/resources.py	Tue Nov 15 11:44:05 2016 +0100
@@ -64,8 +64,17 @@
         self.cls = vreg['etypes'].etype_class(self.etype)
 
     def __getitem__(self, value):
-        attrname = self.cls.cw_rest_attr_info()[0]
-        return EntityResource(self.request, self.cls, attrname, value)
+        # Try eid first, then rest attribute as for URL path evaluation
+        # mecanism in cubicweb.web.views.urlpublishing.
+        for attrname in ('eid', self.cls.cw_rest_attr_info()[0]):
+            resource = EntityResource(self.request, self.cls, attrname, value)
+            try:
+                rset = resource.rset
+            except HTTPNotFound:
+                continue
+            if rset.rowcount:
+                return resource
+        raise KeyError(value)
 
     @reify
     def rset(self):