Coverage for apps/catalogues/serializers.py: 81%
67 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-27 13:33 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-10-27 13:33 -0600
1from django.db import transaction
2from django.utils import timezone
3from rest_framework import serializers
5from .models import Color, Material, Period, SizeCategory, Sole, Week
8class BasicSizeCategorySerializer(serializers.ModelSerializer):
9 """
10 Serializer for Size Category with minimum data
11 """
13 class Meta:
14 model = SizeCategory
15 read_only_fields = ["random_slug", "name", "initial", "final", "step"]
16 fields = read_only_fields
19class SizeCategorySerializer(serializers.ModelSerializer):
20 """
21 Serializer for SizeCategory
22 """
24 class Meta:
25 model = SizeCategory
26 read_only_fields = ["random_slug"]
27 fields = read_only_fields + ["name", "initial", "final", "step"]
30class WeekSerializer(serializers.ModelSerializer):
31 """
32 Serializer for Week
33 """
35 week_number = serializers.SerializerMethodField()
37 class Meta:
38 model = Week
39 read_only_fields = ["random_slug", "start_date", "end_date", "week_number"]
40 fields = read_only_fields + ["name"]
42 def get_week_number(self, instance):
43 year = instance.start_date.year
44 number = self.Meta.model.objects.filter(start_date__year=year, end_date__lt=instance.start_date).count() + 1
45 return number
48class PeriodSerializer(serializers.ModelSerializer):
49 """
50 Serializer for Period
51 """
53 weeks = WeekSerializer(many=True, read_only=True)
55 class Meta:
56 model = Period
57 read_only_fields = ["random_slug", "end_date"]
58 fields = read_only_fields + ["name", "start_date", "weeks"]
61class BulkPeriodCreateSerializer(serializers.Serializer):
62 """
63 Serializer to bulk create Periods given start date and number of periods
64 """
66 start_date = serializers.DateField()
67 number_of_periods = serializers.IntegerField(default=13)
68 year = serializers.CharField()
69 periods = PeriodSerializer(many=True, read_only=True)
71 def create(self, validated_data):
72 start_date = validated_data.get("start_date")
73 year = validated_data.get("year")
74 number_of_periods = validated_data.get("number_of_periods")
76 with transaction.atomic():
77 period_start_date = start_date
78 periods = []
79 for i in range(1, number_of_periods + 1):
80 period = Period.objects.create(name=f"P{i}-{year}", start_date=period_start_date)
81 periods.append(period)
82 period_start_date = period.end_date + timezone.timedelta(days=1)
84 validated_data["periods"] = periods
85 return validated_data
87 def validate_year(self, value):
88 if Period.objects.filter(name__icontains=value).exists():
89 raise serializers.ValidationError(f"Ya existen periodos para el año {value}")
90 return value
93class ColorSerializer(serializers.ModelSerializer):
94 """
95 Serializer for Color
96 """
98 class Meta:
99 model = Color
100 read_only_fields = ["random_slug"]
101 fields = read_only_fields + ["name", "value"]
104class MaterialSerializer(serializers.ModelSerializer):
105 """
106 Serializer for Material
107 """
109 class Meta:
110 model = Material
111 read_only_fields = ["random_slug"]
112 fields = read_only_fields + ["name"]
115class SoleSerializer(serializers.ModelSerializer):
116 """
117 Serializer for Sole
118 """
120 class Meta:
121 model = Sole
122 read_only_fields = ["random_slug"]
123 fields = read_only_fields + ["name"]