From c6a9bd4363a1b039ad2983cfb77f4b31e8bf0773 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Wed, 18 Mar 2020 14:53:26 +0100 Subject: [PATCH] Make balance a user attribute + decimalfield --- uncloud/uncloud/__init__.py | 4 +++ .../migrations/0002_auto_20200318_1343.py | 25 +++++++++++++++++++ .../migrations/0003_auto_20200318_1345.py | 23 +++++++++++++++++ uncloud/uncloud_auth/models.py | 20 ++++++++++++++- uncloud/uncloud_auth/serializers.py | 13 +++++----- uncloud/uncloud_auth/views.py | 3 ++- uncloud/uncloud_pay/models.py | 12 ++++----- 7 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 uncloud/uncloud_auth/migrations/0002_auto_20200318_1343.py create mode 100644 uncloud/uncloud_auth/migrations/0003_auto_20200318_1345.py diff --git a/uncloud/uncloud/__init__.py b/uncloud/uncloud/__init__.py index e69de29..9e2545a 100644 --- a/uncloud/uncloud/__init__.py +++ b/uncloud/uncloud/__init__.py @@ -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 diff --git a/uncloud/uncloud_auth/migrations/0002_auto_20200318_1343.py b/uncloud/uncloud_auth/migrations/0002_auto_20200318_1343.py new file mode 100644 index 0000000..ad2654f --- /dev/null +++ b/uncloud/uncloud_auth/migrations/0002_auto_20200318_1343.py @@ -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, + ), + ] diff --git a/uncloud/uncloud_auth/migrations/0003_auto_20200318_1345.py b/uncloud/uncloud_auth/migrations/0003_auto_20200318_1345.py new file mode 100644 index 0000000..31b1717 --- /dev/null +++ b/uncloud/uncloud_auth/migrations/0003_auto_20200318_1345.py @@ -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)]), + ), + ] diff --git a/uncloud/uncloud_auth/models.py b/uncloud/uncloud_auth/models.py index 3d30525..aef1e20 100644 --- a/uncloud/uncloud_auth/models.py +++ b/uncloud/uncloud_auth/models.py @@ -1,5 +1,23 @@ 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): - 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) diff --git a/uncloud/uncloud_auth/serializers.py b/uncloud/uncloud_auth/serializers.py index cd05112..3627149 100644 --- a/uncloud/uncloud_auth/serializers.py +++ b/uncloud/uncloud_auth/serializers.py @@ -1,14 +1,13 @@ from django.contrib.auth import get_user_model 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 Meta: model = get_user_model() - fields = ['username', 'email', 'balance'] + fields = ['username', 'email', 'balance', 'maximum_credit' ] - # Display current 'balance' - balance = serializers.SerializerMethodField('get_balance') - - def get_balance(self, user): - return get_balance_for_user(user) + balance = serializers.DecimalField(max_digits=AMOUNT_MAX_DIGITS, + decimal_places=AMOUNT_DECIMALS) diff --git a/uncloud/uncloud_auth/views.py b/uncloud/uncloud_auth/views.py index 40b8408..2f78e1f 100644 --- a/uncloud/uncloud_auth/views.py +++ b/uncloud/uncloud_auth/views.py @@ -10,7 +10,8 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet): obj = get_user_model().objects.all() else: # 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) return obj diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index 63f351a..a11c3c1 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -14,14 +14,14 @@ from math import ceil from datetime import timedelta from calendar import monthrange -import uncloud_pay.stripe -from uncloud_pay.helpers import beginning_of_month, end_of_month - from decimal import Decimal -# Define DecimalField properties, used to represent amounts of money. -AMOUNT_MAX_DIGITS=10 -AMOUNT_DECIMALS=2 +import uncloud_pay.stripe +from uncloud_pay.helpers import beginning_of_month, end_of_month +from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS + + + # Used to generate bill due dates. BILL_PAYMENT_DELAY=timedelta(days=10)