Introduce custom ProductViewSet preventing customer from updating

products
This commit is contained in:
fnux 2020-02-28 14:57:45 +01:00
parent 33cc2b2111
commit b3bbfafa04
3 changed files with 19 additions and 3 deletions

View file

@ -1,10 +1,11 @@
from functools import reduce from functools import reduce
from rest_framework import mixins
from rest_framework.viewsets import GenericViewSet
from .models import Bill, Payment, PaymentMethod from .models import Bill, Payment, PaymentMethod
def sum_amounts(entries): def sum_amounts(entries):
return reduce(lambda acc, entry: acc + entry.amount, entries, 0) return reduce(lambda acc, entry: acc + entry.amount, entries, 0)
def get_balance_for(user): def get_balance_for(user):
bills = sum_amounts(Bill.objects.filter(owner=user)) bills = sum_amounts(Bill.objects.filter(owner=user))
payments = sum_amounts(Payment.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 method
return None return None
class ProductViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.ListModelMixin,
GenericViewSet):
"""
A customer-facing viewset that provides default `create()`, `retrieve()`
and `list()`.
"""
pass

View file

@ -9,6 +9,7 @@ from rest_framework.response import Response
from .models import VMHost, VMProduct, VMSnapshotProduct from .models import VMHost, VMProduct, VMSnapshotProduct
from uncloud_pay.models import Order, RecurringPeriod from uncloud_pay.models import Order, RecurringPeriod
from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer from .serializers import VMHostSerializer, VMProductSerializer, VMSnapshotProductSerializer
from uncloud_pay.helpers import ProductViewSet
import datetime import datetime
@ -19,7 +20,7 @@ class VMHostViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAdminUser] permission_classes = [permissions.IsAdminUser]
class VMProductViewSet(viewsets.ModelViewSet): class VMProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
serializer_class = VMProductSerializer serializer_class = VMProductSerializer

View file

@ -3,12 +3,15 @@ from rest_framework import viewsets, permissions
from .models import MatrixServiceProduct from .models import MatrixServiceProduct
from .serializers import MatrixServiceProductSerializer from .serializers import MatrixServiceProductSerializer
class MatrixServiceProductViewSet(viewsets.ModelViewSet): from uncloud_pay.helpers import ProductViewSet
class MatrixServiceProductViewSet(ProductViewSet):
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
serializer_class = MatrixServiceProductSerializer serializer_class = MatrixServiceProductSerializer
def get_queryset(self): def get_queryset(self):
return MatrixServiceProduct.objects.filter(owner=self.request.user) return MatrixServiceProduct.objects.filter(owner=self.request.user)
def create(self, request): def create(self, request):
# TODO # TODO
pass pass