From 19f58211e7cc555a9280e87c10ecaa2c5c3493bb Mon Sep 17 00:00:00 2001 From: AlveElde Date: Wed, 5 Jul 2023 21:54:49 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=83=20Make=20order=20sum=20a=20decimal?= =?UTF-8?q?=20field?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bbs/api/types/users.py | 3 ++- bbs/forms.py | 4 +++- bbs/migrations/0003_alter_order_order_sum.py | 18 ++++++++++++++++++ bbs/models.py | 5 +++-- 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 bbs/migrations/0003_alter_order_order_sum.py diff --git a/bbs/api/types/users.py b/bbs/api/types/users.py index 0ddfceb..55ae3f7 100644 --- a/bbs/api/types/users.py +++ b/bbs/api/types/users.py @@ -1,5 +1,6 @@ from datetime import datetime from typing import List +from decimal import Decimal import strawberry import strawberry_django @@ -25,7 +26,7 @@ class UserProfile: balance: auto image: auto last_purchase_date: datetime - order_sum_total: float + order_sum_total: Decimal @strawberry.field() def top_months(self) -> List[TopMonth]: diff --git a/bbs/forms.py b/bbs/forms.py index 65de3d4..10a1561 100644 --- a/bbs/forms.py +++ b/bbs/forms.py @@ -2,9 +2,11 @@ from django import forms from bbs.models import InventoryTransaction, Product +MAX_DIGITS = 9 +DECIMAL_PLACES = 1 class InventoryTransactionForm(forms.ModelForm): - unit_price = forms.FloatField(required=True, localize=True) + unit_price = forms.DecimalField(max_digits=MAX_DIGITS, decimal_places=DECIMAL_PLACES, required=True, localize=True) def __init__(self, *args, **kwargs): # Only show active products diff --git a/bbs/migrations/0003_alter_order_order_sum.py b/bbs/migrations/0003_alter_order_order_sum.py new file mode 100644 index 0000000..ce55f0a --- /dev/null +++ b/bbs/migrations/0003_alter_order_order_sum.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.2 on 2023-06-28 19:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('bbs', '0002_alter_inventorytransaction_amount_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='order', + name='order_sum', + field=models.DecimalField(db_column='sum', decimal_places=1, max_digits=9), + ), + ] diff --git a/bbs/models.py b/bbs/models.py index aa884b6..a6968b5 100755 --- a/bbs/models.py +++ b/bbs/models.py @@ -2,6 +2,7 @@ import hashlib from datetime import datetime from itertools import groupby from typing import Type +from decimal import Decimal from django.conf import settings from django.db import models @@ -112,7 +113,7 @@ class Order(models.Model): blank=True, related_name="orders", ) - order_sum = models.FloatField(db_column="sum") + order_sum = models.DecimalField(db_column="sum", max_digits=MAX_DIGITS, decimal_places=DECIMAL_PLACES) created = models.DateTimeField(auto_now_add=True) objects = OrderManager() @@ -234,7 +235,7 @@ class UserProfile(models.Model): def product_order_stats(self): return OrderLine.objects.filter(order__customer__pk=self.pk).product_order_stats() - def order_sum_total(self) -> float: + def order_sum_total(self) -> Decimal: return Order.objects.filter(customer_id=self.user_id).aggregate(sum=Sum("order_sum"))["sum"] or 0 def image_url(self) -> str: