Add snapshots

This commit is contained in:
Edgar P. Burkhart 2022-05-21 21:00:26 +02:00
parent 52eade1159
commit 29484e3c02
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
4 changed files with 120 additions and 1 deletions

View file

@ -1,7 +1,8 @@
from django.contrib import admin
from .models import Transaction, Invoice, Category
from .models import Transaction, Invoice, Category, Snapshot
admin.site.register(Transaction)
admin.site.register(Invoice)
admin.site.register(Category)
admin.site.register(Snapshot)

View file

@ -0,0 +1,44 @@
# Generated by Django 4.0.4 on 2022-05-21 18:40
import datetime
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
dependencies = [
("main", "0008_alter_category_options_category_icon"),
]
operations = [
migrations.CreateModel(
name="Snapshot",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("date", models.DateField(default=datetime.date.today)),
(
"value",
models.DecimalField(decimal_places=2, default=0, max_digits=12),
),
(
"previous",
models.OneToOneField(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="main.snapshot",
),
),
],
),
]

View file

@ -0,0 +1,25 @@
# Generated by Django 4.0.4 on 2022-05-21 18:44
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
("main", "0009_snapshot"),
]
operations = [
migrations.AlterField(
model_name="snapshot",
name="previous",
field=models.OneToOneField(
blank=True,
editable=False,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="main.snapshot",
),
),
]

View file

@ -72,3 +72,52 @@ class InvoiceForm(ModelForm):
class Meta:
model = Invoice
fields = ["name", "file"]
class Snapshot(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
date = models.DateField(default=date.today, unique=True)
value = models.DecimalField(max_digits=12, decimal_places=2, default=0)
previous = models.OneToOneField(
"self", on_delete=models.SET_NULL, blank=True, null=True, editable=False
)
def __str__(self):
if self.previous is None:
return f"Snapshot {self.date}: {self.value}"
return f"Snapshot {self.date}: {self.value}€ (Previous: {self.previous.date})"
def save(self, *args, only_super=False, **kwargs):
if not only_super:
_prev = (
self.__class__.objects.order_by("-date")
.filter(date__lt=self.date)
.first()
)
try:
_next = self.__class__.objects.get(previous=_prev)
except self.__class__.DoesNotExist:
pass
else:
_next.previous = self
_next.save(only_super=True)
self.previous = _prev
super().save(*args, **kwargs)
def delete(self, *args, only_super=False, **kwargs):
if not only_super:
try:
_next = self.__class__.objects.get(previous=self)
except self.__class__.DoesNotExist:
pass
else:
_next.previous = self.previous
super().delete(*args, **kwargs)
_next.save(only_super=True)
else:
super().delete(*args, **kwargs)
class Meta:
ordering = ["-date"]