From 1dd33242756e469afb2779f7abcf11eb9d39d72b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Thu, 27 Feb 2020 15:50:46 +0100 Subject: [PATCH] Wiring initial user balance --- uncloud/uncloud_pay/models.py | 11 ++++++----- uncloud/uncloud_pay/serializers.py | 17 ++++++++++++++--- uncloud/uncloud_pay/views.py | 8 +++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index 643361a..c824a00 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -9,8 +9,7 @@ AMOUNT_DECIMALS=2 class Bill(models.Model): owner = models.ForeignKey(get_user_model(), - on_delete=models.CASCADE, - editable=False) + on_delete=models.CASCADE) creation_date = models.DateTimeField() starting_date = models.DateTimeField() @@ -23,7 +22,7 @@ class Bill(models.Model): @property def amount(self): # iterate over all related orders - pass + return 20 class Order(models.Model): @@ -82,6 +81,9 @@ class PaymentMethod(models.Model): description = models.TextField() primary = models.BooleanField(default=True) + def charge(self, amount): + pass + class Meta: unique_together = [['owner', 'primary']] @@ -89,8 +91,7 @@ class Payment(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) owner = models.ForeignKey(get_user_model(), - on_delete=models.CASCADE, - editable=False) + on_delete=models.CASCADE) amount = models.DecimalField( default=0.0, diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index 93a3031..040c78a 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -2,6 +2,8 @@ from django.contrib.auth import get_user_model from rest_framework import serializers from .models import * +from functools import reduce + class BillSerializer(serializers.ModelSerializer): class Meta: model = Bill @@ -26,7 +28,16 @@ class OrderSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer): class Meta: model = get_user_model() - fields = ['username', 'email'] + fields = ['username', 'email', 'balance'] - def get_balance(self, obj): - return 666 + # Display current 'balance' + balance = serializers.SerializerMethodField('get_balance') + def __sum_balance(self, entries): + return reduce(lambda acc, entry: acc + entry.amount, entries, 0) + + def get_balance(self, user): + bills = self.__sum_balance(Bill.objects.filter(owner=user)) + payments = self.__sum_balance(Payment.objects.filter(owner=user)) + balance = payments - bills + + return balance diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py index 0b39ff3..ea3cca7 100644 --- a/uncloud/uncloud_pay/views.py +++ b/uncloud/uncloud_pay/views.py @@ -4,6 +4,8 @@ from rest_framework import viewsets, permissions, status from rest_framework.response import Response from rest_framework.decorators import action +import json + from .models import * from .serializers import * from datetime import datetime @@ -54,10 +56,6 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet): 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) - class PaymentMethodViewSet(viewsets.ModelViewSet): serializer_class = PaymentMethodSerializer permission_classes = [permissions.IsAuthenticated] @@ -104,7 +102,7 @@ class AdminBillViewSet(viewsets.ModelViewSet): def create(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) - serializer.save(created_at=datetime.now()) + serializer.save(creation_date=datetime.now()) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)