author | Christophe de Vienne <christophe@unlish.com> |
Thu, 31 Jul 2014 17:48:32 +0200 | |
changeset 11492 | b0b8942cdb80 |
child 11493 | 00e5cb9771c5 |
permissions | -rw-r--r-- |
11492
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
1 |
from contextlib import contextmanager |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
2 |
from warnings import warn |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
3 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
4 |
import rql |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
5 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
6 |
from cubicweb.web.request import CubicWebRequestBase |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
7 |
from cubicweb import repoapi |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
8 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
9 |
import cubicweb |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
10 |
import cubicweb.web |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
11 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
12 |
from pyramid import security, httpexceptions |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
13 |
from pyramid.httpexceptions import HTTPSeeOther |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
14 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
15 |
from pyramid_cubicweb import authplugin |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
16 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
17 |
import logging |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
18 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
19 |
log = logging.getLogger(__name__) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
20 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
21 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
22 |
@contextmanager |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
23 |
def cw_to_pyramid(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
24 |
"""Wrap a call to the cubicweb API. |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
25 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
26 |
All CW exceptions will be transformed into their pyramid equivalent. |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
27 |
When needed, some CW reponse bits may be converted too (mainly headers)""" |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
28 |
try: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
29 |
yield |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
30 |
except cubicweb.web.Redirect as ex: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
31 |
assert 300 <= ex.status < 400 |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
32 |
raise httpexceptions.status_map[ex.status](ex.location) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
33 |
except cubicweb.web.StatusResponse as ex: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
34 |
warn('[3.16] StatusResponse is deprecated use req.status_out', |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
35 |
DeprecationWarning, stacklevel=2) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
36 |
request.body = ex.content |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
37 |
request.status_int = ex.status |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
38 |
except cubicweb.web.Unauthorized as ex: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
39 |
raise httpexceptions.HTTPForbidden( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
40 |
request.cw_request._( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
41 |
'You\'re not authorized to access this page. ' |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
42 |
'If you think you should, please contact the site ' |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
43 |
'administrator.')) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
44 |
except cubicweb.web.Forbidden: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
45 |
raise httpexceptions.HTTPForbidden( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
46 |
request.cw_request._( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
47 |
'This action is forbidden. ' |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
48 |
'If you think it should be allowed, please contact the site ' |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
49 |
'administrator.')) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
50 |
except (rql.BadRQLQuery, cubicweb.web.RequestError) as ex: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
51 |
raise |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
52 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
53 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
54 |
class CubicWebPyramidRequest(CubicWebRequestBase): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
55 |
def __init__(self, request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
56 |
self._request = request |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
57 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
58 |
self.path = request.upath_info |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
59 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
60 |
vreg = request.registry['cubicweb.appli'].vreg |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
61 |
https = request.scheme == 'https' |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
62 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
63 |
post = request.params |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
64 |
headers_in = request.headers |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
65 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
66 |
super(CubicWebPyramidRequest, self).__init__(vreg, https, post, |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
67 |
headers=headers_in) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
68 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
69 |
def is_secure(self): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
70 |
return self._request.scheme == 'https' |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
71 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
72 |
def relative_path(self, includeparams=True): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
73 |
path = self._request.path[1:] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
74 |
if includeparams and self._request.query_string: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
75 |
return '%s?%s' % (path, self._request.query_string) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
76 |
return path |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
77 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
78 |
def instance_uri(self): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
79 |
return self._request.application_url |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
80 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
81 |
def get_full_path(self): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
82 |
path = self._request.path |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
83 |
if self._request.query_string: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
84 |
return '%s?%s' % (path, self._request.query_string) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
85 |
return path |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
86 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
87 |
def http_method(self): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
88 |
return self._request.method |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
89 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
90 |
def _set_status_out(self, value): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
91 |
self._request.response.status_int = value |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
92 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
93 |
def _get_status_out(self): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
94 |
return self._request.response.status_int |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
95 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
96 |
status_out = property(_get_status_out, _set_status_out) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
97 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
98 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
99 |
def render_view(request, vid, **kwargs): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
100 |
vreg = request.registry['cubicweb.registry'] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
101 |
# XXX The select() function could, know how to handle a pyramid |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
102 |
# request, and feed it directly to the views that supports it. |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
103 |
# On the other hand, we could refine the View concept and decide it works |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
104 |
# with a cnx, and never with a WebRequest |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
105 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
106 |
with cw_to_pyramid(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
107 |
view = vreg['views'].select(vid, request.cw_request, **kwargs) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
108 |
view.set_stream() |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
109 |
view.render() |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
110 |
return view._stream.getvalue() |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
111 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
112 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
113 |
def login(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
114 |
repo = request.registry['cubicweb.repository'] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
115 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
116 |
response = request.response |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
117 |
user_eid = None |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
118 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
119 |
if '__login' in request.params: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
120 |
login = request.params['__login'] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
121 |
password = request.params['__password'] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
122 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
123 |
try: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
124 |
with repo.internal_cnx() as cnx: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
125 |
user = repo.authenticate_user(cnx, login, password=password) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
126 |
user_eid = user.eid |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
127 |
except cubicweb.AuthenticationError: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
128 |
raise |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
129 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
130 |
if user_eid is not None: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
131 |
headers = security.remember(request, user_eid) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
132 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
133 |
raise HTTPSeeOther( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
134 |
request.params.get('postlogin_path', '/'), |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
135 |
headers=headers) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
136 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
137 |
response.headerlist.extend(headers) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
138 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
139 |
response.text = render_view(request, 'login') |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
140 |
return response |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
141 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
142 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
143 |
def _cw_cnx(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
144 |
cnx = repoapi.ClientConnection(request.cw_session) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
145 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
146 |
def cleanup(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
147 |
if request.exception is not None: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
148 |
cnx.rollback() |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
149 |
else: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
150 |
cnx.commit() |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
151 |
cnx.__exit__(None, None, None) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
152 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
153 |
request.add_finished_callback(cleanup) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
154 |
cnx.__enter__() |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
155 |
return cnx |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
156 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
157 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
158 |
def _cw_close_session(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
159 |
request.cw_session.close() |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
160 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
161 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
162 |
def _cw_session(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
163 |
"""Obtains a cw session from a pyramid request""" |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
164 |
repo = request.registry['cubicweb.repository'] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
165 |
config = request.registry['cubicweb.config'] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
166 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
167 |
if not request.authenticated_userid: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
168 |
login, password = config.anonymous_user() |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
169 |
sessionid = repo.connect(login, password=password) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
170 |
session = repo._sessions[sessionid] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
171 |
request.add_finished_callback(_cw_close_session) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
172 |
else: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
173 |
session = request._cw_cached_session |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
174 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
175 |
# XXX Ideally we store the cw session data in the pyramid session. |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
176 |
# BUT some data in the cw session data dictionnary makes pyramid fail. |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
177 |
session.data = request.session |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
178 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
179 |
return session |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
180 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
181 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
182 |
def _cw_request(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
183 |
req = CubicWebPyramidRequest(request) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
184 |
req.set_cnx(request.cw_cnx) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
185 |
return req |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
186 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
187 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
188 |
def get_principals(login, request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
189 |
repo = request.registry['cubicweb.repository'] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
190 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
191 |
try: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
192 |
sessionid = repo.connect( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
193 |
str(login), __pyramid_directauth=authplugin.EXT_TOKEN) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
194 |
session = repo._sessions[sessionid] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
195 |
request._cw_cached_session = session |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
196 |
request.add_finished_callback(_cw_close_session) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
197 |
except: |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
198 |
log.exception("Failed") |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
199 |
raise |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
200 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
201 |
return session.user.groups |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
202 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
203 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
204 |
def hello_world(request): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
205 |
request.response.text = \ |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
206 |
u"<html><body>Hello %s</body></html>" % request.cw_cnx.user.login |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
207 |
return request.response |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
208 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
209 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
210 |
def includeme(config): |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
211 |
repo = config.registry['cubicweb.repository'] |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
212 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
213 |
repo.system_source.add_authentifier(authplugin.DirectAuthentifier()) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
214 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
215 |
config.add_request_method( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
216 |
_cw_session, name='cw_session', property=True, reify=True) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
217 |
config.add_request_method( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
218 |
_cw_cnx, name='cw_cnx', property=True, reify=True) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
219 |
config.add_request_method( |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
220 |
_cw_request, name='cw_request', property=True, reify=True) |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
221 |
|
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
222 |
config.add_route('login', '/login') |
b0b8942cdb80
Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff
changeset
|
223 |
config.add_view(login, route_name='login') |