You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.4 KiB
45 lines
1.4 KiB
import six
|
|
from rest_framework.response import Response
|
|
from rest_framework.serializers import Serializer
|
|
|
|
|
|
class CCAIResponse(Response):
|
|
def __init__(self, data=None, status=200, code=200, msg="成功",
|
|
template_name=None, headers=None,
|
|
exception=False, content_type=None, pagination=None, count=None):
|
|
|
|
super(Response, self).__init__(None, status=status)
|
|
|
|
if isinstance(data, Serializer):
|
|
msg = (
|
|
"You passed a Serializer instance as data, but "
|
|
"probably meant to pass serialized `.data` or "
|
|
"`.error`. representation."
|
|
)
|
|
raise AssertionError(msg)
|
|
if status >= 400 and msg=='成功':
|
|
msg = "失败"
|
|
if code == 200:
|
|
code = status
|
|
if pagination or count:
|
|
self.data = {
|
|
"code": code,
|
|
"message": msg,
|
|
"data": data,
|
|
"count": count,
|
|
"pagination": pagination
|
|
}
|
|
else:
|
|
self.data = {
|
|
"code": code,
|
|
"message": msg,
|
|
"data": data,
|
|
}
|
|
self.template_name = template_name
|
|
self.exception = exception
|
|
self.content_type = content_type
|
|
|
|
if headers:
|
|
for name, value in six.iteritems(headers):
|
|
self[name] = value
|
|
|
|
|