From 23ed467d37a0b1949c9cd4ef5616058d5448e030 Mon Sep 17 00:00:00 2001 From: Glandos Date: Thu, 21 May 2020 21:31:24 +0200 Subject: [PATCH] Replace currencyformat_nc with currency filter (#625) --- ihatemoney/run.py | 8 ++++++-- ihatemoney/templates/list_bills.html | 2 +- ihatemoney/templates/settle_bills.html | 2 +- ihatemoney/templates/sidebar_table_layout.html | 2 +- ihatemoney/templates/statistics.html | 6 +++--- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/ihatemoney/run.py b/ihatemoney/run.py index e084e5bc..15e295b8 100644 --- a/ihatemoney/run.py +++ b/ihatemoney/run.py @@ -6,6 +6,7 @@ from flask import Flask, g, render_template, request, session from flask_babel import Babel, format_currency from flask_mail import Mail from flask_migrate import Migrate, stamp, upgrade +from jinja2 import contextfilter from werkzeug.middleware.proxy_fix import ProxyFix from ihatemoney import default_settings @@ -155,7 +156,10 @@ def create_app( # Undocumented currencyformat filter from flask_babel is forwarding to Babel format_currency # We overwrite it to remove the currency sign ¤ when there is no currency - def currencyformat_nc(number, currency, *args, **kwargs): + @contextfilter + def currency(context, number, currency=None, *args, **kwargs): + if currency is None: + currency = context.get("g").project.default_currency """ Same as flask_babel.Babel.currencyformat, but without the "no currency ¤" sign when there is no currency. @@ -167,7 +171,7 @@ def create_app( **kwargs ).strip() - app.jinja_env.filters["currencyformat_nc"] = currencyformat_nc + app.jinja_env.filters["currency"] = currency @babel.localeselector def get_locale(): diff --git a/ihatemoney/templates/list_bills.html b/ihatemoney/templates/list_bills.html index 1f4235bb..487e70eb 100644 --- a/ihatemoney/templates/list_bills.html +++ b/ihatemoney/templates/list_bills.html @@ -1,7 +1,7 @@ {% extends "sidebar_table_layout.html" %} {%- macro bill_amount(bill, currency=bill.original_currency, amount=bill.amount) %} - {{ amount|currencyformat_nc(currency) }} ({{ _("%(amount)s each", amount=bill.pay_each_default(amount)|currencyformat_nc(currency)) }}) + {{ amount|currency(currency) }} ({{ _("%(amount)s each", amount=bill.pay_each_default(amount)|currency(currency)) }}) {% endmacro -%} {% block title %} - {{ g.project.name }}{% endblock %} diff --git a/ihatemoney/templates/settle_bills.html b/ihatemoney/templates/settle_bills.html index a9b0dbc2..601156c6 100644 --- a/ihatemoney/templates/settle_bills.html +++ b/ihatemoney/templates/settle_bills.html @@ -15,7 +15,7 @@ {{ bill.ower }} {{ bill.receiver }} - {{ bill.amount|currencyformat_nc(g.project.default_currency) }} + {{ bill.amount|currency }} {% endfor %} diff --git a/ihatemoney/templates/sidebar_table_layout.html b/ihatemoney/templates/sidebar_table_layout.html index d616e684..b25a3d68 100644 --- a/ihatemoney/templates/sidebar_table_layout.html +++ b/ihatemoney/templates/sidebar_table_layout.html @@ -37,7 +37,7 @@ {%- endif %} {%- endif %} - {% if balance[member.id] | round(2) > 0 %}+{% endif %}{{ balance[member.id]|currencyformat_nc(g.project.default_currency) }} + {% if balance[member.id] | round(2) > 0 %}+{% endif %}{{ balance[member.id]|currency }} {%- endfor %} diff --git a/ihatemoney/templates/statistics.html b/ihatemoney/templates/statistics.html index 3b0a9dd5..9e7a10de 100644 --- a/ihatemoney/templates/statistics.html +++ b/ihatemoney/templates/statistics.html @@ -15,8 +15,8 @@ {% for stat in members_stats|sort(attribute='member.name') %} {{ stat.member.name }} - {{ stat.paid|currencyformat_nc(g.project.default_currency) }} - {{ stat.spent|currencyformat_nc(g.project.default_currency) }} + {{ stat.paid|currency }} + {{ stat.spent|currency }} {% endfor %} @@ -28,7 +28,7 @@ {% for month in months %} {{ _(month.strftime("%B")) }} {{ month.year }} - {{ monthly_stats[month.year][month.month]|currencyformat_nc(g.project.default_currency) }} + {{ monthly_stats[month.year][month.month]|currency }} {% endfor %}