Exemple #1
0
    def post(self, request):
        inventory = request.data.get('inventory')
        if not inventory:
            raise ValidationError("`inventory` is required.")

        try:
            data = get_products(inventory)
        except:
            # The csv module may raise any type of exception.
            raise ValidationError("Invalid inventory file.")

        # Store the data from the inventory file.
        customer = self.get_customer()
        new_contracts = self.create_contracts(data['contracts'], customer)
        new_locations = self.create_locations(data['locations'], customer)
        new_products = self.create_products(data['products'], customer)

        log.info('inventory.import',
                 user=self.request.user.pk,
                 customer=customer.pk)

        result = {
            'new_contracts': ContractSerializer(new_contracts, many=True).data,
            'new_locations': LocationSerializer(new_locations, many=True).data,
            'new_products': ProductSerializer(new_products, many=True).data,
        }

        return Response(result)
Exemple #2
0
class UserProfileSerializer(serializers.ModelSerializer):
    id = serializers.ReadOnlyField()
    saved_products = ProductSerializer(many=True)
    current_cart = serializers.SerializerMethodField()

    class Meta:
        model = UserProfile
        fields = ("id", "picture", "saved_products", "carts", "current_cart")

    def get_current_cart(self, obj):
        cart = obj.carts.filter(checked_out=False).first()
        serializer = CartSerializer(cart)
        return serializer.data
Exemple #3
0
def product_list(request):
    if request.method == 'GET':
        products = Product.objects.all()
        serializer = ProductSerializer(products, many=True)
        return JsonResponse(serializer.data, safe=False)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = ProductSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
 def test_get_valid_single_product(self):
     product = Product.objects.create(
         code="PR004",
         name="Product 4",
         family="F002",
         price=185,
         remark="test retrieving a product"
     )
     response = self.client.get(
         reverse(PRODUCT_DETAIL, kwargs={"pk": product.pk})
     )
     serializer = ProductSerializer(product)
     self.assertEqual(response.data, serializer.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
Exemple #5
0
class ProductorProfileSerializer(serializers.ModelSerializer):
    id = serializers.ReadOnlyField()
    address = AddressSerializer()
    rating = serializers.FloatField(read_only=True)
    pictures = serializers.ListSerializer(child=MyImageSerializer(),
                                          required=False,
                                          allow_null=True)
    user_picture = serializers.FileField(source="user.user_profile.picture",
                                         read_only=True)
    products = ProductSerializer(many=True, required=False, allow_null=True)

    class Meta:
        model = ProductorProfile
        fields = ("id", "rating", "address", "pictures", "bio", "name",
                  "user_picture", 'products')
        read_only_fields = ("id", "user_picture", "rating")
Exemple #6
0
def category_product(request, pk):

    try:

        category = Category.objects.get(id=pk)

    except Category.DoesNotExist as e:

        return JsonResponse({'error': str(e)})



    products = category.product_set.all()

    serializer = ProductSerializer(products, many=True)

    return JsonResponse(serializer.data, safe=False)
 def test_retrieve_products_list(self):
     # Test retrieving a list of products
     Product.objects.create(
         code="PR001",
         name="Product 1",
         family="F001",
         price=137,
     )
     Product.objects.create(
         code="PR002",
         name="Product 2",
         family="F001",
         price=200,
     )
     response = self.client.get(PRODUCTS_URL)
     products = Product.objects.all().order_by("id")
     serializer = ProductSerializer(products, many=True)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data, serializer.data)
Exemple #8
0
def product_detail(request, pk):
    try:
        product = Product.objects.get(pk=pk)
    except Product.DoesNotExist:
        return HttpResponse(status=404)

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

    elif request.method == 'PUT':
        data = JsonParser().parse(request)
        serializer = ProductSerializer(product, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        product.delete()
        return HttpResponse(status=204)
Exemple #9
0
 def deleted(self, request):
     queryset = Product.objects.filter(is_deleted=True)
     serializer = ProductSerializer(queryset, many=True)
     return Response(serializer.data)