Example #1
0
 def post(self, request):
     self.check_object_permissions(self.request, request.data)
     serializer = ProductSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
 def put(self, request, pk):
     product = self.get_object(pk)
     serializer = ProductSerializer(product, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #3
0
def api_update_product_view(request):
    try:
        product = Product.objects.get(id=1)
    except Product.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == "PUT":
        serializer = ProductSerializer(product)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data["success"] = "update successful"
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #4
0
class WishlistSerializer(serializers.ModelSerializer):
    products = ProductSerializer(many=True, read_only=True)
    owner = UserSerializer(read_only=True)

    class Meta:
        model = Wishlist
        fields = ('id', 'owner', 'products', 'name')
Example #5
0
 def test_not_authenticated_detail_retrieve(self):
     self.client.force_authenticate(user=None)
     serializer_data = ProductSerializer(instance=self.product).data
     response = self.client.get(
         reverse('product-detail', kwargs={"pk": self.product.id}))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(serializer_data, response.data)
 def post(self, request, *args, **kwargs):
     try:
         category = Category.objects.get(id=kwargs["id"])
     except:
         return JsonResponse(
             {
                 "status": "ERROR",
                 "message": "Category ot found"
             },
             status=404)
     data = json.loads(request.body)
     serializer = ProductSerializer(data=data)
     if serializer.is_valid():
         serializer.save(category=category)
         return JsonResponse({"status": "SUCCESS"}, status=201)
     else:
         return JsonResponse(serializer.errors, status=400)
Example #7
0
    def get_products(self, request, pk):
        category = self.get_object()
        products = category.products.all()
        serializer = ProductSerializer(products, many=True)

        print(request.user)

        return Response(data=serializer.data)
Example #8
0
def individual_product(request, id):
    if SellerProfile.objects.get(user=request.user) is not None:
        try:
            product = Product.objects.get(id=id)
        except Product.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if request.method == 'GET':
            serializer = ProductSerializer(product)
            return Response(serializer.data)

        elif request.method == 'PUT':
            serializer = ProductSerializer(product, data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data)
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        elif request.method == 'DELETE':
            product.delete()
            return Response(status=status.HTTP_204_NO_CONTENT)
    else:
        return Response({
            'Authorization Error':
            'You have to be a seller to add a product to sell'
        })
Example #9
0
def all_products(request):

    # if request.method == 'GET' and not request.user.is_anonymous:
    # 	try:
    # 		seller=SellerProfile.objects.get(user=request.user)
    # 		print(request.user)
    # 		products = Product.objects.filter(seller=request.user)
    # 		serializer = ProductSerializer(products, many=True)
    # 		return Response(serializer.data)
    # 	except SellerProfile.DoesNotExist:
    # 		data={}
    # 		data['seller_doesnt_exist']="Seller doesnt exist"
    # 		print(request.user)
    # 		products = Product.objects.all()
    # 		serializer = ProductSerializer(products, many=True)
    # 		return Response(serializer.data)

    if request.method == 'GET':
        data = {}
        print(request.user)
        products = Product.objects.all()
        serializer = ProductSerializer(products, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        seller = User.objects.get(id=1)
        product = Product(seller=seller)
        serializer = ProductSerializer(product, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def get(self, request, *args, **kwargs):
     try:
         category = Category.objects.get(id=kwargs["id"])
     except:
         return JsonResponse(
             {
                 "status": "ERROR",
                 "message": "Category ot found"
             },
             status=404)
     serializer = ProductSerializer(category.products.all(), many=True)
     return JsonResponse(serializer.data, safe=False)
Example #11
0
def api_detail_product_view(request):
    try:
        product = Product.objects.get(id=1)
    except Product.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == "GET":
        serializer = ProductSerializer(product)
        return JsonResponse({
            "prop1": "hey",
            "img": "some_fake_url.jpg",
            "title": product.title
        })
    def get(self, request, *args, **kwargs):
        try:
            product_id = kwargs["id"]
            product = Product.objects.get(id=product_id)
        except:
            return JsonResponse({
                "status": "ERROR",
                "message": "Not found"
            },
                                status=404)

        serializer = ProductSerializer(product)
        return JsonResponse(serializer.data)
Example #13
0
 def get(self, request):
     pro = Product.objects.filter()
     serializer = ProductSerializer(pro, many=True)
     return Response(serializer.data)
Example #14
0
 def list(self, request):
     queryset = Product.objects.filter(product__customer_id=request.user.pk)
     serializer = ProductSerializer(queryset,
                                    many=True,
                                    context={'request': request})
     return Response(status=status.HTTP_200_OK, data=serializer.data)
 def get(self, request, *args, **kwargs):
     products = Product.objects.all()
     serializer = ProductSerializer(products, many=True)
     return JsonResponse(serializer.data, safe=False)
Example #16
0
 def get(self, request, pk):
     product = self.get_object(pk)
     serializer = ProductSerializer(product)
     return Response(serializer.data, status=status.HTTP_200_OK)
Example #17
0
 def get(self, request):
     products = self.get_queryset(request=request)
     serializer = ProductSerializer(products,
                                    many=True,
                                    context={"request": request})
     return self.get_paginated_response(serializer.data)
Example #18
0
 def get(self, request, pk):
     pro = self.get_object(pk)
     serializer = ProductSerializer(pro)
     return Response(serializer.data)