From be9940cdc03d74dbae84d3e76acf9cb8a35293c1 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Mon, 17 Nov 2025 15:59:39 +0300 Subject: [PATCH] Features: 1) Added `arith` template tags for `sub` and `neg` operations to support arithmetic in templates; Fixes: 1) Replaced `add:-X` with `sub:X` in template logic for consistent arithmetic; Extra: 1) Added `arith` to template load context; 2) Introduced `_to_float` helper for safe type conversion; 3) Used `suppress` to handle invalid conversions gracefully. --- engine/core/templates/admin/index.html | 6 +++--- engine/core/templatetags/arith.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 engine/core/templatetags/arith.py diff --git a/engine/core/templates/admin/index.html b/engine/core/templates/admin/index.html index 510ae1f0..3527f3c9 100644 --- a/engine/core/templates/admin/index.html +++ b/engine/core/templates/admin/index.html @@ -1,5 +1,5 @@ {% extends 'admin/base.html' %} -{% load i18n unfold static %} +{% load i18n unfold static arith %} {% block title %} {% if subtitle %} @@ -68,7 +68,7 @@ {% endcomponent %} {% if total and total > 0 %} {% with net=revenue_net_30|default:0 %} - {% with tax_amt=gross|add:-net %} + {% with tax_amt=gross|sub:net %} {% with returns_capped=returns %} {% if returns > gross %} {% with returns_capped=gross %}{% endwith %} @@ -77,7 +77,7 @@ {% if tax_amt_pos < 0 %} {% with tax_amt_pos=0 %}{% endwith %} {% endif %} - {% with net_for_pie=gross|add:-tax_amt_pos|add:-returns_capped %} + {% with net_for_pie=gross|sub:tax_amt_pos|sub:returns_capped %} {% if net_for_pie < 0 %} {% with net_for_pie=0 %}{% endwith %} {% endif %} diff --git a/engine/core/templatetags/arith.py b/engine/core/templatetags/arith.py new file mode 100644 index 00000000..fbd9dfa1 --- /dev/null +++ b/engine/core/templatetags/arith.py @@ -0,0 +1,22 @@ +from contextlib import suppress + +from django import template + +register = template.Library() + + +def _to_float(val: object) -> float: + conv: float = 0.0 + with suppress(Exception): + return float(val) # type: ignore [arg-type] + return conv + + +@register.filter(name="sub") +def sub(value: object, arg: object) -> float: + return _to_float(value) - _to_float(arg) + + +@register.filter(name="neg") +def neg(value: object) -> float: + return -_to_float(value)