2020-02-27 10:21:38 +00:00
|
|
|
from django.shortcuts import render
|
2020-02-27 11:38:04 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-02-27 11:10:26 +00:00
|
|
|
from rest_framework import viewsets, permissions, status
|
|
|
|
from rest_framework.response import Response
|
2020-02-27 11:38:04 +00:00
|
|
|
from rest_framework.decorators import action
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 10:59:28 +00:00
|
|
|
from .models import Bill, Payment
|
2020-02-27 11:38:04 +00:00
|
|
|
from .serializers import BillSerializer, PaymentSerializer, UserSerializer
|
2020-02-27 11:10:26 +00:00
|
|
|
from datetime import datetime
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 11:21:25 +00:00
|
|
|
###
|
|
|
|
# Standard user views:
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 10:59:28 +00:00
|
|
|
class BalanceViewSet(viewsets.ViewSet):
|
2020-02-27 10:21:38 +00:00
|
|
|
# here we return a number
|
|
|
|
# number = sum(payments) - sum(bills)
|
|
|
|
|
2020-02-27 10:59:28 +00:00
|
|
|
#bills = Bill.objects.filter(owner=self.request.user)
|
|
|
|
#payments = Payment.objects.filter(owner=self.request.user)
|
2020-02-27 10:21:38 +00:00
|
|
|
|
|
|
|
# sum_paid = sum([ amount for amount payments..,. ]) # you get the picture
|
|
|
|
# sum_to_be_paid = sum([ amount for amount bills..,. ]) # you get the picture
|
2020-02-27 10:59:28 +00:00
|
|
|
pass
|
2020-02-27 10:21:38 +00:00
|
|
|
|
|
|
|
|
2020-02-27 11:21:25 +00:00
|
|
|
class BillViewSet(viewsets.ReadOnlyModelViewSet):
|
2020-02-27 10:59:28 +00:00
|
|
|
serializer_class = BillSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 10:59:28 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return Bill.objects.filter(owner=self.request.user)
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 10:59:28 +00:00
|
|
|
def unpaid(self, request):
|
|
|
|
return Bill.objects.filter(owner=self.request.user, paid=False)
|
2020-02-27 10:21:38 +00:00
|
|
|
|
2020-02-27 11:21:25 +00:00
|
|
|
class PaymentViewSet(viewsets.ReadOnlyModelViewSet):
|
2020-02-27 10:59:28 +00:00
|
|
|
serializer_class = PaymentSerializer
|
2020-02-27 10:21:38 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-02-27 11:10:26 +00:00
|
|
|
return Payment.objects.filter(owner=self.request.user)
|
2020-02-27 10:59:28 +00:00
|
|
|
|
2020-02-27 11:38:04 +00:00
|
|
|
class UserViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
serializer_class = UserSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return get_user_model().objects.all()
|
|
|
|
|
|
|
|
@action(detail=True)
|
|
|
|
def balance(self, request):
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
2020-02-27 11:21:25 +00:00
|
|
|
###
|
|
|
|
# Admin views.
|
|
|
|
|
|
|
|
class AdminPaymentViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = PaymentSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Payment.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save(timestamp=datetime.now())
|
|
|
|
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
|
|
|
|
class AdminBillViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = BillSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Bill.objects.filter(owner=self.request.user)
|
|
|
|
|
|
|
|
def unpaid(self, request):
|
|
|
|
return Bill.objects.filter(owner=self.request.user, paid=False)
|
|
|
|
|
2020-02-27 10:59:28 +00:00
|
|
|
def create(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-02-27 11:21:25 +00:00
|
|
|
serializer.save(created_at=datetime.now())
|
2020-02-27 10:59:28 +00:00
|
|
|
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|