Fix bugs and update templating

This commit is contained in:
Edgar P. Burkhart 2022-05-22 12:32:26 +02:00
parent 5d6f47b852
commit df0882bd88
Signed by: edpibu
GPG Key ID: 9833D3C5A25BD227
9 changed files with 105 additions and 100 deletions

View File

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

View File

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

View File

@ -26,35 +26,6 @@
{% if transactions %}
<h2>Transactions</h2>
<div id="transactions" class="table col6">
<div class="header">
<strong class="date center">Date</strong>
<strong class="name">Nom</strong>
<strong class="value center">Valeur</strong>
<strong class="trader center">Commerçant</strong>
<strong class="category center">Catégorie</strong>
<strong class="description">Description</strong>
</div>
{% for trans in transactions %}
<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">{{ 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 %}
<i class="fa fa-{{ trans.category.icon }}"></i>
<a href="{% url 'category' trans.category.id %}">
{{ trans.category }}
</a>
{% else %}
{% endif %}
</span>
<span class="description text">{{ trans.description }}</span>
</div>
{% endfor %}
</div>
{% transaction_table transactions %}
{% endif %}
{% endblock %}

View File

@ -12,35 +12,8 @@
<h1>Nummi</h1>
{% if transactions %}
<div id="transactions" class="table col6">
<div class="header">
<strong class="date center">Date</strong>
<strong class="name">Nom</strong>
<strong class="value center">Valeur</strong>
<strong class="trader center">Commerçant</strong>
<strong class="category center">Catégorie</strong>
<strong class="description">Description</strong>
</div>
{% for trans in transactions %}
<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">{{ 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 %}
<i class="fa fa-{{ trans.category.icon }}"></i>
<a href="{% url 'category' trans.category.id %}">
{{ trans.category }}
</a>
{% else %}
{% endif %}
</span>
<span class="description text">{{ trans.description }}</span>
</div>
{% endfor %}
</div>
<h2>Transactions</h2>
{% transaction_table transactions %}
{% endif %}
{% if snapshots %}
@ -58,11 +31,13 @@
<span class="date num center">
<a href="{% url 'snapshot' snap.date %}">{{ snap.date|date:"Y-m-d" }}</a>
</span>
<span class="value num right">{{ snap.value|floatformat:"2g" }} €</span>
<span class="diff num right">{{ snap.diff|floatformat:"2g"|pm }} €</span>
<span class="value num right">{% value snap.value %}</span>
<span class="diff num right">
{% pmvalue snap.diff %}
</span>
{% with sum=snap.sum %}
<span class="sum num right">
{% if sum is not None %}{{ sum|floatformat:"2g"|pm }} €{% endif %}
{% pmvalue sum %}
</span>
<span class="valid center">
{% if snap.previous is not None %}

View File

@ -36,37 +36,9 @@
</form>
{% if transactions %}
<h2>Transactions ({{ sum|floatformat:"2g"|pm }} € / {{ snapshot.diff|floatformat:"2g"|pm }} €)</h2>
<h2>Transactions ({% pmvalue sum %} / {% pmvalue snapshot.diff %})</h2>
<div id="transactions" class="table col6">
<div class="header">
<strong class="date center">Date</strong>
<strong class="name">Nom</strong>
<strong class="value center">Valeur</strong>
<strong class="trader center">Commerçant</strong>
<strong class="category center">Catégorie</strong>
<strong class="description">Description</strong>
</div>
{% for trans in transactions %}
<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">{{ 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 %}
<i class="fa fa-{{ trans.category.icon }}"></i>
<a href="{% url 'category' trans.category.id %}">
{{ trans.category }}
</a>
{% else %}
{% endif %}
</span>
<span class="description text">{{ trans.description }}</span>
</div>
{% endfor %}
</div>
{% transaction_table transactions %}
{% endif %}
{% endwith %}
{% endblock %}

View File

@ -0,0 +1,8 @@
{% extends "main/tag/value.html" %}
{% block "value" %}
{% if value %}
{% if value > 0 %}+{% endif %}
{{ block.super }}
{% else %}{% endif %}
{% endblock %}

View File

@ -0,0 +1,31 @@
{% load main_extras %}
<div id="transactions" class="table col6">
<div class="header">
<strong class="date center">Date</strong>
<strong class="name">Nom</strong>
<strong class="value center">Valeur</strong>
<strong class="trader center">Commerçant</strong>
<strong class="category center">Catégorie</strong>
<strong class="description">Description</strong>
</div>
{% for trans in transactions %}
<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">{% pmvalue trans.value %}</span>
<span class="trader text center">{{ trans.trader|default_if_none:"" }}</span>
<span class="category text center">
{% if trans.category %}
<i class="fa fa-{{ trans.category.icon }}"></i>
<a href="{% url 'category' trans.category.id %}">
{{ trans.category }}
</a>
{% else %}
{% endif %}
</span>
<span class="description text">{{ trans.description }}</span>
</div>
{% endfor %}
</div>

View File

@ -0,0 +1,7 @@
{% spaceless %}
<span>
{% block "value" %}
{{ value|floatformat:"2g" }} €
{% endblock %}
</span>
{% endspaceless %}

View File

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