diff --git a/nummi/main/migrations/0002_alter_snapshot_date_alter_snapshot_diff.py b/nummi/main/migrations/0002_alter_snapshot_date_alter_snapshot_diff.py
new file mode 100644
index 0000000..2753a1d
--- /dev/null
+++ b/nummi/main/migrations/0002_alter_snapshot_date_alter_snapshot_diff.py
@@ -0,0 +1,25 @@
+# Generated by Django 4.0.4 on 2022-05-22 09:15
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("main", "0001_initial"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="snapshot",
+ name="date",
+ field=models.DateField(unique=True),
+ ),
+ migrations.AlterField(
+ model_name="snapshot",
+ name="diff",
+ field=models.DecimalField(
+ blank=True, decimal_places=2, editable=False, max_digits=12, null=True
+ ),
+ ),
+ ]
diff --git a/nummi/main/models.py b/nummi/main/models.py
index d2e761f..d4cfcf0 100644
--- a/nummi/main/models.py
+++ b/nummi/main/models.py
@@ -82,7 +82,7 @@ class Snapshot(models.Model):
"self", on_delete=models.SET_NULL, blank=True, null=True, editable=False
)
diff = models.DecimalField(
- max_digits=12, decimal_places=2, default=0, editable=False
+ max_digits=12, decimal_places=2, editable=False, blank=True, null=True
)
def __str__(self):
@@ -105,11 +105,18 @@ class Snapshot(models.Model):
pass
else:
try:
- _prevnext = self.__class__.objects.exclude(id=self.id).get(previous=self)
+ _prevnext = self.__class__.objects.exclude(id=self.id).get(
+ previous=self
+ )
except self.__class__.DoesNotExist:
pass
else:
- _prevnext.previous = self.__class__.objects.order_by("-date").exclude(id=self.id).filter(date__lt=_prevnext.date).first()
+ _prevnext.previous = (
+ self.__class__.objects.order_by("-date")
+ .exclude(id=self.id)
+ .filter(date__lt=_prevnext.date)
+ .first()
+ )
_prevnext.save(only_super=True)
_next.previous = self
_next.save(only_super=True)
@@ -117,7 +124,7 @@ class Snapshot(models.Model):
self.previous = _prev
if self.previous is None:
- self.diff = 0
+ self.diff = None
else:
self.diff = self.value - self.previous.value
super().save(*args, **kwargs)
diff --git a/nummi/main/templates/main/category.html b/nummi/main/templates/main/category.html
index 38bccee..c193c60 100644
--- a/nummi/main/templates/main/category.html
+++ b/nummi/main/templates/main/category.html
@@ -26,35 +26,6 @@
{% if transactions %}
Transactions
-
-
-
- {% for trans in transactions %}
-
-
{{ trans.date|date:"Y-m-d" }}
-
{{ trans.name }}
-
{{ trans.value|floatformat:"2g"|pm }} €
-
{{ trans.trader|default_if_none:"–" }}
-
- {% if trans.category %}
-
-
- {{ trans.category }}
-
- {% else %}
- –
- {% endif %}
-
-
{{ trans.description }}
-
- {% endfor %}
-
+{% transaction_table transactions %}
{% endif %}
{% endblock %}
diff --git a/nummi/main/templates/main/index.html b/nummi/main/templates/main/index.html
index cc61b65..fe09c99 100644
--- a/nummi/main/templates/main/index.html
+++ b/nummi/main/templates/main/index.html
@@ -12,35 +12,8 @@
Nummi
{% if transactions %}
-
-
- {% for trans in transactions %}
-
-
{{ trans.date|date:"Y-m-d" }}
-
{{ trans.name }}
-
{{ trans.value|floatformat:"2g"|pm }} €
-
{{ trans.trader|default_if_none:"–" }}
-
- {% if trans.category %}
-
-
- {{ trans.category }}
-
- {% else %}
- –
- {% endif %}
-
-
{{ trans.description }}
-
- {% endfor %}
-
+Transactions
+{% transaction_table transactions %}
{% endif %}
{% if snapshots %}
@@ -58,11 +31,13 @@
{{ snap.date|date:"Y-m-d" }}
- {{ snap.value|floatformat:"2g" }} €
- {{ snap.diff|floatformat:"2g"|pm }} €
+ {% value snap.value %}
+
+ {% pmvalue snap.diff %}
+
{% with sum=snap.sum %}
- {% if sum is not None %}{{ sum|floatformat:"2g"|pm }} €{% endif %}
+ {% pmvalue sum %}
{% if snap.previous is not None %}
diff --git a/nummi/main/templates/main/snapshot.html b/nummi/main/templates/main/snapshot.html
index 54289d6..a85bca7 100644
--- a/nummi/main/templates/main/snapshot.html
+++ b/nummi/main/templates/main/snapshot.html
@@ -36,37 +36,9 @@
{% if transactions %}
-Transactions ({{ sum|floatformat:"2g"|pm }} € / {{ snapshot.diff|floatformat:"2g"|pm }} €)
+Transactions ({% pmvalue sum %} / {% pmvalue snapshot.diff %})
-
-
- {% for trans in transactions %}
-
-
{{ trans.date|date:"Y-m-d" }}
-
{{ trans.name }}
-
{{ trans.value|floatformat:"2g"|pm }} €
-
{{ trans.trader|default_if_none:"–" }}
-
- {% if trans.category %}
-
-
- {{ trans.category }}
-
- {% else %}
- –
- {% endif %}
-
-
{{ trans.description }}
-
- {% endfor %}
-
+{% transaction_table transactions %}
{% endif %}
{% endwith %}
{% endblock %}
diff --git a/nummi/main/templates/main/tag/pmvalue.html b/nummi/main/templates/main/tag/pmvalue.html
new file mode 100644
index 0000000..306aa87
--- /dev/null
+++ b/nummi/main/templates/main/tag/pmvalue.html
@@ -0,0 +1,8 @@
+{% extends "main/tag/value.html" %}
+
+{% block "value" %}
+ {% if value %}
+ {% if value > 0 %}+{% endif %}
+ {{ block.super }}
+ {% else %}–{% endif %}
+{% endblock %}
diff --git a/nummi/main/templates/main/tag/transaction_table.html b/nummi/main/templates/main/tag/transaction_table.html
new file mode 100644
index 0000000..2ab25d1
--- /dev/null
+++ b/nummi/main/templates/main/tag/transaction_table.html
@@ -0,0 +1,31 @@
+{% load main_extras %}
+
+
+
+ {% for trans in transactions %}
+
+
{{ trans.date|date:"Y-m-d" }}
+
{{ trans.name }}
+
{% pmvalue trans.value %}
+
{{ trans.trader|default_if_none:"–" }}
+
+ {% if trans.category %}
+
+
+ {{ trans.category }}
+
+ {% else %}
+ –
+ {% endif %}
+
+
{{ trans.description }}
+
+ {% endfor %}
+
diff --git a/nummi/main/templates/main/tag/value.html b/nummi/main/templates/main/tag/value.html
new file mode 100644
index 0000000..d004f9a
--- /dev/null
+++ b/nummi/main/templates/main/tag/value.html
@@ -0,0 +1,7 @@
+{% spaceless %}
+
+ {% block "value" %}
+ {{ value|floatformat:"2g" }} €
+ {% endblock %}
+
+{% endspaceless %}
diff --git a/nummi/main/templatetags/main_extras.py b/nummi/main/templatetags/main_extras.py
index cbdac32..4f911e6 100644
--- a/nummi/main/templatetags/main_extras.py
+++ b/nummi/main/templatetags/main_extras.py
@@ -4,7 +4,16 @@ from django.template.defaultfilters import stringfilter
register = template.Library()
-@register.filter
-@stringfilter
-def pm(value):
- return f"{'+' if value[0] != '-' else ''}{value}"
+@register.inclusion_tag("main/tag/value.html")
+def value(value):
+ return {"value": value}
+
+
+@register.inclusion_tag("main/tag/pmvalue.html")
+def pmvalue(value):
+ return {"value": value}
+
+
+@register.inclusion_tag("main/tag/transaction_table.html")
+def transaction_table(transactions):
+ return {"transactions": transactions}