From 45b47dd1bac80335fece6ebfe34d41f0843ebadd Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Tue, 2 Jan 2024 11:11:05 +0100
Subject: [PATCH] Move templatetags to history templatetags

---
 nummi/history/templates/history/plot.html    |  3 +-
 nummi/history/templatetags/history_extras.py | 78 ++++++++++++++++++++
 nummi/main/templatetags/main_extras.py       | 74 -------------------
 3 files changed, 80 insertions(+), 75 deletions(-)
 create mode 100644 nummi/history/templatetags/history_extras.py

diff --git a/nummi/history/templates/history/plot.html b/nummi/history/templates/history/plot.html
index 8d9bad4..49a52f2 100644
--- a/nummi/history/templates/history/plot.html
+++ b/nummi/history/templates/history/plot.html
@@ -1,4 +1,5 @@
 {% load main_extras %}
+{% load history_extras %}
 {% load i18n %}
 <div class="plot">
     <table class="full-width">
@@ -58,7 +59,7 @@
                         {% endif %}
                         {% if m %}
                             <td class="{% if m.sum > 0 %}p{% else %}m{% endif %}"
-                                style="opacity: {% opacity m.sum history.max.sum %}"></td>
+                                style="opacity: {% calendar_opacity m.sum history.max.sum %}"></td>
                         {% else %}
                             <td></td>
                         {% endif %}
diff --git a/nummi/history/templatetags/history_extras.py b/nummi/history/templatetags/history_extras.py
new file mode 100644
index 0000000..1efb7ec
--- /dev/null
+++ b/nummi/history/templatetags/history_extras.py
@@ -0,0 +1,78 @@
+import math
+
+from django import template
+from django.template.defaultfilters import date
+from django.urls import reverse
+from django.utils.safestring import mark_safe
+from main.templatetags.main_extras import pmrvalue, remix
+
+register = template.Library()
+
+
+@register.simple_tag
+def calendar_opacity(v, vmax):
+    return f"{math.sin(math.fabs(v/vmax)*math.pi/2):.3f}"
+
+
+@register.simple_tag
+def empty_calendar_cells(n):
+    return mark_safe(n * "<td></td>")
+
+
+@register.simple_tag
+def empty_calendar_cells_start(n):
+    return empty_calendar_cells(n - 1)
+
+
+@register.simple_tag
+def empty_calendar_cells_end(n):
+    return empty_calendar_cells(12 - n)
+
+
+@register.simple_tag
+def up_down_icon(val):
+    if val > 0:
+        return remix("arrow-up-s", "green")
+    elif val < 0:
+        return remix("arrow-down-s", "red")
+
+
+@register.simple_tag
+def month_url(month, account=None, category=None):
+    url_name = "transaction_month"
+    url_params = {"year": month.year, "month": month.month}
+
+    if account:
+        url_name = "account_" + url_name
+        url_params |= {"account": account.pk}
+    elif category:
+        url_name = "category_" + url_name
+        url_params |= {"category": category.pk}
+
+    url = reverse(url_name, kwargs=url_params)
+    return mark_safe(f"""<a href="{url}">{ date(month, "Y-m") }</a>""")
+
+
+@register.simple_tag
+def plot_bar(s, sum_pm, s_max):
+    _res = ""
+
+    if sum_pm:
+        _w = abs(sum_pm / s_max)
+        _res += f"""<div style="width: {_w:.1%}"></div>"""
+    if sum_pm is not None and s * sum_pm > 0:
+        _w = abs(s / s_max)
+        _res += (
+            f"""<div class="tot" style="width: {_w:.1%}">"""
+            f"""<span>{pmrvalue(s)}</span></div>"""
+        )
+
+    return mark_safe(_res)
+
+
+@register.simple_tag
+def calendar_head():
+    months = range(1, 13)
+    th = (f"""<th>{month:02d}</th>""" for month in months)
+
+    return mark_safe("".join(th))
diff --git a/nummi/main/templatetags/main_extras.py b/nummi/main/templatetags/main_extras.py
index 8fca38e..a85d8db 100644
--- a/nummi/main/templatetags/main_extras.py
+++ b/nummi/main/templatetags/main_extras.py
@@ -1,10 +1,5 @@
-import datetime
-import math
-
 from django import template
-from django.template.defaultfilters import date
 from django.templatetags.static import static
-from django.urls import reverse
 from django.utils import formats
 from django.utils.safestring import mark_safe
 
@@ -72,72 +67,3 @@ def css(href):
     return mark_safe(
         f"""<link rel="stylesheet" href="{static(href)}" type="text/css">"""
     )
-
-
-@register.simple_tag
-def opacity(v, vmax):
-    return f"{math.sin(math.fabs(v/vmax)*math.pi/2):.3f}"
-
-
-@register.simple_tag
-def empty_calendar_cells(n):
-    return mark_safe(n * "<td></td>")
-
-
-@register.simple_tag
-def empty_calendar_cells_start(n):
-    return empty_calendar_cells(n - 1)
-
-
-@register.simple_tag
-def empty_calendar_cells_end(n):
-    return empty_calendar_cells(12 - n)
-
-
-@register.simple_tag
-def up_down_icon(val):
-    if val > 0:
-        return mark_safe("""<span class="ri-arrow-up-s-line green"></span>""")
-    elif val < 0:
-        return mark_safe("""<span class="ri-arrow-down-s-line red"></span>""")
-
-
-@register.simple_tag
-def month_url(month, account=None, category=None):
-    url_name = "transaction_month"
-    url_params = {"year": month.year, "month": month.month}
-
-    if account:
-        url_name = "account_" + url_name
-        url_params |= {"account": account.pk}
-    elif category:
-        url_name = "category_" + url_name
-        url_params |= {"category": category.pk}
-
-    url = reverse(url_name, kwargs=url_params)
-    return mark_safe(f"""<a href="{url}">{ date(month, "Y-m") }</a>""")
-
-
-@register.simple_tag
-def plot_bar(s, sum_pm, s_max):
-    _res = ""
-
-    if sum_pm:
-        _w = abs(sum_pm / s_max)
-        _res += f"""<div style="width: {_w:.1%}"></div>"""
-    if sum_pm is not None and s * sum_pm > 0:
-        _w = abs(s / s_max)
-        _res += (
-            f"""<div class="tot" style="width: {_w:.1%}">"""
-            f"""<span>{pmrvalue(s)}</span></div>"""
-        )
-
-    return mark_safe(_res)
-
-
-@register.simple_tag
-def calendar_head():
-    months = (datetime.date(1, m + 1, 1) for m in range(12))
-    th = (f"""<th>{date(month, "m")}</th>""" for month in months)
-
-    return mark_safe("".join(th))
