Last active
August 30, 2017 13:54
-
-
Save theY4Kman/3773fe8baaa1b3f27f7a01ee5d44b720 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import json | |
from djangorestframework_camel_case.util import camelize, underscoreize | |
class CamelCaseMixin: | |
def serialize_data(self, instance): | |
data = super().serialize_data(instance) | |
return camelize(data) | |
def reply(self, action, data=None, errors=[], status=200, request_id=None): | |
""" | |
Helper method to send a encoded response to the message's reply_channel. | |
""" | |
payload = { | |
'errors': errors, | |
'data': data, | |
'action': action, | |
'response_status': status, | |
'request_id': request_id | |
} | |
payload = camelize(payload) | |
return self.message.reply_channel.send(self.encode(self.stream, payload)) | |
def deserialize(self, message): | |
body = json.loads(message['text']) | |
body = underscoreize(body) | |
self.request_id = body.get("request_id") | |
action = body['action'] | |
pk = body.get('pk', None) | |
data = body.get('data', None) | |
return action, pk, underscoreize(data) |
This file contains hidden or 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
djangorestframework-camel-case==0.2.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment