From 0759ad08041fa773c5cf39e221a4c030e5e55ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Fri, 4 Oct 2024 11:36:46 +0200 Subject: [PATCH] make purchase price a float --- common/utils.py | 4 ++++ games/migrations/0038_alter_purchase_price.py | 18 ++++++++++++++++++ games/models.py | 2 +- games/views/game.py | 4 ++-- games/views/purchase.py | 3 ++- 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 games/migrations/0038_alter_purchase_price.py diff --git a/common/utils.py b/common/utils.py index 15d117c..30bade1 100644 --- a/common/utils.py +++ b/common/utils.py @@ -60,3 +60,7 @@ def generate_split_ranges( except IndexError: end = len(value_list) yield (value_list[start], value_list[end - 1]) + + +def format_float_or_int(number: int | float): + return int(number) if float(number).is_integer() else f"{number:03.2f}" diff --git a/games/migrations/0038_alter_purchase_price.py b/games/migrations/0038_alter_purchase_price.py new file mode 100644 index 0000000..1c6f382 --- /dev/null +++ b/games/migrations/0038_alter_purchase_price.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.1 on 2024-10-04 09:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('games', '0037_platform_icon'), + ] + + operations = [ + migrations.AlterField( + model_name='purchase', + name='price', + field=models.FloatField(default=0), + ), + ] diff --git a/games/models.py b/games/models.py index 165b664..143f9bd 100644 --- a/games/models.py +++ b/games/models.py @@ -111,7 +111,7 @@ class Purchase(models.Model): date_finished = models.DateField(blank=True, null=True) date_dropped = models.DateField(blank=True, null=True) infinite = models.BooleanField(default=False) - price = models.IntegerField(default=0) + price = models.FloatField(default=0) price_currency = models.CharField(max_length=3, default="USD") ownership_type = models.CharField( max_length=2, choices=OWNERSHIP_TYPES, default=DIGITAL diff --git a/games/views/game.py b/games/views/game.py index 9e8401e..db05164 100644 --- a/games/views/game.py +++ b/games/views/game.py @@ -25,7 +25,7 @@ from common.time import ( local_strftime, timeformat, ) -from common.utils import safe_division, truncate +from common.utils import format_float_or_int, safe_division, truncate from games.forms import GameForm from games.models import Edition, Game, Purchase, Session from games.views.general import use_custom_redirect @@ -247,7 +247,7 @@ def view_game(request: HttpRequest, game_id: int) -> HttpResponse: ), purchase.get_type_display(), purchase.date_purchased.strftime(dateformat), - f"{purchase.price} {purchase.price_currency}", + f"{format_float_or_int(purchase.price)} {purchase.price_currency}", render_to_string( "cotton/button_group.html", { diff --git a/games/views/purchase.py b/games/views/purchase.py index 76be33a..7ca1f94 100644 --- a/games/views/purchase.py +++ b/games/views/purchase.py @@ -15,6 +15,7 @@ from django.utils import timezone from common.components import A, Button, Icon, LinkedNameWithPlatformIcon from common.time import dateformat +from common.utils import format_float_or_int from games.forms import PurchaseForm from games.models import Edition, Purchase from games.views.general import use_custom_redirect @@ -65,7 +66,7 @@ def list_purchases(request: HttpRequest) -> HttpResponse: platform=purchase.platform, ), purchase.get_type_display(), - purchase.price, + format_float_or_int(purchase.price), purchase.price_currency, purchase.infinite, purchase.date_purchased.strftime(dateformat),