forked from uncloud/uncloud
115 lines
3.7 KiB
Python
115 lines
3.7 KiB
Python
from django.shortcuts import render
|
|
from django.contrib.auth import get_user_model
|
|
from rest_framework import viewsets, permissions, status
|
|
from rest_framework.response import Response
|
|
from rest_framework.decorators import action
|
|
|
|
import json
|
|
|
|
from .models import *
|
|
from .serializers import *
|
|
from datetime import datetime
|
|
|
|
###
|
|
# Standard user views:
|
|
|
|
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.ReadOnlyModelViewSet):
|
|
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)
|
|
|
|
class PaymentViewSet(viewsets.ReadOnlyModelViewSet):
|
|
serializer_class = PaymentSerializer
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return Payment.objects.filter(owner=self.request.user)
|
|
|
|
class OrderViewSet(viewsets.ModelViewSet):
|
|
serializer_class = OrderSerializer
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return Order.objects.filter(owner=self.request.user)
|
|
|
|
class UserViewSet(viewsets.ReadOnlyModelViewSet):
|
|
serializer_class = UserSerializer
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return get_user_model().objects.all()
|
|
|
|
class PaymentMethodViewSet(viewsets.ModelViewSet):
|
|
serializer_class = PaymentMethodSerializer
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return PaymentMethod.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)
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
###
|
|
# Admin views.
|
|
|
|
class AdminPaymentViewSet(viewsets.ModelViewSet):
|
|
serializer_class = PaymentSerializer
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return Payment.objects.all()
|
|
|
|
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.all()
|
|
|
|
def unpaid(self, request):
|
|
return Bill.objects.filter(owner=self.request.user, paid=False)
|
|
|
|
def create(self, request):
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
serializer.save(creation_date=datetime.now())
|
|
|
|
headers = self.get_success_headers(serializer.data)
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
|
|
|
|
class AdminOrderViewSet(viewsets.ModelViewSet):
|
|
serializer_class = OrderSerializer
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return Order.objects.all()
|