Ejemplo n.º 1
0
def exception_handler(exc, context):
    # Authentication
    if isinstance(exc, exceptions.AuthenticationFailed):
        return APIResponse(code=errors.TOKEN_EXPIRED,
                           message=exc.default_detail,
                           data={'detail': exc.detail},
                           status_code=exc.status_code)
    # Throttled
    if isinstance(exc, exceptions.Throttled):
        return APIResponse(code=errors.THROTTLE_REACHED,
                           message=exc.default_detail,
                           data={'detail': exc.detail})

    if isinstance(exc, exceptions.ValidationError):
        return APIResponse(code=errors.PARAMS_ERROR,
                           message=exc.default_code,
                           data={'detail': get_message(exc.detail)})

    if isinstance(exc, Exception):
        return APIResponse(code=errors.OTHER_ERROR,
                           message="服务器错误",
                           data={'detail': str(exc)})

    # Common
    if isinstance(exc, exceptions.APIException):
        # print(exc)
        return APIResponse(code=errors.OTHER_ERROR,
                           message=exc.default_detail,
                           data={'detail': exc.detail})
Ejemplo n.º 2
0
    def tiny_school(self, request, *args, **kwargs):
        if self.request.user.is_system_admin:
            queryset = self.filter_queryset(self.get_queryset())

            serializer = AdminTinySchoolSerializer(queryset, many=True)
            return APIResponse(data=serializer.data)
        return APIResponse(data=None)
Ejemplo n.º 3
0
 def list(self, request, *args, **kwargs):
     if self.request.user.is_school_admin or self.request.user.is_vice_school_admin or self.request.user.is_teacher:
         if self.request.user.teacher.school:
             return APIResponse(data=AdminSchoolSerializer(
                 self.request.user.teacher.school).data)
         else:
             return APIResponse(code=errors.OTHER_ERROR, message="学校不存在")
     return super().list(request, *args, **kwargs)
Ejemplo n.º 4
0
 def update(self, request, *args, **kwargs):
     if self.request.user.is_school_admin:
         if self.request.user.teacher.school:
             instance = self.request.user.teacher.school
             serializer = self.get_serializer(instance,
                                              data=request.data,
                                              partial=True)
             serializer.is_valid(raise_exception=True)
             self.perform_update(serializer)
             return APIResponse(data=serializer.data)
         else:
             return APIResponse(code=errors.OTHER_ERROR, message="学校不存在")
     return super().update(request, *args, **kwargs)
Ejemplo n.º 5
0
 def post(self, request, *args, **kwargs):
     serializer = BoardTokenSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.validated_data['user']
     token, created = AuthToken.objects.get_or_create(user=user)
     if not created:
         token.refresh_key()
     return APIResponse({'token': token.key, 'expire': token.expire})
Ejemplo n.º 6
0
def exception_handler(exc, context):
    # Custom Error
    if isinstance(exc, APIError):
        set_rollback()
        return APIResponse(code=exc.err_code, message=exc.message, data=None)

    # Authentication
    if isinstance(exc, exceptions.AuthenticationFailed):
        return APIResponse(code=errors.TOKEN_EXPIRED,
                           message=exc.default_detail,
                           data={'detail': exc.detail},
                           status_code=exc.status_code)
    # Throttled
    if isinstance(exc, exceptions.Throttled):
        return APIResponse(code=errors.THROTTLE_REACHED,
                           message=exc.default_detail,
                           data={'detail': exc.detail})

    # Common
    if isinstance(exc, exceptions.APIException):
        # print(exc)
        return APIResponse(code=errors.OTHER_ERROR,
                           message=exc.default_detail,
                           data={'detail': exc.detail})
Ejemplo n.º 7
0
    def post(self, request, *args, **kwargs):
        serializer = AuthTokenSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        token, created = AuthToken.objects.get_or_create(user=user)
        if not created:
            token.refresh_key()

        try:
            school = None
            if user.is_teacher:
                teacher = Teacher.objects.get(pk=user.pk)
                school = teacher.school

            if hasattr(request.META, 'HTTP_X_FORWARDED_FOR'):
                ip = request.META['HTTP_X_FORWARDED_FOR']
            else:
                ip = request.META['REMOTE_ADDR']

        except Exception as e:
            logg.info(e)

        return APIResponse({'token': token.key, 'expire': token.expire})
Ejemplo n.º 8
0
 def cascader_board(self, request, *args, **kwargs):
     queryset = self.filter_queryset(
         self.get_queryset().exclude(classes__board=None))
     serializer = self.get_serializer(queryset, many=True)
     return APIResponse(data=serializer.data)
Ejemplo n.º 9
0
 def cascader_school(self, request, *args, **kwargs):
     queryset = self.filter_queryset(self.get_queryset())
     serializer = self.get_serializer(queryset, many=True)
     return APIResponse(data=serializer.data)
Ejemplo n.º 10
0
 def teacher(self, request, class_id):
     cls = self.get_object()
     serializer = ClassTeacherSerializer(cls.teacher_relations.all(),
                                         many=True)
     return APIResponse(serializer.data)
Ejemplo n.º 11
0
 def post(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     serializer.update(self.user, validated_data=serializer.validated_data)
     return APIResponse(self.get_serializer(self.user).data)
Ejemplo n.º 12
0
 def get(self, request, *args, **kwargs):
     serializer = self.get_serializer(self.user)
     return APIResponse(serializer.data)
Ejemplo n.º 13
0
 def post(self, request, *args, **kwargs):
     auth_logout(request)
     return APIResponse()
Ejemplo n.º 14
0
 def post(self, request, *args, **kwargs):
     serializer = AuthTokenSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.validated_data['user']
     auth_login(request, user)
     return APIResponse()
Ejemplo n.º 15
0
def teacher_role_list(request):
    result = []
    for index, r in enumerate(TEACHER_ROLES):
        if index == 0 or index == 9:
            result.append({'role': index, 'role_desc': r})
    return APIResponse(result)
Ejemplo n.º 16
0
 def get(self, request):
     return APIResponse(UserProfileSerializer(instance=request.user).data)