diff --git a/nummi/main/models.py b/nummi/main/models.py index e9dd980..83c924e 100644 --- a/nummi/main/models.py +++ b/nummi/main/models.py @@ -76,7 +76,7 @@ class InvoiceForm(ModelForm): class Snapshot(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) - date = models.DateField(default=date.today, unique=True) + date = models.DateField(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 @@ -98,13 +98,19 @@ class Snapshot(models.Model): .filter(date__lt=self.date) .first() ) - print(_prev) try: _next = self.__class__.objects.exclude(id=self.id).get(previous=_prev) except self.__class__.DoesNotExist: pass else: + try: + _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.save(only_super=True) _next.previous = self _next.save(only_super=True) diff --git a/nummi/main/views.py b/nummi/main/views.py index 4a200fa..3bd4e6b 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -142,7 +142,10 @@ def snapshot(request, date=None): @login_required def update_snapshot(request, uuid): - _snapshot, _ = Snapshot.objects.get_or_create(id=uuid) + try: + _snapshot = Snapshot.objects.get(id=uuid) + except Snapshot.DoesNotExist: + _snapshot = Snapshot(id=uuid) _form = SnapshotForm(request.POST, instance=_snapshot) _form.save() return redirect(snapshot, date=_snapshot.date)