From ae39b4870bf01c4719bacd8dea652b07157f9dc7 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Thu, 20 Apr 2023 15:15:00 +0200 Subject: [PATCH] Add back link to create forms linked to accounts, snapshots --- nummi/main/templates/main/form/base.html | 1 + nummi/main/templates/main/form/snapshot.html | 7 +++ .../main/templates/main/form/transaction.html | 7 +++ nummi/main/views.py | 46 ++++++++++--------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/nummi/main/templates/main/form/base.html b/nummi/main/templates/main/form/base.html index 2904904..1dab35e 100644 --- a/nummi/main/templates/main/form/base.html +++ b/nummi/main/templates/main/form/base.html @@ -33,6 +33,7 @@ {% block h1 %}{{ instance }}{% endblock %} {% endif %} + {% block pre %}{% endblock %}
{% csrf_token %} {% if instance.adding %}{% endif %} diff --git a/nummi/main/templates/main/form/snapshot.html b/nummi/main/templates/main/form/snapshot.html index aa8aec2..b54a3f2 100644 --- a/nummi/main/templates/main/form/snapshot.html +++ b/nummi/main/templates/main/form/snapshot.html @@ -11,6 +11,13 @@ {{ form.instance.sum|check:form.instance.diff }} {{ form.instance }} {% endblock %} +{% block pre %} + {% if account %} +

+ {{ account.icon|remix }}{{ account }} +

+ {% endif %} +{% endblock %} {% block tables %} {% if categories %}

{% translate "Categories" %}

diff --git a/nummi/main/templates/main/form/transaction.html b/nummi/main/templates/main/form/transaction.html index 81e648b..41853fc 100644 --- a/nummi/main/templates/main/form/transaction.html +++ b/nummi/main/templates/main/form/transaction.html @@ -6,6 +6,13 @@ {% block h1_new %} {% translate "New transaction" %} {% endblock %} +{% block pre %} + {% if snapshot %} +

+ {{ snapshot }} +

+ {% endif %} +{% endblock %} {% block tables %} {% if not form.instance.adding %}

{% translate "Invoices" %}

diff --git a/nummi/main/views.py b/nummi/main/views.py index c72d7bf..a95c403 100644 --- a/nummi/main/views.py +++ b/nummi/main/views.py @@ -73,7 +73,7 @@ class UserFormMixin: class NummiCreateView(UserMixin, UserFormMixin, CreateView): def form_valid(self, form): form.instance.user = self.request.user - self.next = form.data.get("next") + self.next = form.data.get("next", None) return super().form_valid(form) def get_success_url(self): @@ -109,15 +109,18 @@ class TransactionCreateView(NummiCreateView): form_class = TransactionForm template_name = "main/form/transaction.html" - def get_form_kwargs(self): - return super().get_form_kwargs() | { - "initial": { - "snapshot": ( - self.kwargs.get("snapshot") - or Snapshot.objects.filter(user=self.request.user).first() - ), - }, - } + def get_initial(self): + _queryset = Snapshot.objects.filter(user=self.request.user) + if "snapshot" in self.kwargs: + self.snapshot = get_object_or_404(_queryset, pk=self.kwargs["snapshot"]) + else: + self.snapshot = _queryset.first() + return {"snapshot": self.snapshot} + + def get_context_data(self, **kwargs): + if "snapshot" in self.kwargs: + return super().get_context_data(**kwargs) | {"snapshot": self.snapshot} + return super().get_context_data(**kwargs) class InvoiceCreateView(NummiCreateView): @@ -147,17 +150,18 @@ class SnapshotCreateView(NummiCreateView): form_class = SnapshotForm template_name = "main/form/snapshot.html" - def get_form_kwargs(self): - return super().get_form_kwargs() | { - "initial": { - "account": ( - self.kwargs.get("account") - or Account.objects.filter(user=self.request.user) - .order_by("-default") - .first() - ), - }, - } + def get_initial(self): + _queryset = Account.objects.filter(user=self.request.user) + if "account" in self.kwargs: + self.account = get_object_or_404(_queryset, pk=self.kwargs["account"]) + else: + self.account = _queryset.first() + return {"account": self.account} + + def get_context_data(self, **kwargs): + if "account" in self.kwargs: + return super().get_context_data(**kwargs) | {"account": self.account} + return super().get_context_data(**kwargs) class AccountUpdateView(NummiUpdateView):