Coverage for apps/campaigns/views.py: 88%
51 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-29 16:03 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-29 16:03 -0600
1from rest_framework.decorators import action
2from rest_framework.mixins import CreateModelMixin as Create
3from rest_framework.mixins import DestroyModelMixin as Delete
4from rest_framework.mixins import ListModelMixin as List
5from rest_framework.mixins import RetrieveModelMixin as Detail
6from rest_framework.mixins import UpdateModelMixin as Update
7from rest_framework.viewsets import GenericViewSet
9from apps.users.mixins import EmployeeMixin, FanMixin, PublicMixin
11from .filters import CampaignFilter
12from .models import Campaign
13from .serializers import (
14 CampaignClaimPolymorphicSerializer,
15 CampaignStatsSerializer,
16 EmployeeCampaignPolymorphicSerializer,
17 EmployeeQuizzCampaignQuestionAnswerSerializer,
18 EmployeeQuizzCampaignQuestionSerializer,
19 FanCampaignPolymorphicSerializer,
20)
23class ReadOnlyBaseCampaignViewSet:
24 """
25 Base Campaign viewset
26 """
28 model = Campaign
29 serializer_class = FanCampaignPolymorphicSerializer
30 filterset_class = CampaignFilter
31 search_fields = ["name"]
33 def get_queryset(self):
34 queryset = super().get_queryset()
35 return queryset.filter(status=Campaign.Status.ACTIVE)
38class EmployeeCampaignViewSet(EmployeeMixin, GenericViewSet, Create, List, Detail, Update, Delete):
39 """
40 Employee viewset for Campaign
41 """
43 model = Campaign
44 serializer_class = EmployeeCampaignPolymorphicSerializer
45 filterset_class = CampaignFilter
46 search_fields = ["name"]
48 def perform_create(self, serializer):
49 serializer.save(author=self.request.user)
51 @action(methods=["GET"], detail=True)
52 def stats(self, request, *args, **kwargs):
53 self.serializer_class = CampaignStatsSerializer
54 return super().retrieve(request, *args, **kwargs)
57class EmployeeQuizzCampaignQuestionViewSet(EmployeeMixin, GenericViewSet, Create, Update, Delete):
58 """
59 ViewSet for Quizz Campaign Questions
60 """
62 serializer_class = EmployeeQuizzCampaignQuestionSerializer
63 filterset_fields = {"quizz_campaign": ["exact"]}
66class EmployeeQuizzCampaignQuestionAnswerViewSet(EmployeeMixin, GenericViewSet, Create, Update, Delete):
67 """
68 ViewSet for Quizz Campaign QuestionAnswers
69 """
71 serializer_class = EmployeeQuizzCampaignQuestionAnswerSerializer
72 filterset_fields = {"quizz_campaign_question": ["exact"]}
75class FanCampaignViewSet(ReadOnlyBaseCampaignViewSet, FanMixin, GenericViewSet, List, Detail):
76 """
77 Fan viewset for Campaign
78 """
80 def get_queryset(self):
81 return self.model.objects.filter(status=Campaign.Status.ACTIVE)
84class PublicCampaignViewSet(ReadOnlyBaseCampaignViewSet, PublicMixin, GenericViewSet, List, Detail):
85 """
86 Public viewset for Campaign
87 """
89 pass
92class EmployeeCampaignClaimViewSet(EmployeeMixin, GenericViewSet, List):
93 """
94 Employee viewset for Campaign Claim
95 """
97 model_organization = "campaign__organization"
99 serializer_class = CampaignClaimPolymorphicSerializer
100 filterset_fields = ["campaign", "fan"]
101 search_fields = ["fan__first_name", "fan__last_name"]
104class FanCampaignClaimViewSet(FanMixin, GenericViewSet, Create, List):
105 """
106 Fan viewset for Campaign Claim
107 """
109 serializer_class = CampaignClaimPolymorphicSerializer
111 def create(self, request, *args, **kwargs):
112 request.data["fan"] = self.request.user.pk
113 return super().create(request, *args, **kwargs)