Coverage for apps/accounting/views.py: 54%
31 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-22 14:26 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-09-22 14:26 -0600
1from django.db.models import CharField, F, Value
2from rest_framework import status
3from rest_framework.mixins import ListModelMixin as List
4from rest_framework.response import Response
5from rest_framework.viewsets import GenericViewSet
7from apps.campaigns.models import CampaignClaim
8from apps.rewards.models import RewardClaim
9from apps.users.mixins import FanMixin
11from .serializers import AccountingHistorySerializer, AccountingSerializer
14class FanAccountingViewSet(FanMixin, GenericViewSet):
15 """
16 Viewset for Fan Accounting
17 """
19 serializer_class = AccountingSerializer
21 def get_queryset(self):
22 queryset = super().get_queryset()
23 origin = self.request.headers.get("Origin", None)
24 try:
25 return queryset.get(fan=self.request.user.fan, organization__domain=origin)
26 except AccountingSerializer.Meta.model.DoesNotExist:
27 return None
29 def list(self, request, *args, **kwargs):
30 queryset = self.filter_queryset(self.get_queryset())
32 if queryset:
33 serializer = self.get_serializer(queryset)
34 else:
35 return Response(status=status.HTTP_404_NOT_FOUND)
36 return Response(serializer.data)
39class FanAccountingHistoryViewSet(FanMixin, GenericViewSet, List):
40 """
41 Viewset for Fan Accounting History
42 """
44 serializer_class = AccountingHistorySerializer
46 def get_queryset(self):
47 fan = self.request.user
49 campaigns = (
50 CampaignClaim.objects.filter(fan=fan)
51 .values("fan", "created_at")
52 .annotate(
53 random_slug=F("random_slug"),
54 name=F("campaign__name"),
55 value=F("campaign__value"),
56 type_of=Value("CampaignClaim", output_field=CharField()),
57 )
58 )
60 rewards = (
61 RewardClaim.objects.filter(fan=fan)
62 .values("fan", "created_at")
63 .annotate(
64 random_slug=F("random_slug"),
65 name=F("reward__name"),
66 value=F("reward__value"),
67 type_of=Value("RewardClaim", output_field=CharField()),
68 )
69 )
71 return campaigns.union(rewards).order_by("-created_at")