author | Rabah Meradi <rabah.meradi@logilab.fr> |
Thu, 27 Aug 2015 11:25:42 +0200 | |
changeset 11597 | 286611d1d7a4 |
parent 11596 | pyramid_cubicweb/entities.py@1f2570f1d72a |
child 11598 | 2dc0b0db2329 |
permissions | -rw-r--r-- |
11596
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
1 |
from __future__ import absolute_import |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
2 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
3 |
from rql import TypeResolverException |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
4 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
5 |
from pyramid.decorator import reify |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
6 |
from pyramid.httpexceptions import HTTPNotFound |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
7 |
from pyramid.view import view_config |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
8 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
9 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
10 |
class EntityResource(object): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
11 |
def __init__(self, request, cls, attrname, value): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
12 |
self.request = request |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
13 |
self.cls = cls |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
14 |
self.attrname = attrname |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
15 |
self.value = value |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
16 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
17 |
@reify |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
18 |
def rset(self): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
19 |
st = self.cls.fetch_rqlst(self.request.cw_cnx.user, ordermethod=None) |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
20 |
st.add_constant_restriction(st.get_variable('X'), self.attrname, |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
21 |
'x', 'Substitute') |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
22 |
if self.attrname == 'eid': |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
23 |
try: |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
24 |
rset = self.request.cw_cnx.execute( |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
25 |
st.as_string(), {'x': int(self.value)}) |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
26 |
except (ValueError, TypeResolverException): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
27 |
# conflicting eid/type |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
28 |
raise HTTPNotFound() |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
29 |
else: |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
30 |
rset = self.request.cw_cnx.execute( |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
31 |
st.as_string(), {'x': unicode(self.value)}) |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
32 |
return rset |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
33 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
34 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
35 |
class ETypeResource(object): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
36 |
@classmethod |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
37 |
def from_match(cls, matchname): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
38 |
def factory(request): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
39 |
return cls(request, request.matchdict[matchname]) |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
40 |
return factory |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
41 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
42 |
def __init__(self, request, etype): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
43 |
vreg = request.registry['cubicweb.registry'] |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
44 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
45 |
self.request = request |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
46 |
self.etype = vreg.case_insensitive_etypes[etype.lower()] |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
47 |
self.cls = vreg['etypes'].etype_class(self.etype) |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
48 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
49 |
def __getitem__(self, value): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
50 |
attrname = self.cls.cw_rest_attr_info()[0] |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
51 |
return EntityResource(self.request, self.cls, attrname, value) |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
52 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
53 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
54 |
class MatchIsETypePredicate(object): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
55 |
def __init__(self, matchname, config): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
56 |
self.matchname = matchname |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
57 |
self.etypes = frozenset( |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
58 |
k.lower() for k in config.registry['cubicweb.registry']['etypes']) |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
59 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
60 |
def text(self): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
61 |
return 'match_is_etype = %s' % self.matchname |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
62 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
63 |
phash = text |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
64 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
65 |
def __call__(self, info, request): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
66 |
return info['match'][self.matchname].lower() in \ |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
67 |
request.registry['cubicweb.registry'].case_insensitive_etypes |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
68 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
69 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
70 |
@view_config( |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
71 |
route_name='cwentities', |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
72 |
context=EntityResource, |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
73 |
request_method='DELETE') |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
74 |
def delete_entity(context, request): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
75 |
context.rset.one().cw_delete() |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
76 |
request.response.status_int = 204 |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
77 |
return request.response |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
78 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
79 |
|
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
80 |
def includeme(config): |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
81 |
config.add_route_predicate('match_is_etype', MatchIsETypePredicate) |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
82 |
config.add_route( |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
83 |
'cwentities', '/{etype}/*traverse', |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
84 |
factory=ETypeResource.from_match('etype'), match_is_etype='etype') |
1f2570f1d72a
[routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
85 |
config.scan(__name__) |