From 0e62ccff3bf7126f2650f7640895f2c84a31fd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Thu, 5 Mar 2020 10:27:33 +0100 Subject: [PATCH] Cleanup/reorder uncloud_pay views --- uncloud/uncloud_pay/models.py | 2 +- uncloud/uncloud_pay/views.py | 68 +++++++++++++++++------------------ 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index 1e54b9e..1650e22 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -171,7 +171,7 @@ class PaymentMethod(models.Model): pass ### -# Bills & Payments. +# Bills. class Bill(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py index fb02774..a22c616 100644 --- a/uncloud/uncloud_pay/views.py +++ b/uncloud/uncloud_pay/views.py @@ -16,40 +16,7 @@ from datetime import datetime import uncloud_pay.stripe as uncloud_stripe ### -# 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) - -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.ReadOnlyModelViewSet): - serializer_class = OrderSerializer - permission_classes = [permissions.IsAuthenticated] - - def get_queryset(self): - return Order.objects.filter(owner=self.request.user) +# Users. class UserViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = UserSerializer @@ -58,6 +25,16 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet): def get_queryset(self): return get_user_model().objects.all() +### +# Payments and Payment Methods. + +class PaymentViewSet(viewsets.ReadOnlyModelViewSet): + serializer_class = PaymentSerializer + permission_classes = [permissions.IsAuthenticated] + + def get_queryset(self): + return Payment.objects.filter(owner=self.request.user) + class PaymentMethodViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] @@ -149,9 +126,30 @@ class PaymentMethodViewSet(viewsets.ModelViewSet): error = 'Could not fetch payment method from stripe. Please try again.' return Response({'error': error}) +### +# Bills and Orders. + +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 OrderViewSet(viewsets.ReadOnlyModelViewSet): + serializer_class = OrderSerializer + permission_classes = [permissions.IsAuthenticated] + + def get_queryset(self): + return Order.objects.filter(owner=self.request.user) + ### -# Admin views. +# Old admin stuff. class AdminPaymentViewSet(viewsets.ModelViewSet): serializer_class = PaymentSerializer