Implement converting prices #79

Merged
lukas merged 22 commits from converted_prices into main 2024-11-11 16:36:58 +00:00
1 changed files with 9 additions and 7 deletions
Showing only changes of commit 013375106b - Show all commits

View File

@ -4,6 +4,7 @@ from games.models import ExchangeRate, Purchase
# fixme: save preferred currency in user model # fixme: save preferred currency in user model
currency_to = "CZK" currency_to = "CZK"
currency_to = currency_to.lower()
def save_converted_info(purchase, converted_price, converted_currency): 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(): def update_converted_prices():
purchases = ( purchases = Purchase.objects.filter(
Purchase.objects.filter(
converted_price__isnull=True, converted_currency__isnull=True converted_price__isnull=True, converted_currency__isnull=True
) )
.exclude(price_currency__iexact=currency_to)
.exclude(price=0)
)
for purchase in purchases: 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 year = purchase.date_purchased.year
currency_from = purchase.price_currency currency_from = purchase.price_currency.lower()
exchange_rate = ExchangeRate.objects.filter( exchange_rate = ExchangeRate.objects.filter(
currency_from=currency_from, currency_to=currency_to, year=year currency_from=currency_from, currency_to=currency_to, year=year
).first() ).first()