From 82c7a03965087eb90a299eefb404d3ea2b33b866 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Sat, 22 Apr 2023 09:48:11 +0200
Subject: [PATCH] Move adding property to filters

---
 nummi/main/models.py                          | 19 +++++--------------
 nummi/main/templates/main/form/account.html   |  2 +-
 nummi/main/templates/main/form/base.html      |  6 +++---
 nummi/main/templates/main/form/form_base.html |  5 +++--
 nummi/main/templates/main/form/snapshot.html  |  2 +-
 .../main/templates/main/form/transaction.html |  3 ++-
 nummi/main/templatetags/main_extras.py        |  5 +++++
 7 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/nummi/main/models.py b/nummi/main/models.py
index 223e544..9203718 100644
--- a/nummi/main/models.py
+++ b/nummi/main/models.py
@@ -23,16 +23,7 @@ class UserModel(models.Model):
         abstract = True
 
 
-class NummiModel(UserModel):
-    @property
-    def adding(self):
-        return self._state.adding
-
-    class Meta:
-        abstract = True
-
-
-class Account(NummiModel):
+class Account(UserModel):
     id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
     name = models.CharField(max_length=64, default=_("Account"), verbose_name=_("Name"))
     icon = models.SlugField(
@@ -72,7 +63,7 @@ class Account(NummiModel):
         verbose_name_plural = _("Accounts")
 
 
-class AccountModel(NummiModel):
+class AccountModel(UserModel):
     account = models.ForeignKey(
         Account,
         on_delete=models.CASCADE,
@@ -83,7 +74,7 @@ class AccountModel(NummiModel):
         abstract = True
 
 
-class Category(NummiModel):
+class Category(UserModel):
     id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
     name = models.CharField(
         max_length=64, default=_("Category"), verbose_name=_("Name")
@@ -194,7 +185,7 @@ class Snapshot(AccountModel):
         verbose_name_plural = _("Statements")
 
 
-class Transaction(NummiModel):
+class Transaction(UserModel):
     id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
     name = models.CharField(
         max_length=256, default=_("Transaction"), verbose_name=_("Name")
@@ -264,7 +255,7 @@ class Transaction(NummiModel):
         verbose_name_plural = _("Transactions")
 
 
-class Invoice(NummiModel):
+class Invoice(UserModel):
     id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
     name = models.CharField(
         max_length=256, default=_("Invoice"), verbose_name=_("Name")
diff --git a/nummi/main/templates/main/form/account.html b/nummi/main/templates/main/form/account.html
index 0125c77..5d16607 100644
--- a/nummi/main/templates/main/form/account.html
+++ b/nummi/main/templates/main/form/account.html
@@ -9,7 +9,7 @@
 {% endblock %}
 {% block h2 %}{{ form.instance.icon|remix }}{{ form.instance }}{% endblock %}
 {% block tables %}
-    {% if not form.instance.adding %}
+    {% if not form.instance|adding %}
         <h3>{% translate "Statements" %}</h3>
         {% include "main/table/snapshot.html" %}
     {% endif %}
diff --git a/nummi/main/templates/main/form/base.html b/nummi/main/templates/main/form/base.html
index b79d2fe..def3515 100644
--- a/nummi/main/templates/main/form/base.html
+++ b/nummi/main/templates/main/form/base.html
@@ -3,7 +3,7 @@
 {% load main_extras %}
 {% load i18n %}
 {% block title %}
-    {% if form.instance.adding %}
+    {% if form.instance|adding %}
         {% block title_new %}{% endblock %}
     {% else %}
         {{ form.instance }}
@@ -24,7 +24,7 @@
 {% endblock %}
 {% block body %}
     {% with instance=form.instance %}
-        {% if instance.adding %}
+        {% if instance|adding %}
             <h2 class="new">
                 {% block h2_new %}{% endblock %}
             </h2>
@@ -36,7 +36,7 @@
         {% block pre %}{% endblock %}
         <form method="post">
             {% csrf_token %}
-            {% if instance.adding %}<input hidden name="next" value="{{ request.path }}" />{% endif %}
+            {% if instance|adding %}<input hidden name="next" value="{{ request.path }}" />{% endif %}
             {{ form }}
         </form>
         {% block tables %}{% endblock %}
diff --git a/nummi/main/templates/main/form/form_base.html b/nummi/main/templates/main/form/form_base.html
index 5a76aba..7fb6952 100644
--- a/nummi/main/templates/main/form/form_base.html
+++ b/nummi/main/templates/main/form/form_base.html
@@ -1,4 +1,5 @@
 {% load i18n %}
+{% load main_extras %}
 {% block fields %}
     {% if form.non_field_errors %}
         <ul class='errorlist'>
@@ -25,11 +26,11 @@
             <tr class="buttons">
                 <td colspan="2">
                     {% block buttons %}
-                        {% if not form.instance.adding %}
+                        {% if not form.instance|adding %}
                             <a class="del" href="{{ form.instance.get_delete_url }}">{% translate "Delete" %}</a>
                         {% endif %}
                         <input type="reset" value="{% translate "Reset" %}" />
-                        {% if form.instance.adding %}
+                        {% if form.instance|adding %}
                             <input type="submit" value="{% translate "Create" %}" />
                         {% else %}
                             <input type="submit" value="{% translate "Save" %}" />
diff --git a/nummi/main/templates/main/form/snapshot.html b/nummi/main/templates/main/form/snapshot.html
index a76c300..feaf251 100644
--- a/nummi/main/templates/main/form/snapshot.html
+++ b/nummi/main/templates/main/form/snapshot.html
@@ -23,7 +23,7 @@
         <h3>{% translate "Categories" %}</h3>
         {% include "main/plot/category.html" %}
     {% endif %}
-    {% if not form.instance.adding %}
+    {% if not form.instance|adding %}
         <h3>{% translate "Transactions" %} ({{ form.instance.sum|pmvalue }} / {{ form.instance.diff|pmvalue }})</h3>
         {% include "main/table/transaction.html" %}
     {% endif %}
diff --git a/nummi/main/templates/main/form/transaction.html b/nummi/main/templates/main/form/transaction.html
index e586fc9..39e10fb 100644
--- a/nummi/main/templates/main/form/transaction.html
+++ b/nummi/main/templates/main/form/transaction.html
@@ -1,5 +1,6 @@
 {% extends "main/form/base.html" %}
 {% load i18n %}
+{% load main_extras %}
 {% block title_new %}
     {% translate "Create transaction" %}
 {% endblock %}
@@ -14,7 +15,7 @@
     {% endif %}
 {% endblock %}
 {% block tables %}
-    {% if not form.instance.adding %}
+    {% if not form.instance|adding %}
         <h3>{% translate "Invoices" %}</h3>
         {% include "main/table/invoice.html" %}
     {% endif %}
diff --git a/nummi/main/templatetags/main_extras.py b/nummi/main/templatetags/main_extras.py
index 129c8a9..6b57178 100644
--- a/nummi/main/templatetags/main_extras.py
+++ b/nummi/main/templatetags/main_extras.py
@@ -54,3 +54,8 @@ def extension(file):
 @register.filter
 def verbose_name(obj):
     return obj._meta.verbose_name
+
+
+@register.filter
+def adding(obj):
+    return obj._state.adding
