def post(self, request, *args, **kwargs): username = request.data.get('username') password = request.data.get('password') user = authenticate(username=username, password=password) if user: payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) return JsonResponse(data={'token': token}, code=200, msg="success", status=status.HTTP_200_OK) else: return JsonResponse(code=400, msg="用户名或密码错误", status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, *args, **kwargs): instance = self.get_object() self.perform_destroy(instance) return JsonResponse(data=[], code=204, msg="delete resource success", status=status.HTTP_204_NO_CONTENT)
def retrieve(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) return JsonResponse(data=serializer.data, code=200, msg="success", status=status.HTTP_200_OK)
def get(self, request): token = request.GET.get('token') token_info = jwt_decode_handler(token) user_obj = UserProfile.objects.filter( username=token_info['username']).first() permission_list = init_permission(request, user_obj) menu_list = init_menu(request, user_obj) data = { 'id': user_obj.id, 'username': user_obj.username, 'name': user_obj.name, 'email': user_obj.email, 'image': request._request._current_scheme_host + '/media/' + str(user_obj.image), 'is_active': user_obj.is_active, 'permission': permission_list, 'menus': menu_list } return JsonResponse(data=data, msg="success", code=200, status=status.HTTP_200_OK)
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) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return JsonResponse(data=serializer.data)
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return JsonResponse(data=serializer.data, msg="success", code=201, status=status.HTTP_201_CREATED, headers=headers)
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) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} return JsonResponse(data=serializer.data, code=200, msg="success", status=status.HTTP_200_OK)