-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support RequestDataTooBig exceptions
- Loading branch information
1 parent
b46f3e5
commit 9c8df3b
Showing
3 changed files
with
49 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.http import HttpResponse, JsonResponse | ||
from django.template.response import TemplateResponse | ||
|
||
from worf.conf import settings | ||
|
||
|
||
def browsable_response(request, response, status_code=200): | ||
template = "worf/api.html" | ||
context = dict( | ||
content=response.content.decode("utf-8"), | ||
response=response, | ||
settings=settings, | ||
) | ||
|
||
response = TemplateResponse(request, template, context=context) | ||
response.status_code = status_code | ||
response.render() | ||
|
||
return response | ||
|
||
|
||
def render_response(request, data, status_code=200): | ||
is_browsable = ( | ||
settings.WORF_BROWSABLE_API | ||
and "text/html" in request.headers.get("Accept", "") | ||
and request.GET.get("format") != "json" | ||
) | ||
|
||
response = ( | ||
JsonResponse(data, json_dumps_params=dict(indent=2 if is_browsable else 0)) | ||
if data != "" | ||
else HttpResponse() | ||
) | ||
|
||
response.status_code = status_code | ||
|
||
if is_browsable: | ||
response = browsable_response(request, response, status_code) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters