forked from morepath/morepath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.py
More file actions
323 lines (249 loc) · 11.1 KB
/
Copy pathrequest.py
File metadata and controls
323 lines (249 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""Morepath request implementation.
Entirely documented in :class:`morepath.Request` and
:class:`morepath.Response` in the public API.
"""
from webob import BaseRequest, Response as BaseResponse
import reg
from .reify import reify
from .traject import create_path, parse_path
from .error import LinkError
from .authentication import NO_IDENTITY
SAME_APP = reg.Sentinel('SAME_APP')
class Request(BaseRequest):
"""Request.
Extends :class:`webob.request.BaseRequest`
"""
def __init__(self, environ, app, **kw):
super(Request, self).__init__(environ, **kw)
# parse path, normalizing dots away in
# in case the client didn't do the normalization
segments = parse_path(self.path_info)
# Webob updates the environ as well
self.path_info = create_path(segments)
# reverse to get unconsumed
segments.reverse()
self.unconsumed = segments
"""Stack of path segments that have not yet been consumed.
See :mod:`morepath.publish`.
"""
self._root_app = app
self.app = app
""":class:`morepath.App` instance currently handling request.
"""
self._after = []
self._link_prefix_cache = {}
def reset(self):
"""Reset request.
This resets the request back to the state it had when request
processing started. This is used by ``more.transaction`` when it
retries a transaction.
"""
self.make_body_seekable()
segments = parse_path(self.path_info)
segments.reverse()
self.unconsumed = segments
self.app = self._root_app
self._after = []
@reify
def body_obj(self):
"""JSON object, converted to an object.
You can use the :meth:`App.load_json` directive to specify
how to transform JSON to a Python object. By default, no
conversion takes place, and ``body_obj`` is identical to
the ``json`` attribute.
"""
if not self.body:
return None
if self.content_type != 'application/json':
return None
return self.app._load_json(self.json, self)
@reify
def identity(self):
"""Self-proclaimed identity of the user.
The identity is established using the identity policy. Normally
this would be an instance of :class:`morepath.Identity`.
If no identity is claimed or established, or if the identity
is not verified by the application, the identity is the the
special value :attr:`morepath.NO_IDENTITY`.
The identity can be used for authentication/authorization of
the user, using Morepath permission directives.
"""
result = self.app._identify(self)
if result is None or result is NO_IDENTITY:
return NO_IDENTITY
if not self.app._verify_identity(result):
return NO_IDENTITY
return result
def link_prefix(self):
"""Prefix to all links created by this request."""
cached = self._link_prefix_cache.get(self.app.__class__)
if cached is not None:
return cached
prefix = self._link_prefix_cache[self.app.__class__]\
= self.app._link_prefix(self)
return prefix
def view(self, obj, default=None, app=SAME_APP, **predicates):
"""Call view for model instance.
This does not render the view, but calls the appropriate
view function and returns its result.
:param obj: the model instance to call the view on.
:param default: default value if view is not found.
:param app: If set, change the application in which to look up
the view. By default the view is looked up for the current
application. The ``defer_links`` directive can be used to change
the default app for all instances of a particular class.
:param predicates: extra predicates to modify view
lookup, such as ``name`` and ``request_method``. The default
``name`` is empty, so the default view is looked up,
and the default ``request_method`` is ``GET``. If you introduce
your own predicates you can specify your own default.
"""
if app is None:
raise LinkError("Cannot view: app is None")
if app is SAME_APP:
app = self.app
predicates['model'] = obj.__class__
def find(app, obj):
return app.get_view.component_by_keys(**predicates)
view, app = app._follow_defers(find, obj)
if view is None:
return default
old_app = self.app
self.app = app
# need to use value as view is registered as a function, not
# as a wrapped method
result = view.func(obj, self)
self.app = old_app
return result
def link(self, obj, name='', default=None, app=SAME_APP):
"""Create a link (URL) to a view on a model instance.
The resulting link is prefixed by the link prefix. By default
this is the full URL based on the Host header.
You can configure the link prefix for an application using the
:meth:`morepath.App.link_prefix` directive.
If no link can be constructed for the model instance, a
:exc:`morepath.error.LinkError` is raised. ``None`` is treated
specially: if ``None`` is passed in the default value is
returned.
The :meth:`morepath.App.defer_links` or
:meth:`morepath.App.defer_class_links` directives can be used
to defer link generation for all instances of a particular
class (if this app doesn't handle them) to another app.
:param obj: the model instance to link to, or ``None``.
:param name: the name of the view to link to. If omitted, the
the default view is looked up.
:param default: if ``None`` is passed in, the default value is
returned. By default this is ``None``.
:param app: If set, change the application to which the
link is made. By default the link is made to an object
in the current application.
"""
if obj is None:
return default
if app is None:
raise LinkError("Cannot link: app is None")
if app is SAME_APP:
app = self.app
info = app._get_deferred_mounted_path(obj)
if info is None:
raise LinkError("Cannot link to: %r" % obj)
return info.url(self.link_prefix(), name)
def class_link(self, model, variables=None, name='', app=SAME_APP):
"""Create a link (URL) to a view on a class.
Given a model class and a variables dictionary, create a link
based on the path registered for the class and interpolate the
variables.
If you have an instance of the model available you'd link to the
model instead, but in some cases it is expensive to instantiate
the model just to create a link. In this case `class_link` can be
used as an optimization.
The :meth:`morepath.App.defer_class_links` directive can be
used to defer link generation for a particular class (if this
app doesn't handle them) to another app.
Note that the :meth:`morepath.App.defer_links` directive has
**no** effect on ``class_link``, as it needs an instance of the
model to work, which is not available.
If no link can be constructed for the model class, a
:exc:`morepath.error.LinkError` is raised. This error is
also raised if you don't supply enough variables. Additional
variables not used in the path are interpreted as URL
parameters.
:param model: the model class to link to.
:param variables: a dictionary with as keys the variable names,
and as values the variable values. These are used to construct
the link URL. If omitted, the dictionary is treated as containing
no variables.
:param name: the name of the view to link to. If omitted, the
the default view is looked up.
:param app: If set, change the application to which the
link is made. By default the link is made to an object
in the current application.
"""
if variables is None:
variables = {}
if app is None:
raise LinkError("Cannot link: app is None")
if app is SAME_APP:
app = self.app
info = app._get_deferred_mounted_class_path(model, variables)
if info is None:
raise LinkError("Cannot link to class: %r" % model)
return info.url(self.link_prefix(), name)
def resolve_path(self, path, app=SAME_APP):
"""Resolve a path to a model instance.
The resulting object is a model instance, or ``None`` if the
path could not be resolved.
:param path: URL path to resolve.
:param app: If set, change the application in which the
path is resolved. By default the path is resolved in the
current application.
:return: instance or ``None`` if no path could be resolved.
"""
if app is None:
raise LinkError("Cannot path: app is None")
if app is SAME_APP:
app = self.app
request = Request(self.environ.copy(), app, path_info=path)
# try to resolve imports..
from .publish import resolve_model
return resolve_model(request)
def after(self, func):
"""Call a function with the response after a successful request.
A request is considered *successful* if the HTTP status is a 2XX or a
3XX code (e.g. 200 OK, 204 No Content, 302 Found).
In this case ``after`` *is* called.
A request is considered *unsuccessful* if the HTTP status lies outside
the 2XX-3XX range (e.g. 403 Forbidden, 404 Not Found,
500 Internal Server Error). Usually this happens if an exception
occurs. In this case ``after`` is *not* called.
Some exceptions indicate a successful request however and their
occurrence still leads to a call to ``after``. These exceptions
inherit from either :class:`webob.exc.HTTPOk` or
:class:`webob.exc.HTTPRedirection`.
You use `request.after` inside a view function definition.
It can be used explicitly::
@App.view(model=SomeModel)
def some_model_default(self, request):
def myfunc(response):
response.headers.add('blah', 'something')
request.after(my_func)
or as a decorator::
@App.view(model=SomeModel)
def some_model_default(self, request):
@request.after
def myfunc(response):
response.headers.add('blah', 'something')
:param func: callable that is called with response
:return: func argument, not wrapped
"""
self._after.append(func)
return func
def run_after(self, response):
for after in self._after:
after(response)
def clear_after(self):
self._after = []
class Response(BaseResponse):
"""Response.
Extends :class:`webob.response.Response`.
"""