kopia lustrzana https://gitlab.com/marnanel/chapeau
As a special case, view_for_mimetype returns the view list if the request param is None.
Tests updated.merge-requests/2/merge
rodzic
e0517f6688
commit
7b37822d8d
|
@ -112,3 +112,31 @@ class Tests(TestCase):
|
|||
vfm(DummyRequest('image/jpeg'), '123'),
|
||||
'df123',
|
||||
)
|
||||
|
||||
def test_passing_None(self):
|
||||
|
||||
def th():
|
||||
pass
|
||||
|
||||
def aj():
|
||||
pass
|
||||
|
||||
def df():
|
||||
pass
|
||||
|
||||
vfm = view_for_mimetype(
|
||||
[
|
||||
('text', 'html', th),
|
||||
('application', 'json', aj),
|
||||
],
|
||||
default = df,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
vfm(None),
|
||||
[
|
||||
('text', 'html', th),
|
||||
('application', 'json', aj),
|
||||
(None, None, df),
|
||||
],
|
||||
)
|
||||
|
|
|
@ -31,6 +31,9 @@ def view_for_mimetype(
|
|||
If you don't give a default, the view function returns
|
||||
a new HttpResponse with status_code==406, i.e. "Not Acceptable".
|
||||
See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406
|
||||
|
||||
As a special case, if request is None, returns the list of views.
|
||||
A default value will be at the end, with type and subtype set to None.
|
||||
"""
|
||||
|
||||
def _view_for_mimetype_inner(
|
||||
|
@ -38,6 +41,14 @@ def view_for_mimetype(
|
|||
*args, **kwargs,
|
||||
):
|
||||
|
||||
if request is None:
|
||||
result = views.copy()
|
||||
|
||||
if default is not None:
|
||||
result.append((None, None, default))
|
||||
|
||||
return result
|
||||
|
||||
accept_header = request.headers.get('Accept', '')
|
||||
accept_parsed = parse_accept_header(accept_header)
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue