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(
"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"]

View File

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

View File

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

View File

@ -1,5 +1,6 @@
{% extends "main/base.html" %}
{% load static %}
{% load main_extras %}
{% block link %}
{{ block.super }}
@ -23,7 +24,7 @@
<div class="transaction {% cycle 'w' 'g' %}">
<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="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="category text center">
{% if trans.category %}
@ -43,15 +44,33 @@
{% if snapshots %}
<h2>Relevés</h2>
<div id="snapshots" class="table col2">
<div id="snapshots" class="table col5">
<div class="header">
<strong class="date center">Date</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>
{% for snap in snapshots %}
<div class="snapshot {% cycle 'w' 'g' %}">
<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>
{% endfor %}
</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}"