Convert prices to GBP and show

Closes: #120
This commit is contained in:
Edward Betts 2024-04-20 07:54:16 +01:00
parent dbc12adb3d
commit 5ab9d93484
3 changed files with 32 additions and 6 deletions

View file

@ -44,11 +44,11 @@ async def get_gbpusd(config: flask.config.Config) -> Decimal:
return typing.cast(Decimal, 1 / data["quotes"]["USDGBP"])
def get_exchange_rates(config) -> dict[str, Decimal]:
def get_rates(config: flask.config.Config) -> dict[str, Decimal]:
"""Get current values of exchange rates for a list of currencies against GBP."""
currencies = config.CURRENCIES
access_key = config.EXCHANGERATE_ACCESS_KEY
data_dir = config.DATA_DIR
currencies = config["CURRENCIES"]
access_key = config["EXCHANGERATE_ACCESS_KEY"]
data_dir = config["DATA_DIR"]
now = datetime.now()
now_str = now.strftime("%Y-%m-%d_%H:%M")
@ -65,7 +65,7 @@ def get_exchange_rates(config) -> dict[str, Decimal]:
recent = datetime.strptime(recent_filename[:16], "%Y-%m-%d_%H:%M")
delta = now - recent
if delta < timedelta(hours=6):
if delta < timedelta(hours=12):
full_path = os.path.join(fx_dir, recent_filename)
with open(full_path) as file:
data = json.load(file, parse_float=Decimal)

View file

@ -32,6 +32,9 @@
<div class="grid-item text-end">
{% if item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,d}".format(item.price | int) }} {{ item.currency }}</span>
{% if item.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% elif item.free %}
<span class="badge bg-success text-nowrap">free to attend</span>
{% endif %}
@ -80,6 +83,9 @@
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% endmacro %}
@ -118,6 +124,9 @@
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% endmacro %}
@ -161,6 +170,9 @@
<div class="grid-item text-end">
{% if g.user.is_authenticated and item.price and item.currency %}
<span class="badge bg-info text-nowrap">{{ "{:,f}".format(item.price) }} {{ item.currency }}</span>
{% if item.currency != "GBP" %}
<span class="badge bg-info text-nowrap">{{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP</span>
{% endif %}
{% endif %}
</div>
{% endmacro %}

View file

@ -2,6 +2,7 @@
"""Web page to show upcoming events."""
import decimal
import inspect
import operator
import os.path
@ -17,6 +18,7 @@ import yaml
import agenda.data
import agenda.error_mail
import agenda.fx
import agenda.holidays
import agenda.thespacedevs
import agenda.trip
@ -125,7 +127,12 @@ def travel_list() -> str:
if all("distance" in leg for leg in train["legs"]):
train["distance"] = sum(leg["distance"] for leg in train["legs"])
return flask.render_template("travel.html", flights=flights, trains=trains)
return flask.render_template(
"travel.html",
flights=flights,
trains=trains,
fx_rate=agenda.fx.get_rates(app.config),
)
def as_date(d: date | datetime) -> date:
@ -151,6 +158,10 @@ def conference_list() -> str:
conf["start_date"] = as_date(conf["start"])
conf["end_date"] = as_date(conf["end"])
price = conf.get("price")
if price:
conf["price"] = decimal.Decimal(price)
key = (conf["start"], conf["name"])
if this_trip := conference_trip_lookup.get(key):
conf["linked_trip"] = this_trip
@ -172,6 +183,7 @@ def conference_list() -> str:
future=future,
today=today,
get_country=agenda.get_country,
fx_rate=agenda.fx.get_rates(app.config),
)
@ -218,6 +230,7 @@ def accommodation_list() -> str:
total_nights_2024=total_nights_2024,
nights_abroad_2024=nights_abroad_2024,
get_country=agenda.get_country,
fx_rate=agenda.fx.get_rates(app.config),
)
@ -263,6 +276,7 @@ def trip_list() -> str:
today=today,
get_country=agenda.get_country,
format_list_with_ampersand=format_list_with_ampersand,
fx_rate=agenda.fx.get_rates(app.config),
)