-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathviews.py
38 lines (29 loc) · 1.34 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from rest_framework import viewsets
from rest_framework.decorators import list_route
from rest_framework.response import Response
from models import Allowance, Transaction
from serializers import TransactionSerializer, AllowanceTransactionSerializer
class TransactionViewSet(viewsets.ModelViewSet):
'''
Transactions not associated with an allowance
'''
queryset = Transaction.objects.without_allowance()
serializer_class = TransactionSerializer
def get_queryset(self, *args, **kwargs):
return self.queryset.today()
class AllowanceTransactionViewSet(viewsets.ModelViewSet):
'''
Transactions associated with the current user's allowance
'''
queryset = Transaction.objects.with_allowance()
serializer_class = AllowanceTransactionSerializer
def get_users_allowance(self):
return Allowance.objects.get(user=self.request.user)
def get_queryset(self, *args, **kwargs):
qs = super(AllowanceTransactionViewSet, self).get_queryset(*args, **
kwargs)
return qs.filter(allowance=self.get_users_allowance()).last_month()
@list_route(methods=['get'])
def total(self, *args, **kwargs):
everything = self.queryset.filter(allowance=self.get_users_allowance())
return Response({'balance': everything.total()})