Fix snapshot updating issues

This commit is contained in:
Edgar P. Burkhart 2022-05-22 11:10:52 +02:00
parent 02b0fae3f7
commit 0e60dfba27
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 12 additions and 3 deletions

View file

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

View file

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