Coverage for events/views.py: 77%
71 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-05-24 17:15 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-05-24 17:15 -0600
1from django.db.models import Sum, F
2from rest_framework import status
3from rest_framework.decorators import action
4from rest_framework.response import Response
5from rest_framework.mixins import (
6 ListModelMixin as List,
7 CreateModelMixin as Create,
8 RetrieveModelMixin as Detail,
9 DestroyModelMixin as Delete,
10 UpdateModelMixin as Update,
11)
12from rest_framework.viewsets import GenericViewSet
13from rest_framework.permissions import AllowAny
14from memberships.mixins import MemberMixin
15from organizations.mixins import OrganizationAdminMixin
16from .filters import EventFilter
17from .models import Event, Ticket
18from .serializers import (
19 EventSerializer,
20 EventDisplaySerializer,
21 EventImageSerializer,
22 EventMembershipTypeTicketLimitsSerializer,
23 TicketSerializer,
24 EventFinancialsSerializer,
25)
28class PublicEventViewSet(GenericViewSet, Detail):
29 """
30 Event View Set for Anonymous Users
31 """
33 permission_classes = [AllowAny]
34 serializer_class = EventDisplaySerializer
35 queryset = Event.objects.filter(only_members=False)
38class EventViewSet(OrganizationAdminMixin, GenericViewSet, List, Create, Detail, Update):
39 """
40 Event View Set for Admin Users
41 """
43 serializer_class = EventSerializer
44 filterset_class = EventFilter
46 def get_serializer_class(self):
47 if self.action == "list" or self.action == "retrieve":
48 return EventDisplaySerializer
49 return self.serializer_class
51 @action(methods=["GET"], detail=True)
52 def financials(self, request, *args, **kwargs):
53 event = self.get_object()
55 has_discounts = event.products.filter(volumediscount__isnull=False).exists()
56 total_amount = event.products.aggregate(total_amount=Sum(F("price") * F("max_amount"))).get("total_amount") or 0
57 paid_amount = event.products.aggregate(paid_amount=Sum("sells__total_price")).get("paid_amount") or 0
59 data = {
60 "has_discounts": has_discounts,
61 "total_amount": total_amount,
62 "paid_amount": paid_amount,
63 "remaining_amount": total_amount - paid_amount,
64 }
65 serializer = EventFinancialsSerializer(data)
66 return Response(serializer.data)
69class MemberEventViewSet(GenericViewSet, List, Detail):
70 """
71 Event View Set for Member Users
72 """
74 serializer_class = EventSerializer
75 filterset_class = EventFilter
76 queryset = Event.objects.all()
78 def get_serializer_class(self):
79 if self.action == "list" or self.action == "retrieve":
80 return EventDisplaySerializer
81 return self.serializer_class
84class EventImageViewSet(OrganizationAdminMixin, GenericViewSet, Create, Update, Delete):
85 """
86 Event Image ViewSet
87 """
89 serializer_class = EventImageSerializer
90 model_organization = "event__organization"
93class EventMembershipTypeTicketLimitsViewSet(OrganizationAdminMixin, GenericViewSet, Create, Update, Delete):
94 """
95 Event MembershipType Ticket Limits ViewSet
96 """
98 serializer_class = EventMembershipTypeTicketLimitsSerializer
99 model_organization = "event__organization"
102class TicketViewSet(OrganizationAdminMixin, GenericViewSet, List, Update, Detail):
103 """
104 Event Tickets View Set for Admin Users
105 """
107 serializer_class = TicketSerializer
108 filterset_fields = ["membership", "event", "checked_in"]
109 search_fields = ["first_name", "last_name"]
110 model_organization = "event__organization"
111 lookup_field = "uuid"
113 @action(detail=True, methods=["POST"])
114 def checkin(self, request, *args, **kwargs):
115 ticket = self.get_object()
117 if ticket.checked_in:
118 return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
120 ticket.checked_in = True
121 ticket.save()
123 serializer = self.get_serializer(ticket)
125 return Response(serializer.data)
128class MemberTicketViewSet(MemberMixin, GenericViewSet, List, Detail):
129 """
130 Event Ticket View Set for Member User
131 """
133 serializer_class = TicketSerializer
134 filterset_fields = ["event", "checked_in"]
135 search_fields = ["first_name", "last_name"]
136 lookup_field = "uuid"
139class PublicTicketViewSet(GenericViewSet, Detail):
140 """
141 Public Ticket View Set
142 """
144 permission_classes = [AllowAny]
145 queryset = Ticket.objects.all()
146 serializer_class = TicketSerializer
147 lookup_field = "uuid"