Coverage for apps/rewards/views.py: 83%
32 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-29 13:12 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-29 13:12 -0600
1from django.db.models import ProtectedError
2from rest_framework.exceptions import ValidationError
3from rest_framework.mixins import CreateModelMixin as Create
4from rest_framework.mixins import DestroyModelMixin as Delete
5from rest_framework.mixins import ListModelMixin as List
6from rest_framework.mixins import RetrieveModelMixin as Detail
7from rest_framework.mixins import UpdateModelMixin as Update
8from rest_framework.viewsets import GenericViewSet
10from apps.users.mixins import EmployeeMixin, FanMixin, PublicMixin
12from .models import Reward
13from .serializers import RewardClaimSerializer, RewardSerializer
16class BaseRewardViewSet:
17 """
18 Base Reward viewset
19 """
21 model = Reward
22 serializer_class = RewardSerializer
23 search_fields = ["name"]
26class EmployeeRewardViewSet(BaseRewardViewSet, EmployeeMixin, GenericViewSet, Create, List, Detail, Update, Delete):
27 """
28 Employee viewset for Rewards
29 """
31 filterset_fields = {"status": ["exact"]}
33 def destroy(self, request, *args, **kwargs):
34 try:
35 return super().destroy(request)
36 except ProtectedError:
37 raise ValidationError({"detail": "You cannot delete Rewards that have already been claimed."})
40class FanRewardViewSet(BaseRewardViewSet, FanMixin, GenericViewSet, List, Detail):
41 """
42 Fan viewset for Rewards
43 """
45 def get_queryset(self):
46 return self.model.objects.filter(status=self.model.Status.ACTIVE, organization=self.organization)
49class PublicRewardViewSet(BaseRewardViewSet, PublicMixin, GenericViewSet, List, Detail):
50 """
51 Public viewset for Rewards
52 """
54 pass
57class FanRewardClaimViewSet(FanMixin, GenericViewSet, Create, List):
58 """
59 Fan viewset for Reward claim
60 """
62 serializer_class = RewardClaimSerializer
64 def create(self, request, *args, **kwargs):
65 request.data["fan"] = self.request.user.pk
66 return super().create(request, *args, **kwargs)