Snapshot table

This commit is contained in:
Edgar P. Burkhart 2022-05-22 09:21:30 +02:00
parent a3797bb7fb
commit 15a317dc0f
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
7 changed files with 87 additions and 8 deletions

View file

@ -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),
),
]

View file

@ -81,6 +81,9 @@ class Snapshot(models.Model):
previous = models.OneToOneField( previous = models.OneToOneField(
"self", on_delete=models.SET_NULL, blank=True, null=True, editable=False "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): def __str__(self):
if self.previous is None: if self.previous is None:
@ -94,9 +97,10 @@ class Snapshot(models.Model):
.filter(date__lt=self.date) .filter(date__lt=self.date)
.first() .first()
) )
print(_prev)
try: try:
_next = self.__class__.objects.get(previous=_prev) _next = self.__class__.objects.exclude(id=self.id).get(previous=_prev)
except self.__class__.DoesNotExist: except self.__class__.DoesNotExist:
pass pass
else: else:
@ -104,6 +108,11 @@ class Snapshot(models.Model):
_next.save(only_super=True) _next.save(only_super=True)
self.previous = _prev self.previous = _prev
if self.previous is None:
self.diff = 0
else:
self.diff = self.value - self.previous.value
super().save(*args, **kwargs) super().save(*args, **kwargs)
def delete(self, *args, only_super=False, **kwargs): def delete(self, *args, only_super=False, **kwargs):
@ -119,5 +128,14 @@ class Snapshot(models.Model):
else: else:
super().delete(*args, **kwargs) 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: class Meta:
ordering = ["-date"] ordering = ["-date"]

View file

@ -54,7 +54,3 @@ h1 {
#categories > a { #categories > a {
margin-right: var(--gap); margin-right: var(--gap);
} }
#snapshots {
max-width: 24rem;
}

View file

@ -22,6 +22,9 @@ body {
--text-link: #0066ff; --text-link: #0066ff;
--gap: 1em; --gap: 1em;
--red: #ff6600;
--green: #66cc00;
} }
h1 { h1 {
@ -35,3 +38,6 @@ a {
a:hover { a:hover {
text-decoration: underline; text-decoration: underline;
} }
.red {color: var(--red)}
.green {color: var(--green)}

View file

@ -1,5 +1,6 @@
{% extends "main/base.html" %} {% extends "main/base.html" %}
{% load static %} {% load static %}
{% load main_extras %}
{% block link %} {% block link %}
{{ block.super }} {{ block.super }}
@ -23,7 +24,7 @@
<div class="transaction {% cycle 'w' 'g' %}"> <div class="transaction {% cycle 'w' 'g' %}">
<span class="date num center">{{ trans.date|date:"Y-m-d" }}</span> <span class="date num center">{{ trans.date|date:"Y-m-d" }}</span>
<span class="name text"><a href="{% url 'transaction' trans.id %}">{{ trans.name }}</a></span> <span class="name text"><a href="{% url 'transaction' trans.id %}">{{ trans.name }}</a></span>
<span class="value num right">{% if trans.value > 0 %}+{% endif %}{{ trans.value|floatformat:"2g" }} €</span> <span class="value num right">{{ trans.value|floatformat:"2g"|pm }} €</span>
<span class="trader text center">{{ trans.trader|default_if_none:"" }}</span> <span class="trader text center">{{ trans.trader|default_if_none:"" }}</span>
<span class="category text center"> <span class="category text center">
{% if trans.category %} {% if trans.category %}
@ -43,15 +44,33 @@
{% if snapshots %} {% if snapshots %}
<h2>Relevés</h2> <h2>Relevés</h2>
<div id="snapshots" class="table col2"> <div id="snapshots" class="table col5">
<div class="header"> <div class="header">
<strong class="date center">Date</strong> <strong class="date center">Date</strong>
<strong class="value center">Valeur</strong> <strong class="value center">Valeur</strong>
<strong class="diff center">Différence</strong>
<strong class="diff center">Transactions</strong>
<strong class="diff center">Valide</strong>
</div> </div>
{% for snap in snapshots %} {% for snap in snapshots %}
<div class="snapshot {% cycle 'w' 'g' %}"> <div class="snapshot {% cycle 'w' 'g' %}">
<span class="date num center">{{ snap.date|date:"Y-m-d" }}</span> <span class="date num center">{{ snap.date|date:"Y-m-d" }}</span>
<span class="value num right">{{ snap.value }} €</span> <span class="value num right">{{ snap.value|floatformat:"2g" }} €</span>
<span class="diff num right">{{ snap.diff|floatformat:"2g"|pm }} €</span>
{% with sum=snap.sum %}
<span class="sum num right">
{% if sum is not None %}{{ sum|floatformat:"2g"|pm }} €{% endif %}
</span>
<span class="valid center">
{% if snap.previous is not None %}
{% if sum == snap.diff %}
<i class="fa fa-check green"></i>
{% else %}
<i class="fa fa-xmark red"></i>
{% endif %}
{% endif %}
</span>
{% endwith %}
</div> </div>
{% endfor %} {% endfor %}
</div> </div>

View file

View file

@ -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}"