Spaces:
Sleeping
Sleeping
File size: 3,126 Bytes
e7b5120 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | from rest_framework import serializers
from .models import (
User,
Address,
Product,
Order,
OrderDetail,
)
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=False)
class Meta:
model = User
fields = [
'id',
'username',
'email',
'password',
'full_name',
'user_age',
'user_gender',
'created_at',
]
read_only_fields = ['id', 'created_at']
def create(self, validated_data):
password = validated_data.pop('password', None)
user = User(**validated_data)
if password:
user.set_password(password)
user.save()
return user
def update(self, instance, validated_data):
password = validated_data.pop('password', None)
for attr, value in validated_data.items():
setattr(instance, attr, value)
if password:
instance.set_password(password)
instance.save()
return instance
class AddressSerializer(serializers.ModelSerializer):
# user is set server-side from the authenticated request
user = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Address
fields = [
'id',
'user',
'line1',
'line2',
'city',
'state',
'country',
'pincode',
'is_default',
'created_at',
]
read_only_fields = ['id', 'created_at']
class ProductSerializer(serializers.ModelSerializer):
display_price = serializers.SerializerMethodField()
is_price_adjusted = serializers.SerializerMethodField()
class Meta:
model = Product
fields = [
'id',
'product_name',
'base_price',
'display_price',
'is_price_adjusted',
'stock_quantity',
]
read_only_fields = ['id']
def get_display_price(self, obj):
"""Return base_price + 10% markup if user is predicted to be a high returner."""
markup = self.context.get('return_risk_markup', {})
if markup.get('apply'):
from decimal import Decimal
return str(round(float(obj.base_price) * 1.10, 2))
return str(obj.base_price)
def get_is_price_adjusted(self, obj):
markup = self.context.get('return_risk_markup', {})
return markup.get('apply', False)
class OrderDetailSerializer(serializers.ModelSerializer):
product = ProductSerializer(read_only=True)
product_id = serializers.PrimaryKeyRelatedField(
write_only=True, queryset=Product.objects.all(), source='product'
)
order = serializers.PrimaryKeyRelatedField(queryset=Order.objects.all())
class Meta:
model = OrderDetail
fields = [
'id',
'order',
'product',
'product_id',
'order_quantity',
'product_price',
'discount_applied',
'return_status',
'return_date',
'return_reason',
'days_to_return',
'is_exchanged',
'exchange_order',
]
read_only_fields = ['id']
class OrderSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(read_only=True)
shipping_address = AddressSerializer(read_only=True)
items = OrderDetailSerializer(many=True, read_only=True)
class Meta:
model = Order
fields = [
'id',
'user',
'shipping_address',
'order_date',
'payment_method',
'shipping_method',
'items',
]
read_only_fields = ['id', 'order_date']
|