diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index d6d3b7d..1342cde 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/models.py b/uncloud/uncloud_pay/models.py index 71653fa..6a33fd5 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -86,7 +86,7 @@ class Payment(models.Model): source = models.CharField(max_length=256, choices = ( ('wire', 'Wire Transfer'), - ('strip', 'Stripe'), + ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral'), ('unknown', 'Unknown') diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index e11544b..f4fd565 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -4,9 +4,9 @@ from .models import Bill, Payment class BillSerializer(serializers.ModelSerializer): class Meta: model = Bill - fields = ['user', 'amount'] + fields = ['owner', 'amount', 'due_date', 'creation_date', 'starting_date', 'ending_date', 'paid'] class PaymentSerializer(serializers.ModelSerializer): class Meta: model = Payment - fields = ['user', 'amount', 'source', 'timestamp'] + fields = ['owner', 'amount', 'source', 'timestamp'] diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py index 8fc02ea..d824d27 100644 --- a/uncloud/uncloud_pay/views.py +++ b/uncloud/uncloud_pay/views.py @@ -1,11 +1,14 @@ from django.shortcuts import render -from rest_framework import viewsets, permissions +from rest_framework import viewsets, permissions, status +from rest_framework.response import Response 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) @@ -18,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) @@ -29,18 +31,45 @@ 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] - http_method_names = ['get', 'post'] def get_queryset(self): - return Payment.objects.filter(user=self.request.user) + return Payment.objects.filter(owner=self.request.user) + +### +# Admin views. + +class AdminPaymentViewSet(viewsets.ModelViewSet): + serializer_class = PaymentSerializer + permission_classes = [permissions.IsAuthenticated] + + def get_queryset(self): + return Payment.objects.filter(owner=self.request.user) def create(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) - serializer.save(user=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)