From 15a317dc0f0f6a09ecf5b137f46f41ca319c72ba Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Sun, 22 May 2022 09:21:30 +0200 Subject: [PATCH] Snapshot table --- ...snapshot_options_snapshot_diff_and_more.py | 30 +++++++++++++++++++ nummi/main/models.py | 20 ++++++++++++- nummi/main/static/main/css/index.css | 4 --- nummi/main/static/main/css/main.css | 6 ++++ nummi/main/templates/main/index.html | 25 ++++++++++++++-- nummi/main/templatetags/__init__.py | 0 nummi/main/templatetags/main_extras.py | 10 +++++++ 7 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 nummi/main/migrations/0011_alter_snapshot_options_snapshot_diff_and_more.py create mode 100644 nummi/main/templatetags/__init__.py create mode 100644 nummi/main/templatetags/main_extras.py diff --git a/nummi/main/migrations/0011_alter_snapshot_options_snapshot_diff_and_more.py b/nummi/main/migrations/0011_alter_snapshot_options_snapshot_diff_and_more.py new file mode 100644 index 0000000..7a48363 --- /dev/null +++ b/nummi/main/migrations/0011_alter_snapshot_options_snapshot_diff_and_more.py @@ -0,0 +1,30 @@ +# Generated by Django 4.0.4 on 2022-05-22 06:43 + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("main", "0010_alter_snapshot_previous"), + ] + + operations = [ + migrations.AlterModelOptions( + name="snapshot", + options={"ordering": ["-date"]}, + ), + migrations.AddField( + model_name="snapshot", + name="diff", + field=models.DecimalField( + decimal_places=2, default=0, editable=False, max_digits=12 + ), + ), + migrations.AlterField( + model_name="snapshot", + name="date", + field=models.DateField(default=datetime.date.today, unique=True), + ), + ] diff --git a/nummi/main/models.py b/nummi/main/models.py index 0cb42e2..5f14876 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -81,6 +81,9 @@ class Snapshot(models.Model): previous = models.OneToOneField( "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 + ) def __str__(self): if self.previous is None: @@ -94,9 +97,10 @@ class Snapshot(models.Model): .filter(date__lt=self.date) .first() ) + print(_prev) try: - _next = self.__class__.objects.get(previous=_prev) + _next = self.__class__.objects.exclude(id=self.id).get(previous=_prev) except self.__class__.DoesNotExist: pass else: @@ -104,6 +108,11 @@ class Snapshot(models.Model): _next.save(only_super=True) self.previous = _prev + + if self.previous is None: + self.diff = 0 + else: + self.diff = self.value - self.previous.value super().save(*args, **kwargs) def delete(self, *args, only_super=False, **kwargs): @@ -119,5 +128,14 @@ class Snapshot(models.Model): else: super().delete(*args, **kwargs) + @property + def sum(self): + if self.previous is None: + return None + trans = Transaction.objects.filter( + date__lt=self.date, date__gte=self.previous.date + ).aggregate(sum=models.Sum("value")) + return trans["sum"] or 0 + class Meta: ordering = ["-date"] diff --git a/nummi/main/static/main/css/index.css b/nummi/main/static/main/css/index.css index 9a68717..e56063e 100644 --- a/nummi/main/static/main/css/index.css +++ b/nummi/main/static/main/css/index.css @@ -54,7 +54,3 @@ h1 { #categories > a { margin-right: var(--gap); } - -#snapshots { - max-width: 24rem; -} diff --git a/nummi/main/static/main/css/main.css b/nummi/main/static/main/css/main.css index 732bdd8..6cd9552 100644 --- a/nummi/main/static/main/css/main.css +++ b/nummi/main/static/main/css/main.css @@ -22,6 +22,9 @@ body { --text-link: #0066ff; --gap: 1em; + + --red: #ff6600; + --green: #66cc00; } h1 { @@ -35,3 +38,6 @@ a { a:hover { text-decoration: underline; } + +.red {color: var(--red)} +.green {color: var(--green)} diff --git a/nummi/main/templates/main/index.html b/nummi/main/templates/main/index.html index daa7cef..fc46057 100644 --- a/nummi/main/templates/main/index.html +++ b/nummi/main/templates/main/index.html @@ -1,5 +1,6 @@ {% extends "main/base.html" %} {% load static %} +{% load main_extras %} {% block link %} {{ block.super }} @@ -23,7 +24,7 @@
{{ trans.date|date:"Y-m-d" }} {{ trans.name }} - {% if trans.value > 0 %}+{% endif %}{{ trans.value|floatformat:"2g" }} € + {{ trans.value|floatformat:"2g"|pm }} € {{ trans.trader|default_if_none:"–" }} {% if trans.category %} @@ -43,15 +44,33 @@ {% if snapshots %}

Relevés

-
+
Date Valeur + Différence + Transactions + Valide
{% for snap in snapshots %}
{{ snap.date|date:"Y-m-d" }} - {{ snap.value }} € + {{ snap.value|floatformat:"2g" }} € + {{ snap.diff|floatformat:"2g"|pm }} € + {% with sum=snap.sum %} + + {% if sum is not None %}{{ sum|floatformat:"2g"|pm }} €{% endif %} + + + {% if snap.previous is not None %} + {% if sum == snap.diff %} + + {% else %} + + {% endif %} + {% endif %} + + {% endwith %}
{% endfor %}
diff --git a/nummi/main/templatetags/__init__.py b/nummi/main/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nummi/main/templatetags/main_extras.py b/nummi/main/templatetags/main_extras.py new file mode 100644 index 0000000..cbdac32 --- /dev/null +++ b/nummi/main/templatetags/main_extras.py @@ -0,0 +1,10 @@ +from django import template +from django.template.defaultfilters import stringfilter + +register = template.Library() + + +@register.filter +@stringfilter +def pm(value): + return f"{'+' if value[0] != '-' else ''}{value}"