From f5eadd6ddbbcbd08a65a1beb8f3796bb6aa5a9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Thu, 27 Feb 2020 12:38:04 +0100 Subject: [PATCH 1/2] Move user view to uncloud_pay --- uncloud/uncloud/urls.py | 1 + uncloud/uncloud_api/views.py | 6 +++++- uncloud/uncloud_pay/serializers.py | 9 +++++++++ uncloud/uncloud_pay/views.py | 15 ++++++++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index 341f81a..358e4c7 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -29,6 +29,7 @@ router.register(r'vm/snapshot', vmviews.VMSnapshotProductView, basename='VMSnaps router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct') # Pay +router.register(r'user', payviews.UserViewSet, basename='user') router.register(r'bill', payviews.BillViewSet, basename='bill') router.register(r'payment', payviews.PaymentViewSet, basename='payment') diff --git a/uncloud/uncloud_api/views.py b/uncloud/uncloud_api/views.py index c90b963..18cc324 100644 --- a/uncloud/uncloud_api/views.py +++ b/uncloud/uncloud_api/views.py @@ -43,9 +43,13 @@ import re # return Response(products) -class UserViewSet(viewsets.ModelViewSet): +class UserViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = UserSerializer permission_classes = [permissions.IsAuthenticated] def get_queryset(self): return self.request.user + + @action(detail=True) + def balance(self, request): + return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index f4fd565..5bb22ec 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -1,3 +1,4 @@ +from django.contrib.auth import get_user_model from rest_framework import serializers from .models import Bill, Payment @@ -10,3 +11,11 @@ class PaymentSerializer(serializers.ModelSerializer): class Meta: model = Payment fields = ['owner', 'amount', 'source', 'timestamp'] + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = get_user_model() + fields = ['username', 'email'] + + def get_balance(self, obj): + return 666 diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py index d824d27..5111f6c 100644 --- a/uncloud/uncloud_pay/views.py +++ b/uncloud/uncloud_pay/views.py @@ -1,9 +1,11 @@ from django.shortcuts import render +from django.contrib.auth import get_user_model from rest_framework import viewsets, permissions, status from rest_framework.response import Response +from rest_framework.decorators import action from .models import Bill, Payment -from .serializers import BillSerializer, PaymentSerializer +from .serializers import BillSerializer, PaymentSerializer, UserSerializer from datetime import datetime ### @@ -38,6 +40,17 @@ class PaymentViewSet(viewsets.ReadOnlyModelViewSet): def get_queryset(self): return Payment.objects.filter(owner=self.request.user) +class UserViewSet(viewsets.ReadOnlyModelViewSet): + serializer_class = UserSerializer + permission_classes = [permissions.IsAuthenticated] + + def get_queryset(self): + return get_user_model().objects.all() + + @action(detail=True) + def balance(self, request): + return Response(status=status.HTTP_204_NO_CONTENT) + ### # Admin views. From 1ff5702ce3cd217f2fd26442c76a466fe558d1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Thu, 27 Feb 2020 12:42:24 +0100 Subject: [PATCH 2/2] Expose Order model --- uncloud/uncloud/urls.py | 2 ++ uncloud/uncloud_pay/serializers.py | 11 +++++++++-- uncloud/uncloud_pay/views.py | 22 ++++++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index 358e4c7..9ea7c6a 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -31,11 +31,13 @@ router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct') # Pay router.register(r'user', payviews.UserViewSet, basename='user') router.register(r'bill', payviews.BillViewSet, basename='bill') +router.register(r'order', payviews.OrderViewSet, basename='order') 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/order', payviews.AdminOrderViewSet, basename='admin/order') 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 5bb22ec..be00a0c 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -1,17 +1,24 @@ from django.contrib.auth import get_user_model from rest_framework import serializers -from .models import Bill, Payment +from .models import Bill, Payment, Order class BillSerializer(serializers.ModelSerializer): class Meta: model = Bill - fields = ['owner', 'amount', 'due_date', 'creation_date', 'starting_date', 'ending_date', 'paid'] + fields = ['owner', 'amount', 'due_date', 'creation_date', + 'starting_date', 'ending_date', 'paid'] class PaymentSerializer(serializers.ModelSerializer): class Meta: model = Payment fields = ['owner', 'amount', 'source', 'timestamp'] +class OrderSerializer(serializers.ModelSerializer): + class Meta: + model = Order + fields = ['owner', 'creation_date', 'starting_date', 'ending_date', + 'bill', 'recurring_price', 'one_time_price', 'recurring_period'] + class UserSerializer(serializers.ModelSerializer): class Meta: model = get_user_model() diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py index 5111f6c..ae88861 100644 --- a/uncloud/uncloud_pay/views.py +++ b/uncloud/uncloud_pay/views.py @@ -4,8 +4,8 @@ from rest_framework import viewsets, permissions, status from rest_framework.response import Response from rest_framework.decorators import action -from .models import Bill, Payment -from .serializers import BillSerializer, PaymentSerializer, UserSerializer +from .models import Bill, Payment, Order +from .serializers import BillSerializer, PaymentSerializer, UserSerializer, OrderSerializer from datetime import datetime ### @@ -40,6 +40,13 @@ class PaymentViewSet(viewsets.ReadOnlyModelViewSet): 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) + class UserViewSet(viewsets.ReadOnlyModelViewSet): serializer_class = UserSerializer permission_classes = [permissions.IsAuthenticated] @@ -59,7 +66,7 @@ class AdminPaymentViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] def get_queryset(self): - return Payment.objects.filter(owner=self.request.user) + return Payment.objects.all() def create(self, request): serializer = self.get_serializer(data=request.data) @@ -74,7 +81,7 @@ class AdminBillViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] def get_queryset(self): - return Bill.objects.filter(owner=self.request.user) + return Bill.objects.all() def unpaid(self, request): return Bill.objects.filter(owner=self.request.user, paid=False) @@ -86,3 +93,10 @@ class AdminBillViewSet(viewsets.ModelViewSet): headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + +class AdminOrderViewSet(viewsets.ModelViewSet): + serializer_class = OrderSerializer + permission_classes = [permissions.IsAuthenticated] + + def get_queryset(self): + return Order.objects.all()