From 5ab9d93484539aba688da622d35edfb3a566fbac Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sat, 20 Apr 2024 07:54:16 +0100 Subject: [PATCH] Convert prices to GBP and show Closes: #120 --- agenda/fx.py | 10 +++++----- templates/macros.html | 12 ++++++++++++ web_view.py | 16 +++++++++++++++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/agenda/fx.py b/agenda/fx.py index b96d0c3..f0ec161 100644 --- a/agenda/fx.py +++ b/agenda/fx.py @@ -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) diff --git a/templates/macros.html b/templates/macros.html index 62dad0a..a20a753 100644 --- a/templates/macros.html +++ b/templates/macros.html @@ -32,6 +32,9 @@
{% if item.price and item.currency %} {{ "{:,d}".format(item.price | int) }} {{ item.currency }} + {% if item.currency != "GBP" %} + {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP + {% endif %} {% elif item.free %} free to attend {% endif %} @@ -80,6 +83,9 @@
{% if g.user.is_authenticated and item.price and item.currency %} {{ "{:,f}".format(item.price) }} {{ item.currency }} + {% if item.currency != "GBP" %} + {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP + {% endif %} {% endif %}
{% endmacro %} @@ -118,6 +124,9 @@
{% if g.user.is_authenticated and item.price and item.currency %} {{ "{:,f}".format(item.price) }} {{ item.currency }} + {% if item.currency != "GBP" %} + {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP + {% endif %} {% endif %}
{% endmacro %} @@ -161,6 +170,9 @@
{% if g.user.is_authenticated and item.price and item.currency %} {{ "{:,f}".format(item.price) }} {{ item.currency }} + {% if item.currency != "GBP" %} + {{ "{:,.2f}".format(item.price / fx_rate[item.currency]) }} GBP + {% endif %} {% endif %}
{% endmacro %} diff --git a/web_view.py b/web_view.py index 0fc1e57..6cc6b6b 100755 --- a/web_view.py +++ b/web_view.py @@ -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), )