From b9b605f407ed53d26418c2475ef928504411d92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Thu, 27 Feb 2020 12:21:25 +0100 Subject: [PATCH] Add ADMIN endpoints for bills and payments --- uncloud/uncloud/urls.py | 2 ++ uncloud/uncloud_pay/serializers.py | 2 +- uncloud/uncloud_pay/views.py | 39 +++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index 79958c5..341f81a 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -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) diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index 024fe3f..f4fd565 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -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: diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py index 8f37814..d824d27 100644 --- a/uncloud/uncloud_pay/views.py +++ b/uncloud/uncloud_pay/views.py @@ -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)