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

This commit is contained in:
Nico Schottelius 2020-02-27 12:36:40 +01:00
commit 1445acb77a
4 changed files with 42 additions and 11 deletions

View file

@ -33,6 +33,8 @@ router.register(r'bill', payviews.BillViewSet, basename='bill')
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/payment', payviews.AdminPaymentViewSet, basename='admin/payment')
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

@ -86,7 +86,7 @@ class Payment(models.Model):
source = models.CharField(max_length=256, source = models.CharField(max_length=256,
choices = ( choices = (
('wire', 'Wire Transfer'), ('wire', 'Wire Transfer'),
('strip', 'Stripe'), ('stripe', 'Stripe'),
('voucher', 'Voucher'), ('voucher', 'Voucher'),
('referral', 'Referral'), ('referral', 'Referral'),
('unknown', 'Unknown') ('unknown', 'Unknown')

View file

@ -4,9 +4,9 @@ from .models import Bill, Payment
class BillSerializer(serializers.ModelSerializer): class BillSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Bill model = Bill
fields = ['user', 'amount'] 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 = ['user', 'amount', 'source', 'timestamp'] fields = ['owner', 'amount', 'source', 'timestamp']

View file

@ -1,11 +1,14 @@
from django.shortcuts import render 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 .models import Bill, Payment
from .serializers import BillSerializer, PaymentSerializer from .serializers import BillSerializer, PaymentSerializer
from datetime import datetime
###
# Standard user views:
# to be implemented
class BalanceViewSet(viewsets.ViewSet): class BalanceViewSet(viewsets.ViewSet):
# here we return a number # here we return a number
# number = sum(payments) - sum(bills) # number = sum(payments) - sum(bills)
@ -18,10 +21,9 @@ class BalanceViewSet(viewsets.ViewSet):
pass pass
class BillViewSet(viewsets.ModelViewSet): class BillViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = BillSerializer serializer_class = BillSerializer
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
http_method_names = ['get']
def get_queryset(self): def get_queryset(self):
return Bill.objects.filter(owner=self.request.user) return Bill.objects.filter(owner=self.request.user)
@ -29,18 +31,45 @@ class BillViewSet(viewsets.ModelViewSet):
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)
class PaymentViewSet(viewsets.ModelViewSet): class PaymentViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = PaymentSerializer serializer_class = PaymentSerializer
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
http_method_names = ['get', 'post']
def get_queryset(self): 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): def create(self, request):
serializer = self.get_serializer(data=request.data) serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True) 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) 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)