Encapsulation

StatusCode

pip install StatusCode

utils/error/error_utils.py

# -*- coding: utf-8 -*-

from StatusCode import BaseStatusCode, StatusCodeField


class UserStatusCode(BaseStatusCode):
    """ User Relative StatusCode 4001xx """
    USER_NOT_FOUND = StatusCodeField(400101, 'User Not Found', description=u'用户不存在')

# INVALID_XXX = StatusCodeField(xxxx00, 'Invalid XXX', description=u'非法XXX')
# XXX_NOT_FOUND = StatusCodeField(xxxx01, 'XXX Not Found', description=u'XXX不存在')

utils/error/response_utils.py

# -*- coding: utf-8 -*-

from django.http import JsonResponse
from StatusCode import StatusCodeField


def response_data(status_code=200, message=None, description=None, data={}, **kwargs):
    return dict({
        'status': status_code,
        'message': message,
        'description': description,
        'data': data,
    }, **kwargs)


def response(status_code=200, message=None, description=None, data={}, **kwargs):
    message, description = (message or status_code.message, description or status_code.description) if isinstance(status_code, StatusCodeField) else (message, description)
    return JsonResponse(response_data(status_code, message, description, data, **kwargs), safe=False)

Usage

from utils.error.errno_utils import UserStatusCode
from utils.error.response_utils import response

def xxx(request):
    return response(UserStatusCode.USERNAME_HAS_REGISTERED)

References

results matching ""

    No results matching ""