示例#1
0
 def post(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     if serializer.is_valid():
         user = serializer.object.get('user') or request.user
         token = serializer.object.get('token')
         response_data = jwt_response_payload_handler(token, user, request)
         result = genernal_message(code=status.HTTP_200_OK, msg="login success", data=response_data)
         response = Response(data=result, status=status.HTTP_200_OK)
         if api_settings.JWT_AUTH_COOKIE:
             expiration = (datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA)
             response.set_cookie(api_settings.JWT_AUTH_COOKIE, token, expires=expiration, httponly=True)
         return response
     err = serializer.errors
     result = genernal_message(code=status.HTTP_400_BAD_REQUEST, msg="login failed", data=err)
     return Response(data=result, status=status.HTTP_400_BAD_REQUEST)
示例#2
0
 def retrieve(self, request, *args, **kwargs):
     instance = self.get_object()
     serializer = self.get_serializer(instance)
     code = status.HTTP_200_OK
     result = genernal_message(code=code,
                               msg="数据获取成功",
                               data=serializer.data)
     return Response(data=result, status=code)
示例#3
0
 def destroy(self, request, *args, **kwargs):
     instance = self.get_object()
     instance = self.perform_destroy(instance)
     code, msg = (status.HTTP_200_OK, "权限删除成功") if isinstance(
         instance.is_delete, int) and instance.is_delete == 1 else (
             status.HTTP_400_BAD_REQUEST, "权限删除失败")
     result = genernal_message(code=code, msg=msg)
     return Response(data=result, status=code)
示例#4
0
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            # 这里会对数据做一些处理,关于数据总数上一页下一页...
            response = self.get_paginated_response(serializer.data)
            result = genernal_message(code=status.HTTP_200_OK,
                                      msg="权限列表返回成功",
                                      data=response.data)
            response.data = result
        else:
            serializer = self.get_serializer(queryset, many=True)
            result = genernal_message(code=status.HTTP_200_OK,
                                      msg="权限列表返回成功",
                                      data=serializer.data)
            response = Response(data=result)
        return response
示例#5
0
 def update(self, request, *args, **kwargs):
     partial = kwargs.pop('partial', False)
     instance = self.get_object()
     serializer = self.get_serializer(instance,
                                      data=request.data,
                                      partial=partial)
     code, re_dict = status.HTTP_200_OK, dict()
     if serializer.is_valid(raise_exception=False):
         self.perform_update(serializer)
         if getattr(instance, '_prefetched_objects_cache', None):
             instance._prefetched_objects_cache = {}
         msg, re_dict = "数据修改成功", serializer.data
     else:
         code, msg = status.HTTP_400_BAD_REQUEST, "数据修改失败"
     result = genernal_message(code=code, msg=msg, data=re_dict)
     return Response(result)
示例#6
0
 def create(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     if serializer.is_valid(raise_exception=False):
         perm = self.perform_create(serializer)
         if perm:
             re_dict = serializer.data
             code, msg = status.HTTP_201_CREATED, "add permission success"
             re_dict["id"] = perm.id
         else:
             re_dict, code, msg = {
                 "error": "创建权限失败"
             }, status.HTTP_400_BAD_REQUEST, "add permission failed"
     else:
         re_dict, code, msg = serializer.errors, status.HTTP_400_BAD_REQUEST, "add permission failed"
     result = genernal_message(code=code, msg=msg, data=re_dict)
     return Response(result, status=code)
示例#7
0
 def create(self, request, *args, **kwargs):
     serializer = self.get_serializer(data=request.data)
     # 如果是抛异常,你就自己没法处理,所以这里让不抛异常自己处理不合法的异常
     headers = None
     if serializer.is_valid(raise_exception=False):
         user = self.perform_create(serializer)
         if user:
             # 生成payload
             payload = jwt_payload_handler(user)
             # 拿到序列化的数据
             re_dict = serializer.data
             headers = self.get_success_headers(re_dict)
             code, msg = status.HTTP_201_CREATED, "user register success"
             # 拼装一个 payload 进去
             re_dict["id"] = user.id
             re_dict["token"] = jwt_encode_handler(payload)
         else:
             re_dict, code, msg = {
                 "error": "创建用户失败"
             }, status.HTTP_400_BAD_REQUEST, "user register failed"
     else:
         re_dict, code, msg = serializer.errors, status.HTTP_400_BAD_REQUEST, "user register failed"
     result = genernal_message(code=code, msg=msg, data=re_dict)
     return Response(result, status=code, headers=headers)