Example #1
0
    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        user = serializer.validated_data.get('user') or request.user
        token = serializer.validated_data.get('token')
        return JsonResponse({'user': user, 'token': token})
Example #2
0
def custom_exception_handler(exc, context):
    code = 9999
    message = '未知异常'
    detail = None
    logger.error('error:{}'.format(exc))
    if isinstance(exc, CustomException):
        code = exc.detail['code']
        message = exc.detail['message']
        detail = exc.detail['detail']
    else:
        response = exception_handler(exc, context)
        if response is not None:
            if response.status_code == 404:
                code = 4004
                message = "资源未找到"
            elif response.status_code == 400:
                code = 4000
                message = '请求参数错误'
            elif response.status_code == 401:
                code = 4001
                message = '用户身份验证失败'
            elif response.status_code == 403:
                code = 4003
                message = "用户无此操作权限"
            elif response.status_code == 405:
                code = 4005
                message = '请求方式错误'
            elif response.status_code >= 500:
                code = 5000
                message = "服务器错误"

            detail = response.data

    return JsonResponse(code=code, message=message, detail=detail)
Example #3
0
 def retrieve(self, request, *args, **kwargs):
     # 重写查看详情逻辑,每次查看详情,该文章阅读数加1
     instance = self.get_object()
     instance.view_nums += 1
     instance.save()
     serializer = self.get_serializer(instance)
     return JsonResponse(serializer.data)
Example #4
0
    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        mobile = serializer.validated_data['mobile']
        send_type = serializer.validated_data['type']
        res = send_sms(mobile, send_type)
        if res['code'] != 'OK':
            raise CustomException(code=4103, message=res['message'])
        return JsonResponse(message='短信发送成功')
Example #5
0
    def get(self, request, *args, **kwargs):
        # 重写查看列表逻辑,提取并返回所有文章的tag
        queryset = self.filter_queryset(self.get_queryset())

        tag_list = []
        for item in queryset:
            if item.tags is not None:
                tag_list += [{'tag': tag} for tag in item['tags'].split(',')]

        serializer = self.get_serializer(tag_list, many=True)

        return JsonResponse(serializer.data)
Example #6
0
    def post(self, request, *args, **kwargs):

        result = talk(request.data['talk'], request.data['username'])
        user_id = self.request.user.id or 0
        user_ip = get_client_ip(request)

        serializer = self.get_serializer(data={
            'talk': request.data['talk'],
            'username': request.data['username'],
        })
        serializer.is_valid(raise_exception=True)
        serializer.save(result=result, user_id=user_id, user_ip=user_ip)
        return JsonResponse({'result': result})
Example #7
0
    def get(self, request, *args, **kwargs):
        # 重写查看列表逻辑,返回由年份和月份组成的一个datetime类型的集合
        queryset = self.filter_queryset(self.get_queryset())

        time_list = [
            f'{item["create_at"].year}-{item["create_at"].month}'
            for item in queryset
        ]

        serializer = ArchiveSerializer([{
            'archive': item
        } for item in set(time_list)],
                                       many=True)

        return JsonResponse(serializer.data)
Example #8
0
    def update(self, request, *args, **kwargs):
        # 重写更新逻辑,只有当文章内容发生改变时,才修改文章的更新时间
        partial = kwargs.pop('partial', False)
        instance = self.get_object()
        if len(request.data['content']) != len(instance.content):
            instance.update_at = datetime.now()
        serializer = self.get_serializer(instance,
                                         data=request.data,
                                         partial=partial)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        if getattr(instance, '_prefetched_objects_cache', None):
            instance._prefetched_objects_cache = {}

        return JsonResponse(serializer.data)
Example #9
0
 def get(self, request, *args, **kwargs):
     res = get_oss_token()
     return JsonResponse(res)
Example #10
0
 def retrieve(self, request, *args, **kwargs):
     instance = self.request.user
     serializer = self.get_serializer(instance)
     return JsonResponse(data=serializer.data, status=status.HTTP_200_OK)
Example #11
0
 def list(self, request, *args, **kwargs):
     queryset = self.filter_queryset(self.get_queryset())
     serializer = self.get_serializer(queryset, many=True)
     return JsonResponse(serializer.data)