Merge remote-tracking branch 'origin/fnux-hacks'

This commit is contained in:
Nico Schottelius 2020-02-27 12:43:24 +01:00
commit e89fb45f9c
4 changed files with 54 additions and 6 deletions

View file

@ -80,6 +80,8 @@ sample values with real values.
## Working Beta APIs ## Working Beta APIs
These APIs can be used for internal testing.
### Snapshotting ### Snapshotting
``` ```

View file

@ -29,12 +29,15 @@ router.register(r'vm/snapshot', vmviews.VMSnapshotProductViewSet, basename='vmsn
router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct') router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct')
# Pay # Pay
router.register(r'user', payviews.UserViewSet, basename='user')
router.register(r'bill', payviews.BillViewSet, basename='bill') router.register(r'bill', payviews.BillViewSet, basename='bill')
router.register(r'order', payviews.OrderViewSet, basename='order')
router.register(r'payment', payviews.PaymentViewSet, basename='payment') router.register(r'payment', payviews.PaymentViewSet, basename='payment')
# admin/staff urls # admin/staff urls
router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill') router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill')
router.register(r'admin/payment', payviews.AdminPaymentViewSet, basename='admin/payment') 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/vmhost', vmviews.VMHostViewSet)
router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula') router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula')
router.register(r'admin/opennebula_raw', oneviews.RawVMViewSet) router.register(r'admin/opennebula_raw', oneviews.RawVMViewSet)

View file

@ -1,12 +1,28 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers from rest_framework import serializers
from .models import Bill, Payment from .models import Bill, Payment, Order
class BillSerializer(serializers.ModelSerializer): class BillSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Bill 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 PaymentSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Payment model = Payment
fields = ['owner', 'amount', 'source', 'timestamp'] 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()
fields = ['username', 'email']
def get_balance(self, obj):
return 666

View file

@ -1,9 +1,11 @@
from django.shortcuts import render from django.shortcuts import render
from django.contrib.auth import get_user_model
from rest_framework import viewsets, permissions, status from rest_framework import viewsets, permissions, status
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.decorators import action
from .models import Bill, Payment from .models import Bill, Payment, Order
from .serializers import BillSerializer, PaymentSerializer from .serializers import BillSerializer, PaymentSerializer, UserSerializer, OrderSerializer
from datetime import datetime from datetime import datetime
### ###
@ -38,6 +40,24 @@ class PaymentViewSet(viewsets.ReadOnlyModelViewSet):
def get_queryset(self): def get_queryset(self):
return Payment.objects.filter(owner=self.request.user) 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]
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. # Admin views.
@ -46,7 +66,7 @@ class AdminPaymentViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
def get_queryset(self): def get_queryset(self):
return Payment.objects.filter(owner=self.request.user) return Payment.objects.all()
def create(self, request): def create(self, request):
serializer = self.get_serializer(data=request.data) serializer = self.get_serializer(data=request.data)
@ -61,7 +81,7 @@ class AdminBillViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
def get_queryset(self): def get_queryset(self):
return Bill.objects.filter(owner=self.request.user) return Bill.objects.all()
def unpaid(self, request): def unpaid(self, request):
return Bill.objects.filter(owner=self.request.user, paid=False) return Bill.objects.filter(owner=self.request.user, paid=False)
@ -73,3 +93,10 @@ class AdminBillViewSet(viewsets.ModelViewSet):
headers = self.get_success_headers(serializer.data) headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) 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()