from django.shortcuts import render from rest_framework import viewsets, permissions, status from rest_framework.response import Response from .models import Bill, Payment from .serializers import BillSerializer, PaymentSerializer from datetime import datetime # to be implemented class BalanceViewSet(viewsets.ViewSet): # here we return a number # number = sum(payments) - sum(bills) #bills = Bill.objects.filter(owner=self.request.user) #payments = Payment.objects.filter(owner=self.request.user) # sum_paid = sum([ amount for amount payments..,. ]) # you get the picture # sum_to_be_paid = sum([ amount for amount bills..,. ]) # you get the picture pass class BillViewSet(viewsets.ModelViewSet): serializer_class = BillSerializer permission_classes = [permissions.IsAuthenticated] http_method_names = ['get'] 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) class PaymentViewSet(viewsets.ModelViewSet): serializer_class = PaymentSerializer permission_classes = [permissions.IsAuthenticated] http_method_names = ['get', 'post'] 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(owner=request.user,timestamp=datetime.now()) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)