Add snapshots
This commit is contained in:
parent
52eade1159
commit
29484e3c02
4 changed files with 120 additions and 1 deletions
|
@ -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)
|
||||
|
|
44
nummi/main/migrations/0009_snapshot.py
Normal file
44
nummi/main/migrations/0009_snapshot.py
Normal 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",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
25
nummi/main/migrations/0010_alter_snapshot_previous.py
Normal file
25
nummi/main/migrations/0010_alter_snapshot_previous.py
Normal 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",
|
||||
),
|
||||
),
|
||||
]
|
|
@ -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"]
|
||||
|
|
Loading…
Reference in a new issue