author | Julien Cristau <julien.cristau@logilab.fr> |
Fri, 01 Apr 2016 16:50:12 +0200 | |
changeset 11615 | 7e798fe70014 |
parent 11599 | f8ba6ea94af9 |
child 11630 | 1400aee10df4 |
permissions | -rw-r--r-- |
11599
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
1 |
"""Contains resources classes. |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
2 |
""" |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
3 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
4 |
from rql import TypeResolverException |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
5 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
6 |
from pyramid.decorator import reify |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
7 |
from pyramid.httpexceptions import HTTPNotFound |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
8 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
9 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
10 |
class EntityResource(object): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
11 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
12 |
"""A resource class for an entity. It provide method to retrieve an entity |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
13 |
by eid. |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
14 |
""" |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
15 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
16 |
@classmethod |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
17 |
def from_eid(cls): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
18 |
def factory(request): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
19 |
return cls(request, None, None, request.matchdict['eid']) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
20 |
return factory |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
21 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
22 |
def __init__(self, request, cls, attrname, value): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
23 |
self.request = request |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
24 |
self.cls = cls |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
25 |
self.attrname = attrname |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
26 |
self.value = value |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
27 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
28 |
@reify |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
29 |
def rset(self): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
30 |
req = self.request.cw_request |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
31 |
if self.cls is None: |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
32 |
return req.execute('Any X WHERE X eid %(x)s', |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
33 |
{'x': int(self.value)}) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
34 |
st = self.cls.fetch_rqlst(self.request.cw_cnx.user, ordermethod=None) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
35 |
st.add_constant_restriction(st.get_variable('X'), self.attrname, |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
36 |
'x', 'Substitute') |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
37 |
if self.attrname == 'eid': |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
38 |
try: |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
39 |
rset = req.execute(st.as_string(), {'x': int(self.value)}) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
40 |
except (ValueError, TypeResolverException): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
41 |
# conflicting eid/type |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
42 |
raise HTTPNotFound() |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
43 |
else: |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
44 |
rset = req.execute(st.as_string(), {'x': unicode(self.value)}) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
45 |
return rset |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
46 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
47 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
48 |
class ETypeResource(object): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
49 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
50 |
"""A resource for etype. |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
51 |
""" |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
52 |
@classmethod |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
53 |
def from_match(cls, matchname): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
54 |
def factory(request): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
55 |
return cls(request, request.matchdict[matchname]) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
56 |
return factory |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
57 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
58 |
def __init__(self, request, etype): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
59 |
vreg = request.registry['cubicweb.registry'] |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
60 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
61 |
self.request = request |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
62 |
self.etype = vreg.case_insensitive_etypes[etype.lower()] |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
63 |
self.cls = vreg['etypes'].etype_class(self.etype) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
64 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
65 |
def __getitem__(self, value): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
66 |
attrname = self.cls.cw_rest_attr_info()[0] |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
67 |
return EntityResource(self.request, self.cls, attrname, value) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
68 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
69 |
@reify |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
70 |
def rset(self): |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
71 |
rql = self.cls.fetch_rql(self.request.cw_cnx.user) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
72 |
rset = self.request.cw_request.execute(rql) |
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
73 |
return rset |