Fix update_converted_prices
This commit is contained in:
parent
34b2c8c751
commit
4a0f5761e8
|
@ -2,20 +2,28 @@ import requests
|
||||||
|
|
||||||
from games.models import ExchangeRate, Purchase
|
from games.models import ExchangeRate, Purchase
|
||||||
|
|
||||||
|
|
||||||
def update_converted_prices():
|
|
||||||
purchases = Purchase.objects.filter(
|
|
||||||
converted_price__isnull=True, converted_currency__isnull=True
|
|
||||||
)
|
|
||||||
|
|
||||||
for purchase in purchases:
|
|
||||||
if purchase.price == 0:
|
|
||||||
continue
|
|
||||||
year = purchase.date_purchased.year
|
|
||||||
currency_from = purchase.price_currency
|
|
||||||
# fixme: save preferred currency in user model
|
# fixme: save preferred currency in user model
|
||||||
currency_to = "CZK"
|
currency_to = "CZK"
|
||||||
|
|
||||||
|
|
||||||
|
def save_converted_info(purchase, converted_price, converted_currency):
|
||||||
|
purchase.converted_price = converted_price
|
||||||
|
purchase.converted_currency = converted_currency
|
||||||
|
purchase.save()
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
)
|
||||||
|
|
||||||
|
for purchase in purchases:
|
||||||
|
year = purchase.date_purchased.year
|
||||||
|
currency_from = purchase.price_currency
|
||||||
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()
|
||||||
|
@ -23,12 +31,11 @@ def update_converted_prices():
|
||||||
if not exchange_rate:
|
if not exchange_rate:
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
f"https://api.exchangeratesapi.io/{year}-01-01",
|
f"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@{year}-01-01/v1/currencies/{currency_from}.json"
|
||||||
params={"base": currency_to, "symbols": "currency_from"},
|
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
rate = data["rates"].get(currency_from)
|
rate = data[currency_from].get(currency_to)
|
||||||
|
|
||||||
if rate:
|
if rate:
|
||||||
exchange_rate = ExchangeRate.objects.create(
|
exchange_rate = ExchangeRate.objects.create(
|
||||||
|
@ -39,10 +46,9 @@ def update_converted_prices():
|
||||||
)
|
)
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
print(
|
print(
|
||||||
f"Failed to fetch exchange rate for {currency_from} in {year}: {e}"
|
f"Failed to fetch exchange rate for {currency_from}->{currency_to} in {year}: {e}"
|
||||||
)
|
)
|
||||||
if exchange_rate:
|
if exchange_rate:
|
||||||
converted_price = purchase.price * exchange_rate.rate
|
save_converted_info(
|
||||||
purchase.converted_price = converted_price
|
purchase, purchase.price * exchange_rate.rate, currency_to
|
||||||
purchase.converted_currency = currency_to
|
)
|
||||||
purchase.save()
|
|
||||||
|
|
Loading…
Reference in New Issue