From 013375106b7099ae84b7a0df5e5e3fff787bcb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Mon, 11 Nov 2024 00:07:03 +0100 Subject: [PATCH] Convert everything to chosen currency --- games/convert_price.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/games/convert_price.py b/games/convert_price.py index 238657f..e883794 100644 --- a/games/convert_price.py +++ b/games/convert_price.py @@ -4,6 +4,7 @@ from games.models import ExchangeRate, Purchase # fixme: save preferred currency in user model currency_to = "CZK" +currency_to = currency_to.lower() def save_converted_info(purchase, converted_price, converted_currency): @@ -13,17 +14,18 @@ def save_converted_info(purchase, converted_price, converted_currency): def update_converted_prices(): - purchases = ( - Purchase.objects.filter( - converted_price__isnull=True, converted_currency__isnull=True - ) - .exclude(price_currency__iexact=currency_to) - .exclude(price=0) + purchases = Purchase.objects.filter( + converted_price__isnull=True, converted_currency__isnull=True ) for purchase in purchases: + if purchase.price_currency.lower() == currency_to or purchase.price == 0: + save_converted_info( + purchase, purchase.price, purchase.price_currency.lower() + ) + continue year = purchase.date_purchased.year - currency_from = purchase.price_currency + currency_from = purchase.price_currency.lower() exchange_rate = ExchangeRate.objects.filter( currency_from=currency_from, currency_to=currency_to, year=year ).first()