Make balance a user attribute + decimalfield

This commit is contained in:
Nico Schottelius 2020-03-18 14:53:26 +01:00
parent cd01f62fde
commit c6a9bd4363
7 changed files with 85 additions and 15 deletions

View file

@ -0,0 +1,4 @@
# Define DecimalField properties, used to represent amounts of money.
# Used in pay and auth
AMOUNT_MAX_DIGITS=10
AMOUNT_DECIMALS=2

View file

@ -0,0 +1,25 @@
# Generated by Django 3.0.3 on 2020-03-18 13:43
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_auth', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='user',
name='amount',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AddField(
model_name='user',
name='maximum_credit',
field=models.FloatField(default=0),
preserve_default=False,
),
]

View file

@ -0,0 +1,23 @@
# Generated by Django 3.0.3 on 2020-03-18 13:45
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_auth', '0002_auto_20200318_1343'),
]
operations = [
migrations.RemoveField(
model_name='user',
name='amount',
),
migrations.AlterField(
model_name='user',
name='maximum_credit',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10, validators=[django.core.validators.MinValueValidator(0)]),
),
]

View file

@ -1,5 +1,23 @@
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.db import models
from django.core.validators import MinValueValidator
from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
from uncloud_pay.models import get_balance_for_user
class User(AbstractUser): class User(AbstractUser):
pass """
We use the standard user and add a maximum negative credit that is allowed
to be accumulated
"""
maximum_credit = models.DecimalField(
default=0.0,
max_digits=AMOUNT_MAX_DIGITS,
decimal_places=AMOUNT_DECIMALS,
validators=[MinValueValidator(0)])
@property
def balance(self):
return get_balance_for_user(self)

View file

@ -1,14 +1,13 @@
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from rest_framework import serializers from rest_framework import serializers
from uncloud_pay.models import get_balance_for_user
from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
class UserSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = get_user_model() model = get_user_model()
fields = ['username', 'email', 'balance'] fields = ['username', 'email', 'balance', 'maximum_credit' ]
# Display current 'balance' balance = serializers.DecimalField(max_digits=AMOUNT_MAX_DIGITS,
balance = serializers.SerializerMethodField('get_balance') decimal_places=AMOUNT_DECIMALS)
def get_balance(self, user):
return get_balance_for_user(user)

View file

@ -10,7 +10,8 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet):
obj = get_user_model().objects.all() obj = get_user_model().objects.all()
else: else:
# This is a bit stupid: we have a user, we create a queryset by # This is a bit stupid: we have a user, we create a queryset by
# matching on the username. # matching on the username. But I don't know a "nicer" way.
# Nico, 2020-03-18
obj = get_user_model().objects.filter(username=self.request.user.username) obj = get_user_model().objects.filter(username=self.request.user.username)
return obj return obj

View file

@ -14,14 +14,14 @@ from math import ceil
from datetime import timedelta from datetime import timedelta
from calendar import monthrange from calendar import monthrange
import uncloud_pay.stripe
from uncloud_pay.helpers import beginning_of_month, end_of_month
from decimal import Decimal from decimal import Decimal
# Define DecimalField properties, used to represent amounts of money. import uncloud_pay.stripe
AMOUNT_MAX_DIGITS=10 from uncloud_pay.helpers import beginning_of_month, end_of_month
AMOUNT_DECIMALS=2 from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
# Used to generate bill due dates. # Used to generate bill due dates.
BILL_PAYMENT_DELAY=timedelta(days=10) BILL_PAYMENT_DELAY=timedelta(days=10)