def post(self, request, format=None):
     """
     Default post method.
     """
     serializer = ProductsSerializer(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 put(self, request, pk, format=None):
     """
     If its a put request, update that row.
     """
     product = self.get_object(pk)
     serializer = ProductsSerializer(product, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
 def post(self, request, *args, **kwargs):
     image_data = request.data["images"]
     del request.data["images"]
     serializer = ProductsSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         last_product = Products.objects.latest("created_date")
         update_image_data = self.get_Image_data(image_data, last_product.product_id)
         for single_image_data in update_image_data:
             serializer_image = ImageSerializer(data=single_image_data)
             if serializer_image.is_valid():
                 serializer_image.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def get(self, request, format=None):
     """
     Default page load on page request
     """
     product = Product.objects.all()
     serializer = ProductsSerializer(product, many=True)
     return Response(serializer.data)
Exemplo n.º 5
0
class ShopCartDetailSerializer(serializers.ModelSerializer):
    # 一条购物车关系记录对应的只有一个products。
    products = ProductsSerializer(many=False, read_only=True)

    class Meta:
        model = ShoppingCart
        fields = ("products", "nums")
Exemplo n.º 6
0
class UserFavDetailSerializer(serializers.ModelSerializer):
    # 通过products_id拿到商品信息。就需要嵌套的Serializer
    products = ProductsSerializer()

    class Meta:
        model = UserFav
        fields = ("products", "id")
Exemplo n.º 7
0
 def test_for_order_by_name(self):
     response = self.client.get(
         build_url('v1:products', params={'order_by': 'name'}))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(
         ProductsSerializer(Product.objects.order_by('name'),
                            many=True).data, response.data)
Exemplo n.º 8
0
 def change_featured(self, request, pk):
     obj = self.get_object()
     obj.is_featured = not obj.is_featured
     obj.save()
     return Response(ProductsSerializer(obj, context={
         'request': request
     }).data,
                     status=status.HTTP_200_OK)
Exemplo n.º 9
0
    def retrieve(self, request, pk=None):
        try:
            product = Product.objects.get(id=pk)
        except Product.DoesNotExist:
            raise exceptions.NotFound()

        serializer = ProductsSerializer(product)

        return Response(serializer.data)
Exemplo n.º 10
0
 def get(self, request, pk, format=None):
     """
     If its a get request, send that row.
     """
     product = self.get_object(pk)
     serializer = ProductsSerializer(product)
     data = serializer.data
     data['content'] = get_url(serializer.data['url'])
     return Response(data)
Exemplo n.º 11
0
 def test_for_order_by_sub_category_desc(self):
     response = self.client.get(
         build_url('v1:products',
                   params={
                       'order_by': 'sub_category',
                       'order': 'DESC'
                   }))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(
         ProductsSerializer(Product.objects.order_by('-sub_category'),
                            many=True).data, response.data)
Exemplo n.º 12
0
    def create(self, request):
        serializer = ProductsSerializer(data=request.data)

        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)
Exemplo n.º 13
0
def product_list(request, format=None):
    if request.method == 'GET':
        products = Product.objects.all()
        serializer = ProductsSerializer(products, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = ProductsSerializer(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)
Exemplo n.º 14
0
 def products(self, request):
     products = Cart.objects.filter(
         created_by_id=self.request.user.id).first().product
     sum_all = products.aggregate(price_all=Sum(F('price')))
     data = {
         "products":
         ProductsSerializer(products,
                            many=True,
                            context={
                                'request': request
                            }).data,
         "cart_value":
         sum_all['price_all']
     }
     return Response(data, status=status.HTTP_200_OK)
Exemplo n.º 15
0
    def update(self, request, pk=None):
        try:
            product = Product.objects.get(id=pk)
        except Product.DoesNotExist:
            raise exceptions.NotFound()

        serializer = ProductsSerializer(instance=product, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
Exemplo n.º 16
0
def product_detail(request, pk, format=None):

    try:
        product = Product.objects.get(pk=pk)
    except Product.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

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

    elif request.method == 'PUT':
        #data = JSONParser().parse(request)
        serializer = ProductsSerializer(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 HttpResponse(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 17
0
def product_search(request, query, format=None):
    if request.method == 'GET':
        products = Product.objects.filter(productName__icontains=query)
        serializer = ProductsSerializer(products, many=True)
        return Response(serializer.data)
Exemplo n.º 18
0
    def list(self, request):
        products = Product.objects.all()
        serializer = ProductsSerializer(products, many=True)

        return Response(serializer.data)
Exemplo n.º 19
0
class OrderProductsSerialzier(serializers.ModelSerializer):
    products = ProductsSerializer(many=False)

    class Meta:
        model = OrderProducts
        fields = "__all__"