Coverage for events/serializers.py: 87%
63 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-13 14:00 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-13 14:00 -0600
1from django.db.models import Sum
2from rest_framework import serializers
3from memberships.models import Membership
4from memberships.serializers import BasicMembershipSerializer
5from .models import Event, EventImage, EventMembershipTypeTicketLimits, Ticket
8class EventImageSerializer(serializers.ModelSerializer):
9 """
10 Event Image Serializer
11 """
13 class Meta:
14 model = EventImage
15 read_only_fields = ["random_slug"]
16 fields = read_only_fields + ["event", "image", "order"]
19class EventMembershipTypeTicketLimitsSerializer(serializers.ModelSerializer):
20 """
21 Event MembershipType Ticket Limits Serializer
22 """
24 class Meta:
25 model = EventMembershipTypeTicketLimits
26 read_only_fields = ["random_slug"]
27 fields = read_only_fields + ["event", "membership_type", "amount"]
29 def validate(self, attrs):
30 amount = attrs.get("amount")
31 if amount:
32 event = self.instance.event if self.instance else attrs.get("event")
33 reserved_tickets = (
34 event.ticket_limits.exclude(pk=getattr(self.instance, "pk", None))
35 .aggregate(reserved_tickets=Sum("amount"))
36 .get("reserved_tickets")
37 or 0
38 )
39 remaining_tickets = event.max_tickets - reserved_tickets
40 if remaining_tickets < amount:
41 raise serializers.ValidationError(
42 {"amount": f"Solo hay {remaining_tickets} boletos disponibles para reservar"}
43 )
44 return attrs
47class EventSerializer(serializers.ModelSerializer):
48 """
49 Event Model Serializer
50 """
52 class Meta:
53 model = Event
54 read_only_fields = ["random_slug", "slug", "created_at"]
55 fields = [
56 "name",
57 "description",
58 "body",
59 "date",
60 "hour",
61 "main_image",
62 "only_members",
63 "allowed_memberships",
64 "max_tickets",
65 "max_tickets_per_membership",
66 "is_active",
67 "google_maps_address",
68 "ticket_description_name",
69 ] + read_only_fields
72class EventFinancialsSerializer(serializers.Serializer):
73 """
74 Serializer for Event financial data
75 """
77 has_discounts = serializers.BooleanField()
78 total_amount = serializers.DecimalField(max_digits=10, decimal_places=2)
79 paid_amount = serializers.DecimalField(max_digits=10, decimal_places=2)
80 remaining_amount = serializers.DecimalField(max_digits=10, decimal_places=2)
83class EventDisplaySerializer(EventSerializer):
84 """
85 Event Model Serializer for display
86 """
88 images = EventImageSerializer(many=True)
89 ticket_limits = EventMembershipTypeTicketLimitsSerializer(many=True)
90 sold_tickets = serializers.SerializerMethodField()
91 remaining_tickets = serializers.SerializerMethodField()
93 class Meta(EventSerializer.Meta):
94 fields = EventSerializer.Meta.fields + ["images", "ticket_limits", "sold_tickets", "remaining_tickets"]
96 def get_sold_tickets(self, instance):
97 return instance.tickets.count()
99 def get_remaining_tickets(self, instance):
100 total_amount = instance.products.aggregate(total_amount=Sum("max_amount")).get("total_amount") or 0
101 sold_tickets = instance.tickets.count()
102 return total_amount - sold_tickets
105class TicketSerializer(serializers.ModelSerializer):
106 """
107 Ticket Model Serializer
108 """
110 class Meta:
111 model = Ticket
112 read_only_fields = ["uuid", "created_at", "qr_code"]
113 fields = read_only_fields + [
114 "event",
115 "membership",
116 "first_name",
117 "last_name",
118 "email",
119 "phone",
120 "description",
121 "checked_in",
122 ]
124 def to_representation(self, instance):
125 representation = super(TicketSerializer, self).to_representation(instance)
126 event_slug = representation.get("event")
127 event = Event.objects.get(random_slug=event_slug)
128 serializer = EventSerializer(instance=event, context={"request": self.context.get("request")})
129 representation["event"] = serializer.data
130 membership_slug = representation.get("membership")
131 membership = Membership.objects.get(random_slug=membership_slug)
132 serializer = BasicMembershipSerializer(instance=membership)
133 representation["membership"] = serializer.data
134 return representation