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.
hbyd_ueba/lib/result.py

82 lines
1.9 KiB

# coding=utf-8
"""
根据nsfocus rest要求返回对应对象
"""
from django.http import JsonResponse
class Result(object):
"""
结果对象 用于返回给 JsonResponse
"""
SUCCESS_CODE = 200 # 成功的状态码
FAILED_CODE = 500 # 失败的状态码
SUCCESS_MESSAGE = 'success' # 返回单个数据的message
SUCCESS_MESSAGE_LIST = '请求成功' # 返回list的message
@classmethod
def ok(cls, data):
"""
返回单个对象
:param data: 对象 dict
:return:
"""
return JsonResponse({
'code': cls.SUCCESS_CODE,
'message': cls.SUCCESS_MESSAGE,
'data': data
})
@classmethod
def list(cls, total, page, limit, data):
"""
返回list
:param total:
:param page:
:param limit:
:param data:
:return:
"""
return JsonResponse({
'code': cls.SUCCESS_CODE,
'message': cls.SUCCESS_MESSAGE_LIST,
'data': {
'total': total,
'page': page,
'limit': limit,
'list': data
}
})
@classmethod
def failed(cls, message, detail=''):
"""
请求失败
:param message: 错误信息
:param detail: 详细描述
:return:
"""
return JsonResponse({
'code': cls.FAILED_CODE,
'message': message,
'data': {
'detail': detail
}
})
@classmethod
def failed_list(cls, message, data):
"""
部分请求失败
:param message: 失败描述
:param data: 失败内容的list
:return:
"""
return JsonResponse({
'code': cls.FAILED_CODE,
'message': message,
'data': {
'list': data
}
})