Coverage for apps/receptions/serializers.py: 92%
53 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-10 13:24 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-10 13:24 -0600
1from django.db import transaction
2from rest_framework import serializers
4from api.serializers import ToRepresentationMixin
5from apps.orders.serializers import BasicOrderSerializer
6from apps.users.serializers import UserSerializer
8from .models import OrderReception, OrderReceptionComment, OrderReceptionImage, SizeReception
11class SizeReceptionSerializer(serializers.ModelSerializer):
12 """
13 Serializer for SizeReception
14 """
16 size = serializers.DecimalField(source="order_item_size.size", decimal_places=1, max_digits=4, read_only=True)
18 class Meta:
19 model = SizeReception
20 read_only_fields = ["random_slug", "size"]
21 fields = read_only_fields + ["order_item_size", "amount"]
22 extra_kwargs = {"order_reception": {"required": False}}
24 def validate(self, attrs):
25 order_item_size = attrs.get("order_item_size")
26 amount = attrs.get("amount")
28 if amount > order_item_size.pending_amount:
29 raise serializers.ValidationError(
30 {"amount": f"El monto de recepcion no puede ser mayor al pendiente ({order_item_size.pending_amount})."}
31 )
33 return attrs
36class OrderReceptionSerializer(ToRepresentationMixin):
37 """
38 Serializer for OrderReception
39 """
41 REPRESENTATION_FIELDS = [["order", BasicOrderSerializer, False]]
43 sizes = SizeReceptionSerializer(many=True)
45 class Meta:
46 model = OrderReception
47 read_only_fields = ["random_slug", "total_amount"]
48 fields = read_only_fields + ["order", "date", "sizes"]
50 def create(self, validated_data):
51 with transaction.atomic():
52 sizes = validated_data.pop("sizes", [])
54 order_reception = super().create(validated_data)
56 for size in sizes:
57 size["order_reception"] = order_reception
58 size_reception_serializer = SizeReceptionSerializer(size)
59 size_reception_serializer.create(size)
61 return order_reception
64class OrderReceptionCommentSerializer(serializers.ModelSerializer):
65 """
66 Serializer for OrderReceptionComment
67 """
69 author = UserSerializer(read_only=True)
70 children_comments = serializers.SerializerMethodField()
72 class Meta:
73 model = OrderReceptionComment
74 read_only_fields = ["random_slug", "created_at", "author", "children_comments"]
75 fields = read_only_fields + ["order_reception", "parent_comment", "comment"]
77 def get_children_comments(self, instance):
78 return OrderReceptionCommentSerializer(instance.children_comments.all(), many=True).data
80 def validate_parent_comment(self, value):
81 if value.parent_comment is not None:
82 raise serializers.ValidationError("No puede haber comentarios de mas de un nivel.")
83 return value
86class OrderReceptionImageSerializer(serializers.ModelSerializer):
87 """
88 Serializer for OrderReceptionImage
89 """
91 class Meta:
92 model = OrderReceptionImage
93 read_only_fields = ["random_slug", "created_at"]
94 fields = read_only_fields + ["order_reception", "image", "description"]