diff --git a/nummi/main/models.py b/nummi/main/models.py
index 6bec146..b293099 100644
--- a/nummi/main/models.py
+++ b/nummi/main/models.py
@@ -203,9 +203,11 @@ class Snapshot(models.Model):
def save(self, *args, only_super=False, **kwargs):
if not only_super:
- _prever = Snapshot.objects.get(id=self.id)
- if _prever.file and _prever.file != self.file:
- pathlib.Path(_prever.file.path).unlink(missing_ok=True)
+ if Snapshot.objects.filter(id=self.id).exists():
+ _prever = Snapshot.objects.get(id=self.id)
+ if _prever.file and _prever.file != self.file:
+ pathlib.Path(_prever.file.path).unlink(missing_ok=True)
+
_prev = (
self.__class__.objects.order_by("-date")
.exclude(id=self.id)
@@ -266,6 +268,16 @@ class Snapshot(models.Model):
else:
super().delete(*args, **kwargs)
+ def get_absolute_url(self):
+ return reverse("snapshot", kwargs={"pk": self.pk})
+
+ def get_delete_url(self):
+ return reverse("del_snapshot", kwargs={"pk": self.pk})
+
+ @property
+ def adding(self):
+ return self._state.adding
+
@property
def sum(self):
if self.previous is None:
diff --git a/nummi/main/templates/main/snapshot.html b/nummi/main/templates/main/snapshot.html
deleted file mode 100644
index 55d9b88..0000000
--- a/nummi/main/templates/main/snapshot.html
+++ /dev/null
@@ -1,82 +0,0 @@
-{% extends "main/base.html" %}
-{% load static %}
-{% load main_extras %}
-{% load i18n %}
-{% block link %}
- {{ block.super }}
-
-
-
-{% endblock %}
-{% block body %}
- {% with sum=snapshot.sum %}
-
- {% if snapshot.previous is not None %}
- {% if sum == snapshot.diff %}
-
- {% else %}
-
- {% endif %}
- {% endif %}
- {{ snapshot }}
-
-
- {% if categories %}
- {% translate "Categories" %}
-
- {% for cat in categories %}
-
-
{{ cat.sum_m|pmvalue }}
-
-
- {% if cat.sum < 0 %}
-
- {{ cat.sum|pmvalue }}
-
- {% endif %}
-
-
-
- {% if cat.sum >= 0 %}
-
- {{ cat.sum|pmvalue }}
-
- {% endif %}
-
-
{{ cat.sum_p|pmvalue }}
- {% endfor %}
-
- {% endif %}
- {% if snapshot.transactions %}
- {% translate "Transactions" %} ({{ sum|pmvalue }} / {{ snapshot.diff|pmvalue }})
- {% transaction_table snapshot.transactions %}
- {% endif %}
- {% endwith %}
- {% endblock %}
diff --git a/nummi/main/templates/main/snapshot_form.html b/nummi/main/templates/main/snapshot_form.html
new file mode 100644
index 0000000..fd71694
--- /dev/null
+++ b/nummi/main/templates/main/snapshot_form.html
@@ -0,0 +1,79 @@
+{% extends "main/base.html" %}
+{% load static %}
+{% load main_extras %}
+{% load i18n %}
+{% block link %}
+ {{ block.super }}
+
+
+
+{% endblock %}
+{% block body %}
+ {% with snapshot=form.instance %}
+
+ {% if snapshot.previous is not None %}
+ {% if snapshot.sum == snapshot.diff %}
+
+ {% else %}
+
+ {% endif %}
+ {% endif %}
+ {{ snapshot }}
+
+
+ {% if categories %}
+ {% translate "Categories" %}
+
+ {% for cat in categories %}
+
+
{{ cat.sum_m|pmvalue }}
+
+
+ {% if cat.sum < 0 %}
+
+ {{ cat.sum|pmvalue }}
+
+ {% endif %}
+
+
+
+ {% if cat.sum >= 0 %}
+
+ {{ cat.sum|pmvalue }}
+
+ {% endif %}
+
+
{{ cat.sum_p|pmvalue }}
+ {% endfor %}
+
+ {% endif %}
+ {% if snapshot.transactions %}
+ {% translate "Transactions" %} ({{ snapshot.sum|pmvalue }} / {{ snapshot.diff|pmvalue }})
+ {% transaction_table snapshot.transactions %}
+ {% endif %}
+ {% endwith %}
+{% endblock %}
diff --git a/nummi/main/urls.py b/nummi/main/urls.py
index a028581..259deb1 100644
--- a/nummi/main/urls.py
+++ b/nummi/main/urls.py
@@ -14,6 +14,7 @@ urlpatterns = [
name="invoice",
),
path("category", views.CategoryCreateView.as_view(), name="category"),
+ path("snapshot", views.SnapshotCreateView.as_view(), name="snapshot"),
path("transaction/", views.TransactionUpdateView.as_view(), name="transaction"),
path(
"transaction//invoice/",
@@ -21,6 +22,7 @@ urlpatterns = [
name="invoice",
),
path("category/", views.CategoryUpdateView.as_view(), name="category"),
+ path("snapshot/", views.SnapshotUpdateView.as_view(), name="snapshot"),
path(
"transaction//delete",
views.TransactionDeleteView.as_view(),
@@ -34,9 +36,9 @@ urlpatterns = [
path(
"category//delete", views.CategoryDeleteView.as_view(), name="del_category"
),
- path("snapshot", views.snapshot, name="snapshot"),
- path("snapshot/", views.snapshot, name="snapshot"),
- path("snapshot//del", views.del_snapshot, name="del_snapshot"),
+ path(
+ "snapshot//delete", views.SnapshotDeleteView.as_view(), name="del_snapshot"
+ ),
path("search", views.search, name="search_post"),
path("search/", views.SearchView.as_view(), name="search"),
]
diff --git a/nummi/main/views.py b/nummi/main/views.py
index 20355cd..5f26a57 100644
--- a/nummi/main/views.py
+++ b/nummi/main/views.py
@@ -80,6 +80,11 @@ class CategoryCreateView(LoginRequiredMixin, CreateView):
form_class = CategoryForm
+class SnapshotCreateView(LoginRequiredMixin, CreateView):
+ model = Snapshot
+ form_class = SnapshotForm
+
+
class TransactionUpdateView(LoginRequiredMixin, UpdateView):
model = Transaction
form_class = TransactionForm
@@ -103,6 +108,11 @@ class CategoryUpdateView(LoginRequiredMixin, UpdateView):
form_class = CategoryForm
+class SnapshotUpdateView(LoginRequiredMixin, UpdateView):
+ model = Snapshot
+ form_class = SnapshotForm
+
+
class TransactionDeleteView(LoginRequiredMixin, DeleteView):
model = Transaction
template_name = "main/confirm_delete.html"
@@ -128,67 +138,10 @@ class CategoryDeleteView(LoginRequiredMixin, DeleteView):
success_url = reverse_lazy("index")
-@login_required
-def snapshot(request, uuid=None):
- if request.method == "GET":
- if uuid is None:
- _snapshot = Snapshot()
- else:
- _snapshot = get_object_or_404(Snapshot, id=uuid)
- context = {
- "snapshot": _snapshot,
- "form": SnapshotForm(instance=_snapshot),
- }
- elif request.method == "POST":
- try:
- _snapshot = Snapshot.objects.get(id=uuid)
- except Snapshot.DoesNotExist:
- _snapshot = Snapshot(id=uuid)
- _form = SnapshotForm(request.POST, request.FILES, instance=_snapshot)
- if _form.is_valid():
- _form.save()
- return redirect(snapshot, uuid=uuid)
-
- context = {
- "snapshot": _snapshot,
- "form": _form,
- }
-
- if _snapshot.transactions:
- context["categories"] = (
- _snapshot.transactions.filter(category__budget=True)
- .values("category", "category__name", "category__icon")
- .annotate(
- sum=models.Sum("value"),
- sum_m=models.Sum("value", filter=models.Q(value__lt=0)),
- sum_p=models.Sum("value", filter=models.Q(value__gt=0)),
- )
- .order_by("-sum")
- )
- context["cat_lim"] = max(
- map(
- lambda x: abs(x) if x else 0,
- context["categories"]
- .aggregate(
- max=models.Max("sum_p"),
- min=models.Min("sum_m"),
- )
- .values(),
- )
- )
- context["cat_lim_m"] = -context["cat_lim"]
- return render(
- request,
- "main/snapshot.html",
- context,
- )
-
-
-@login_required
-def del_snapshot(request, uuid):
- _snapshot = get_object_or_404(Snapshot, id=uuid)
- _snapshot.delete()
- return redirect(index)
+class SnapshotDeleteView(LoginRequiredMixin, DeleteView):
+ model = Snapshot
+ template_name = "main/confirm_delete.html"
+ success_url = reverse_lazy("index")
@login_required