# _*_coding:utf-8_*_
from rest_framework.response import Response
class ApiResponse(Response):
msg_code = 1000
code = 200
msg = ""
def __init__(self, data="", msg_code=None, msg=None, code=None):
if msg_code:
self.biz_code = msg_code
if msg:
self.msg = msg
if code:
self.code = code
self.data = data
super().__init__(data=self.get_body(), headers=self.headers, status=self.code)
@property
def headers(self):
return {'Content-Type': 'application/json'}
def get_body(self):
body = dict(
msg=self.msg,
msg_code=self.msg_code,
data=self.data,
)
return body
class Success(ApiResponse):
code = 201
msg = "创建成功"
msg_code = 1000
class Response(ApiResponse):
code = 200
msg = "请求成功"
error_code = 1001
class ModifySuccess(ApiResponse):
code = 201
msg = "修改成功"
msg_code = 1002
class DeleteSuccess(ApiResponse):
code = 202
msg = "删除成功"
msg_code = 1003
class ParameterException(ApiResponse):
code = 400
msg = "请求参数异常"
msg_code = 2001
class ServerError(ApiResponse):
code = 500
msg = "服务器异常"
msg_code = 3001
class NotFound(ApiResponse):
code = 404
msg = '该资源未找到'
msg_code = 2002
class AuthFailed(ApiResponse):
code = 401
msg_code = 2003
msg = '身份验证失败'
class Forbidden(ApiResponse):
code = 403
msg_code = 2004
msg = '禁止访问'