Coverage for discounts/serializers.py: 79%
21 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-01 11:04 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-01 11:04 -0600
1from rest_framework import serializers
2from products.models import Product
3from products.serializers import BasicProductPolymorphicSerializer
4from .models import MembershipDiscount, VolumeDiscount
7class MembershipDiscountSerializer(serializers.ModelSerializer):
8 """
9 Serializer for Membership discount
10 """
12 class Meta:
13 model = MembershipDiscount
14 read_only_fields = ["random_slug", "created_at"]
15 fields = [
16 "membership",
17 "percentage",
18 "max_amount",
19 "amount",
20 "product_categories",
21 "start_date",
22 "end_date",
23 "comments",
24 ] + read_only_fields
27class VolumeDiscountSerializer(serializers.ModelSerializer):
28 """
29 Serializer for Volume Discount
30 """
32 class Meta:
33 model = VolumeDiscount
34 read_only_fields = ["random_slug", "created_at"]
35 fields = read_only_fields + [
36 "percentage",
37 "max_amount",
38 "amount",
39 "product",
40 "min_purchase",
41 "max_purchase",
42 ]
44 def to_representation(self, instance):
45 representation = super(VolumeDiscountSerializer, self).to_representation(instance)
46 product_slug = representation.get("product")
47 product = Product.objects.get(random_slug=product_slug)
48 serializer = BasicProductPolymorphicSerializer(instance=product)
49 representation["product"] = serializer.data
50 return representation