From 4a812cee5fc259efac50b72a44e0cd62839026b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Fri, 15 Nov 2024 18:03:08 +0100 Subject: [PATCH] consistently format prices everywhere --- common/components.py | 11 +++++++++++ games/static/base.css | 4 ++++ games/templates/cotton/popover.html | 4 +++- games/templates/stats.html | 8 +++++--- games/views/game.py | 5 +++-- games/views/purchase.py | 4 ++-- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/common/components.py b/common/components.py index 527ecd7..09656fb 100644 --- a/common/components.py +++ b/common/components.py @@ -3,6 +3,7 @@ from string import ascii_lowercase from typing import Any, Callable from django.template import TemplateDoesNotExist +from django.template.defaultfilters import floatformat from django.template.loader import render_to_string from django.urls import NoReverseMatch, reverse from django.utils.safestring import SafeText, mark_safe @@ -50,6 +51,7 @@ def randomid(seed: str = "", length: int = 10) -> str: def Popover( popover_content: str, wrapped_content: str = "", + wrapped_classes: str = "", children: list[HTMLTag] = [], attributes: list[HTMLAttribute] = [], ) -> str: @@ -62,6 +64,7 @@ def Popover( ("id", id), ("wrapped_content", wrapped_content), ("popover_content", popover_content), + ("wrapped_classes", wrapped_classes), ], children=children, template="cotton/popover.html", @@ -193,3 +196,11 @@ def NameWithPlatformIcon(name: str, platform: str) -> SafeText: ) return mark_safe(content) + + +def PurchasePrice(purchase) -> str: + return Popover( + popover_content=f"{floatformat(purchase.price)} {purchase.price_currency}", + wrapped_content=f"{floatformat(purchase.converted_price)} {purchase.converted_currency}", + wrapped_classes="underline decoration-dotted", + ) diff --git a/games/static/base.css b/games/static/base.css index 1132bfd..83fded9 100644 --- a/games/static/base.css +++ b/games/static/base.css @@ -2192,6 +2192,10 @@ input:checked + .toggle-bg { text-decoration-color: #64748b; } +.decoration-dotted { + text-decoration-style: dotted; +} + .opacity-0 { opacity: 0; } diff --git a/games/templates/cotton/popover.html b/games/templates/cotton/popover.html index 7806a71..c4895f1 100644 --- a/games/templates/cotton/popover.html +++ b/games/templates/cotton/popover.html @@ -1,8 +1,10 @@ -{{ wrapped_content|default:slot }} +{{ wrapped_content|default:slot }} diff --git a/games/templates/stats.html b/games/templates/stats.html index af2c012..4a204be 100644 --- a/games/templates/stats.html +++ b/games/templates/stats.html @@ -142,7 +142,9 @@ Spendings ({{ total_spent_currency }}) - {{ total_spent }} ({{ spent_per_game }}/game) + + {{ total_spent | floatformat }} ({{ spent_per_game | floatformat }}/game) + @@ -253,7 +255,7 @@ {% for purchase in purchased_unfinished %} {% partial purchase-name %} - {{ purchase.converted_price }} + {{ purchase.converted_price | floatformat }} {{ purchase.date_purchased | date:"d/m/Y" }} {% endfor %} @@ -274,7 +276,7 @@ {% for purchase in all_purchased_this_year %} {% partial purchase-name %} - {{ purchase.converted_price }} + {{ purchase.converted_price | floatformat }} {{ purchase.date_purchased | date:"d/m/Y" }} {% endfor %} diff --git a/games/views/game.py b/games/views/game.py index 4b1a76c..d55fb14 100644 --- a/games/views/game.py +++ b/games/views/game.py @@ -16,6 +16,7 @@ from common.components import ( NameWithPlatformIcon, Popover, PopoverTruncated, + PurchasePrice, ) from common.time import ( dateformat, @@ -25,7 +26,7 @@ from common.time import ( local_strftime, timeformat, ) -from common.utils import format_float_or_int, safe_division, truncate +from common.utils import 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 +248,7 @@ def view_game(request: HttpRequest, game_id: int) -> HttpResponse: ), purchase.get_type_display(), purchase.date_purchased.strftime(dateformat), - f"{format_float_or_int(purchase.price)} {purchase.price_currency}", + PurchasePrice(purchase), render_to_string( "cotton/button_group.html", { diff --git a/games/views/purchase.py b/games/views/purchase.py index 005f08f..1b2ca12 100644 --- a/games/views/purchase.py +++ b/games/views/purchase.py @@ -9,13 +9,13 @@ from django.http import ( HttpResponseRedirect, ) from django.shortcuts import get_object_or_404, redirect, render +from django.template.defaultfilters import floatformat from django.template.loader import render_to_string from django.urls import reverse 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 @@ -66,7 +66,7 @@ def list_purchases(request: HttpRequest) -> HttpResponse: platform=purchase.platform, ), purchase.get_type_display(), - format_float_or_int(purchase.price), + floatformat(purchase.price), purchase.price_currency, purchase.infinite, purchase.date_purchased.strftime(dateformat),