As a special case, view_for_mimetype returns the view list if the request param is None.

Tests updated.
merge-requests/2/merge
Marnanel Thurman 2020-11-25 20:20:00 +00:00
rodzic e0517f6688
commit 7b37822d8d
2 zmienionych plików z 39 dodań i 0 usunięć

Wyświetl plik

@ -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),
],
)

Wyświetl plik

@ -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)