Fix snapshot updating issues
This commit is contained in:
parent
02b0fae3f7
commit
0e60dfba27
2 changed files with 12 additions and 3 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue