Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Last active August 30, 2017 13:54
Show Gist options
  • Save theY4Kman/3773fe8baaa1b3f27f7a01ee5d44b720 to your computer and use it in GitHub Desktop.
Save theY4Kman/3773fe8baaa1b3f27f7a01ee5d44b720 to your computer and use it in GitHub Desktop.
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)
djangorestframework-camel-case==0.2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment