From b3bbfafa04db6aee9d7b496032520355cad2385d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Fri, 28 Feb 2020 14:57:45 +0100 Subject: [PATCH] Introduce custom ProductViewSet preventing customer from updating products --- uncloud/uncloud_pay/helpers.py | 14 +++++++++++++- uncloud/uncloud_vm/views.py | 3 ++- uncloud/ungleich_service/views.py | 5 ++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/uncloud/uncloud_pay/helpers.py b/uncloud/uncloud_pay/helpers.py index 2f68e9e..8ca82aa 100644 --- a/uncloud/uncloud_pay/helpers.py +++ b/uncloud/uncloud_pay/helpers.py @@ -1,10 +1,11 @@ from functools import reduce +from rest_framework import mixins +from rest_framework.viewsets import GenericViewSet from .models import Bill, Payment, PaymentMethod def sum_amounts(entries): return reduce(lambda acc, entry: acc + entry.amount, entries, 0) - def get_balance_for(user): bills = sum_amounts(Bill.objects.filter(owner=user)) payments = sum_amounts(Payment.objects.filter(owner=user)) @@ -18,3 +19,14 @@ def get_payment_method_for(user): return method return None + + +class ProductViewSet(mixins.CreateModelMixin, + mixins.RetrieveModelMixin, + mixins.ListModelMixin, + GenericViewSet): + """ + A customer-facing viewset that provides default `create()`, `retrieve()` + and `list()`. + """ + pass diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index e5fd4ba..c3704e1 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -9,6 +9,7 @@ from rest_framework.response import Response from .models import VMHost, VMProduct, VMSnapshotProduct from uncloud_pay.models import Order, RecurringPeriod from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer +from uncloud_pay.helpers import ProductViewSet import datetime @@ -19,7 +20,7 @@ class VMHostViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAdminUser] -class VMProductViewSet(viewsets.ModelViewSet): +class VMProductViewSet(ProductViewSet): permission_classes = [permissions.IsAuthenticated] serializer_class = VMProductSerializer diff --git a/uncloud/ungleich_service/views.py b/uncloud/ungleich_service/views.py index 776b94c..9c27df8 100644 --- a/uncloud/ungleich_service/views.py +++ b/uncloud/ungleich_service/views.py @@ -3,12 +3,15 @@ from rest_framework import viewsets, permissions from .models import MatrixServiceProduct from .serializers import MatrixServiceProductSerializer -class MatrixServiceProductViewSet(viewsets.ModelViewSet): +from uncloud_pay.helpers import ProductViewSet + +class MatrixServiceProductViewSet(ProductViewSet): permission_classes = [permissions.IsAuthenticated] serializer_class = MatrixServiceProductSerializer def get_queryset(self): return MatrixServiceProduct.objects.filter(owner=self.request.user) + def create(self, request): # TODO pass