Wiring initial user balance
This commit is contained in:
parent
36fcff5149
commit
1dd3324275
3 changed files with 23 additions and 13 deletions
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue