Compare commits
1 Commits
4ec1cf5f28
...
64cce8a048
Author | SHA1 | Date | |
---|---|---|---|
64cce8a048 |
@ -3,7 +3,7 @@ from datetime import timedelta
|
|||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import F, Sum
|
from django.db.models import F, Sum
|
||||||
from django.template.defaultfilters import floatformat, slugify
|
from django.template.defaultfilters import slugify
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from common.time import format_duration
|
from common.time import format_duration
|
||||||
@ -131,22 +131,6 @@ class Purchase(models.Model):
|
|||||||
)
|
)
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
@property
|
|
||||||
def standardized_price(self):
|
|
||||||
return (
|
|
||||||
f"{floatformat(self.converted_price)} {self.converted_currency}"
|
|
||||||
if self.converted_price
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def num_purchases(self):
|
|
||||||
return self.games.count()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def has_one_item(self):
|
|
||||||
return self.games.count() == 1
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def standardized_name(self):
|
def standardized_name(self):
|
||||||
return self.name if self.name else self.first_game.name
|
return self.name if self.name else self.first_game.name
|
||||||
@ -156,7 +140,19 @@ class Purchase(models.Model):
|
|||||||
return self.games.first()
|
return self.games.first()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.standardized_name} ({self.num_purchases}, {self.date_purchased}, {self.standardized_price})"
|
additional_info = [
|
||||||
|
self.get_type_display() if self.type != Purchase.GAME else "",
|
||||||
|
(
|
||||||
|
f"{self.first_game.platform} version on {self.platform}"
|
||||||
|
if self.platform != self.first_game.platform
|
||||||
|
else self.platform
|
||||||
|
),
|
||||||
|
self.first_game.year_released,
|
||||||
|
self.get_ownership_type_display(),
|
||||||
|
]
|
||||||
|
return (
|
||||||
|
f"{self.first_game} ({', '.join(filter(None, map(str, additional_info)))})"
|
||||||
|
)
|
||||||
|
|
||||||
def is_game(self):
|
def is_game(self):
|
||||||
return self.type == self.GAME
|
return self.type == self.GAME
|
||||||
|
@ -32,9 +32,6 @@ def convert_prices():
|
|||||||
).first()
|
).first()
|
||||||
|
|
||||||
if not exchange_rate:
|
if not exchange_rate:
|
||||||
print(
|
|
||||||
f"Getting exchange rate from {currency_from} to {currency_to} for {year}..."
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
# this API endpoint only accepts lowercase currency string
|
# this API endpoint only accepts lowercase currency string
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
@ -42,19 +39,15 @@ def convert_prices():
|
|||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
currency_from_data = data.get(currency_from.lower())
|
rate = data[currency_from].get(currency_to)
|
||||||
rate = currency_from_data.get(currency_to.lower())
|
|
||||||
|
|
||||||
if rate:
|
if rate:
|
||||||
print(f"Got {rate}, saving...")
|
|
||||||
exchange_rate = ExchangeRate.objects.create(
|
exchange_rate = ExchangeRate.objects.create(
|
||||||
currency_from=currency_from,
|
currency_from=currency_from,
|
||||||
currency_to=currency_to,
|
currency_to=currency_to,
|
||||||
year=year,
|
year=year,
|
||||||
rate=rate,
|
rate=rate,
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
print("Could not get an exchange rate.")
|
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
print(
|
print(
|
||||||
f"Failed to fetch exchange rate for {currency_from}->{currency_to} in {year}: {e}"
|
f"Failed to fetch exchange rate for {currency_from}->{currency_to} in {year}: {e}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user