diff --git a/nummi/main/admin.py b/nummi/main/admin.py index d8bde1e..ab84be8 100644 --- a/nummi/main/admin.py +++ b/nummi/main/admin.py @@ -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) diff --git a/nummi/main/migrations/0009_snapshot.py b/nummi/main/migrations/0009_snapshot.py new file mode 100644 index 0000000..f572375 --- /dev/null +++ b/nummi/main/migrations/0009_snapshot.py @@ -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", + ), + ), + ], + ), + ] diff --git a/nummi/main/migrations/0010_alter_snapshot_previous.py b/nummi/main/migrations/0010_alter_snapshot_previous.py new file mode 100644 index 0000000..53393fd --- /dev/null +++ b/nummi/main/migrations/0010_alter_snapshot_previous.py @@ -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", + ), + ), + ] diff --git a/nummi/main/models.py b/nummi/main/models.py index 1a22b2d..0cb42e2 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -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"]