Commit c0ad0b0b authored by Laurent Peuch's avatar Laurent Peuch
Browse files

fix: re-introduce cubicweb.pyramid.resources.EntityResource/ETypeResource

branch : 3.28
parent 7107100426f4
......@@ -18,6 +18,11 @@
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <>.
"""Pyramid resources definitions for CubicWeb."""
from rql import TypeResolverException
from pyramid.decorator import reify
from pyramid.httpexceptions import HTTPNotFound
from cubicweb import rdf
......@@ -48,3 +53,77 @@ class RDFResource:
def __init__(self, entity, mime_type):
self.entity = entity
self.mime_type = mime_type
class EntityResource:
"""A resource class for an entity. It provides method to retrieve an entity
by eid.
def from_eid(cls):
def factory(request):
return cls(request, None, None, request.matchdict['eid'])
return factory
def __init__(self, request, cls, attrname, value):
self.request = request
self.cls = cls
self.attrname = attrname
self.value = value
def rset(self):
req = self.request.cw_request
if self.cls is None:
return req.execute('Any X WHERE X eid %(x)s',
{'x': int(self.value)})
st = self.cls.fetch_rqlst(self.request.cw_cnx.user, ordermethod=None)
st.add_constant_restriction(st.get_variable('X'), self.attrname,
'x', 'Substitute')
if self.attrname == 'eid':
rset = req.execute(st.as_string(), {'x': int(self.value)})
except (ValueError, TypeResolverException):
# conflicting eid/type
raise HTTPNotFound()
rset = req.execute(st.as_string(), {'x': self.value})
return rset
class ETypeResource:
"""A resource for etype.
def from_match(cls, matchname):
def factory(request):
return cls(request, request.matchdict[matchname])
return factory
def __init__(self, request, etype):
vreg = request.registry['cubicweb.registry']
self.request = request
self.etype = vreg.case_insensitive_etypes[etype.lower()]
self.cls = vreg['etypes'].etype_class(self.etype)
def __getitem__(self, 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)
rset = resource.rset
except HTTPNotFound:
if rset.rowcount:
return resource
raise KeyError(value)
def rset(self):
rql = self.cls.fetch_rql(self.request.cw_cnx.user)
rset = self.request.cw_request.execute(rql)
return rset
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