forked from gothinkster/django-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e0f76e
commit f2bb395
Showing
6 changed files
with
25 additions
and
23 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
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
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 |
---|---|---|
@@ -1,35 +1,29 @@ | ||
import json | ||
|
||
from rest_framework.renderers import JSONRenderer | ||
from rest_framework.utils.serializer_helpers import ReturnList | ||
|
||
|
||
class ConduitJSONRenderer(JSONRenderer): | ||
charset = 'utf-8' | ||
object_label = 'object' | ||
object_label_plural = 'objects' | ||
pagination_object_label = 'objects' | ||
pagination_object_count = 'count' | ||
|
||
def render(self, data, media_type=None, renderer_context=None): | ||
if isinstance(data, ReturnList): | ||
_data = json.loads( | ||
super(ConduitJSONRenderer, self).render(data).decode('utf-8') | ||
) | ||
|
||
if data.get('results', None) is not None: | ||
return json.dumps({ | ||
self.object_label_plural: _data | ||
self.pagination_object_label: data['results'], | ||
self.pagination_count_label: data['count'] | ||
}) | ||
else: | ||
# If the view throws an error (such as the user can't be authenticated | ||
# or something similar), `data` will contain an `errors` key. We want | ||
# the default JSONRenderer to handle rendering errors, so we need to | ||
# check for this case. | ||
errors = data.get('errors', None) | ||
|
||
if errors is not None: | ||
# As mentioned about, we will let the default JSONRenderer handle | ||
# rendering errors. | ||
return super(ConduitJSONRenderer, self).render(data) | ||
# If the view throws an error (such as the user can't be authenticated | ||
# or something similar), `data` will contain an `errors` key. We want | ||
# the default JSONRenderer to handle rendering errors, so we need to | ||
# check for this case. | ||
elif data.get('errors', None) is not None: | ||
return super(ConduitJSONRenderer, self).render(data) | ||
|
||
else: | ||
return json.dumps({ | ||
self.object_label: data | ||
}) |
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