Пример #1
0
 def getSuperusers(request):
     a = User.objects.filter(is_superuser=True)
     # data = serializers.serialize('json', a)
     # print(data)
     # return HttpResponse(data, content_type="application/json")
     serializer = UserSerializer(a, many=True)
     print(serializer.data)
     return Response(serializer.data)
Пример #2
0
    def get(self, request, *args, **kwargs):

        username = request.query_params.get("username")
        password = request.query_params.get("password")
        user = User.objects.filter(username=username,
                                   password=password).first()
        if user:
            data = UserSerializer(user).data
            return APIResponse(200, True, results=data)

        return APIResponse(400, False, results='000')
Пример #3
0
    def post(self, request, *args, **kwargs):
        request_data = request.data
        ser = UserSerializer(data=request_data)
        ser.is_valid(raise_exception=True)
        user_obj = ser.save()

        return APIResponse(200, True, results=UserSerializer(user_obj).data)
Пример #4
0
def User_list(request):
    if request.method == "GET":
        users = User.objects.all()
        serializer = UserSerializer(users, many=True)
        return Response(serializer.data)

    elif request.method == "POST":
        print(request.body)
        serializer = UserSerializer(data=json.load(request.body))
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Пример #5
0
def User_detail(request, pk):
    try:
        user = User.objects.get(pk=pk)
    except User.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.methond == "GET":
        serializer = UserSerializer(user)
        return Response(serializer.data)
    elif request.method == "PUT":
        serializer = UserSerializer(user, data=json.loads(request.body))
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_404_NOT_FOUND)
    elif request.method == "DELETE":
        user.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #6
0
 def get(self, request):
     users = User.objects.all()
     serializer = UserSerializer(users, many=True)
     return Response(serializer.data)
Пример #7
0
 def get_list(request):
     a = User.objects.all()
     serializer = UserSerializer(a, many=True)
     return Response({"users": serializer.data})
Пример #8
0
 def get(self, request, pk):
     a = User.objects.get(pk=pk)
     serializer = UserSerializer(a, many=False)
     return Response({"user": serializer.data})
Пример #9
0
 def get(self, request):
     serializer = UserSerializer(request.user)
     return Response({"status": "success", "data": serializer.data})
Пример #10
0
 def get(self, request):
     serializer = UserSerializer(request.user)
     return Response(serializer.data)
Пример #11
0
 def test_get_one_user(self):
     resp = self.client.get(f'/api/v1/users/{self.user1.pk}/')
     user = User.objects.get(pk=self.user1.pk)
     serializer = UserSerializer(user)
     self.assertEqual(resp.status_code, 200)
     self.assertEqual(resp.data, serializer.data)
Пример #12
0
 def test_get_all_users(self):
     resp = self.client.get('/api/v1/users/')
     users = User.objects.all()
     serializer = UserSerializer(users, many=True)
     self.assertEqual(resp.status_code, 200)
     self.assertEqual(resp.data, serializer.data)
Пример #13
0
    def get(self, request, format=None):

        user = UserSerializer(self.request.user)
        return JsonResponse({'user': user.data})