Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/fava/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def _setup_template_config(fava_app: Flask, *, incognito: bool) -> None:
fava_app.add_template_filter(template_filters.flag_to_type)
fava_app.add_template_filter(template_filters.format_currency)
fava_app.add_template_filter(template_filters.meta_items)
fava_app.add_template_filter(template_filters.price_base)
fava_app.add_template_filter(
template_filters.replace_numbers
if incognito
Expand Down
6 changes: 6 additions & 0 deletions src/fava/template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def format_currency(value: Decimal, currency: str | None = None) -> str:
return g.ledger.format_decimal(value or ZERO, currency)


def price_base(value: Decimal) -> Decimal:
"""Return a scaled base amount for displaying a price."""
exponent = max(0, round(-value.log10())) if value > ZERO else 0

@upsuper upsuper Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using round here means that the result would be consistent if the number is floating around power of 10, e.g. we can have both 1 EUR = 1.18 USD and 1 EUR = 0.97 USD, rather than 10 EUR = 9.70 USD. The latter is what it would be if it's floor here. But in fact it just pushes the boundary towards $\sqrt{10} \approx 3.16$, so if the value floats around that, the base can still switch back and forth.

return Decimal(10) ** exponent


FLAGS_TO_TYPES = {"*": "cleared", "!": "pending"}


Expand Down
4 changes: 2 additions & 2 deletions src/fava/templates/_journal_table.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% from 'macros/_commodity_macros.html' import render_num, render_amount %}
{% from 'macros/_commodity_macros.html' import render_num, render_amount, render_price %}

{% set short_type = {
'balance': 'Bal',
Expand Down Expand Up @@ -133,7 +133,7 @@
<span class='num'>{{ posting.cost.number|incognito }} {{ posting.cost.currency }}
{{- ', {}'.format(posting.cost.date) if posting.cost.date else '' }}
{{- ', "{}"'.format(posting.cost.label) if posting.cost.label else '' }}</span>
{{ render_amount(ledger, posting.price) }}
{{ render_price(ledger, posting.units.currency, posting.price) }}
</p>
{{ _render_metadata(posting.meta|meta_items) }}
</li>
Expand Down
11 changes: 11 additions & 0 deletions src/fava/templates/macros/_commodity_macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@
{% macro render_num(ledger, currency, number) -%}
<span title='{{ ledger.commodities.name(currency) }}'>{{ number|format_currency(currency)|incognito }} {{ currency }}</span>
{%- endmacro %}

{% macro render_price(ledger, base_currency, price) %}
{% if price is none or price.number is none -%}
<span class='num'></span>
{%- else -%}
{% set base = price.number|price_base %}
<span class='num' title='{{ ledger.commodities.name(price.currency) }}'>
{{- base|format_currency(base_currency)|incognito }} {{ base_currency }} = {{ (base * price.number)|format_currency(price.currency)|incognito }} {{ price.currency -}}
</span>
{%- endif %}
{% endmacro %}
10 changes: 10 additions & 0 deletions tests/test_template_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fava.template_filters import basename
from fava.template_filters import format_currency
from fava.template_filters import passthrough_numbers
from fava.template_filters import price_base
from fava.template_filters import replace_numbers

if TYPE_CHECKING: # pragma: no cover
Expand All @@ -29,6 +30,15 @@ def test_format_currency(app: Flask) -> None:
assert format_currency(Decimal("2.12")) == "2.12"


def test_price_base() -> None:
assert price_base(Decimal(2)) == Decimal(1)
assert price_base(Decimal("0.5")) == Decimal(1)
assert price_base(Decimal("0.2")) == Decimal(10)
assert price_base(Decimal("0.02")) == Decimal(100)
assert price_base(Decimal("0.000123")) == Decimal(10000)
assert price_base(Decimal()) == Decimal(1)


def test_basename() -> None:
"""Get the basename of a file path."""
assert basename(__file__) == "test_template_filters.py"
17 changes: 17 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ def test_render_amount(app: Flask, example_ledger: FavaLedger) -> None:
assert macro(example_ledger, create.amount("10 TEST")) == res


def test_render_price(app: Flask, example_ledger: FavaLedger) -> None:
with app.test_request_context("/long-example/"):
app.preprocess_request()
macro = get_template_attribute(
"macros/_commodity_macros.html",
"render_price",
)
res = "<span class='num'></span>"
assert macro(example_ledger, "USD", None) == res
res = " <span class='num' title='TEST'>100.00 USD = 2.00 TEST</span>"
assert macro(example_ledger, "USD", create.amount("0.02 TEST")) == res
res = (
" <span class='num' title='US Dollar'>1.00 TEST = 2.00 USD</span>"
)
assert macro(example_ledger, "TEST", create.amount("2 USD")) == res


def test_account_name(app: Flask, example_ledger: FavaLedger) -> None:
with app.test_request_context("/long-example/"):
app.preprocess_request()
Expand Down