diff --git a/kepi/tophat_ui/tests/test_view_for_mimetype.py b/kepi/tophat_ui/tests/test_view_for_mimetype.py index d4e506f..731e44f 100644 --- a/kepi/tophat_ui/tests/test_view_for_mimetype.py +++ b/kepi/tophat_ui/tests/test_view_for_mimetype.py @@ -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), + ], + ) diff --git a/kepi/tophat_ui/view_for_mimetype.py b/kepi/tophat_ui/view_for_mimetype.py index 0d6aabb..3de3bd8 100644 --- a/kepi/tophat_ui/view_for_mimetype.py +++ b/kepi/tophat_ui/view_for_mimetype.py @@ -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)