python类APIException()的实例源码

views.py 文件源码 项目:rest_channels 作者: KhasanovBI 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def exception_handler(exc, context):
    if isinstance(exc, exceptions.APIException):
        if isinstance(exc.detail, dict):
            data = exc.detail
        else:
            data = {'detail': exc.detail}
        set_rollback()
        return data
    # Note: Unhandled exceptions will raise a 500 error.
    return None
exception.py 文件源码 项目:ws-backend-community 作者: lavalamp- 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def handle_api_exception(exc, context):
    """
    Handle the given APIException.
    :param exc: The exception that was thrown.
    :param context: The context in which the exception was thrown.
    :return: A Django Response object.
    """
    response = exception_handler(exc, context)
    response.data = {
        "status_code": response.status_code,
        "message": "Exception thrown",
        "detail": exc.detail,
        "error_fields": [],
    }
    return response
views.py 文件源码 项目:BackendAllStars 作者: belatrix 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def top(request, kind, quantity):
    """
    Returns top {quantity} list, {kind} (total_score, level, last_month_score, current_month_score, last_year_score,
    current_year_score)
    ---
    serializer: employees.serializers.EmployeeTopListSerializer
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden, authentication credentials were not provided
    - code: 404
      message: Not found
    - code: 500
      message: Internal server error, cannot resolve keyword into field.
    """
    try:
        employee_list_filtered = []
        if request.method == 'GET':
            employee_list = Employee.objects.filter(is_active=True,
                                                    is_base_profile_complete=True).order_by('-' + kind)[:quantity]
            if kind == 'total_score':
                for employee in employee_list:
                    if employee.total_score > 0:
                        employee_list_filtered.append(employee)
                serializer = EmployeeTopTotalScoreList(employee_list_filtered, many=True)
            elif kind == 'level':
                for employee in employee_list:
                    if employee.level > 0:
                        employee_list_filtered.append(employee)
                serializer = EmployeeTopLevelList(employee_list_filtered, many=True)
            elif kind == 'current_month_score':
                for employee in employee_list:
                    if employee.current_month_score > 0:
                        employee_list_filtered.append(employee)
                serializer = EmployeeTopCurrentMonthList(employee_list_filtered, many=True)
            elif kind == 'current_year_score':
                for employee in employee_list:
                    if employee.current_year_score > 0:
                        employee_list_filtered.append(employee)
                serializer = EmployeeTopCurrentYearList(employee_list_filtered, many=True)
            elif kind == 'last_month_score':
                for employee in employee_list:
                    if employee.last_month_score > 0:
                        employee_list_filtered.append(employee)
                serializer = EmployeeTopLastMonthList(employee_list_filtered, many=True)
            elif kind == 'last_year_score':
                for employee in employee_list:
                    if employee.last_year_score > 0:
                        employee_list_filtered.append(employee)
                serializer = EmployeeTopLastYearList(employee_list_filtered, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
    except Exception as e:
        raise APIException(e)


问题


面经


文章

微信
公众号

扫码关注公众号