Add ADMIN endpoints for bills and payments

This commit is contained in:
fnux 2020-02-27 12:21:25 +01:00
parent f358acca05
commit b9b605f407
3 changed files with 36 additions and 7 deletions

View file

@ -33,6 +33,8 @@ router.register(r'bill', payviews.BillViewSet, basename='bill')
router.register(r'payment', payviews.PaymentViewSet, basename='payment')
# admin/staff urls
router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill')
router.register(r'admin/payment', payviews.AdminPaymentViewSet, basename='admin/payment')
router.register(r'admin/vmhost', vmviews.VMHostViewSet)
router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula')
router.register(r'admin/opennebula_raw', oneviews.RawVMViewSet)

View file

@ -4,7 +4,7 @@ from .models import Bill, Payment
class BillSerializer(serializers.ModelSerializer):
class Meta:
model = Bill
fields = ['owner', 'amount']
fields = ['owner', 'amount', 'due_date', 'creation_date', 'starting_date', 'ending_date', 'paid']
class PaymentSerializer(serializers.ModelSerializer):
class Meta:

View file

@ -6,8 +6,9 @@ from .models import Bill, Payment
from .serializers import BillSerializer, PaymentSerializer
from datetime import datetime
###
# Standard user views:
# to be implemented
class BalanceViewSet(viewsets.ViewSet):
# here we return a number
# number = sum(payments) - sum(bills)
@ -20,10 +21,9 @@ class BalanceViewSet(viewsets.ViewSet):
pass
class BillViewSet(viewsets.ModelViewSet):
class BillViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = BillSerializer
permission_classes = [permissions.IsAuthenticated]
http_method_names = ['get']
def get_queryset(self):
return Bill.objects.filter(owner=self.request.user)
@ -31,10 +31,19 @@ class BillViewSet(viewsets.ModelViewSet):
def unpaid(self, request):
return Bill.objects.filter(owner=self.request.user, paid=False)
class PaymentViewSet(viewsets.ModelViewSet):
class PaymentViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = PaymentSerializer
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
return Payment.objects.filter(owner=self.request.user)
###
# Admin views.
class AdminPaymentViewSet(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)
@ -42,7 +51,25 @@ class PaymentViewSet(viewsets.ModelViewSet):
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())
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)
def create(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save(created_at=datetime.now())
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)