Add back link to create forms linked to accounts, snapshots

This commit is contained in:
Edgar P. Burkhart 2023-04-20 15:15:00 +02:00
parent 7521cc236f
commit ae39b4870b
Signed by: edpibu
GPG Key ID: 9833D3C5A25BD227
4 changed files with 40 additions and 21 deletions

View File

@ -33,6 +33,7 @@
{% block h1 %}{{ instance }}{% endblock %}
</h1>
{% endif %}
{% block pre %}{% endblock %}
<form method="post">
{% csrf_token %}
{% if instance.adding %}<input hidden name="next" value="{{ request.path }}" />{% endif %}

View File

@ -11,6 +11,13 @@
{{ form.instance.sum|check:form.instance.diff }}
{{ form.instance }}
{% endblock %}
{% block pre %}
{% if account %}
<p>
<a href="{{ account.get_absolute_url }}">{{ account.icon|remix }}{{ account }}</a>
</p>
{% endif %}
{% endblock %}
{% block tables %}
{% if categories %}
<h2>{% translate "Categories" %}</h2>

View File

@ -6,6 +6,13 @@
{% block h1_new %}
{% translate "New transaction" %}
{% endblock %}
{% block pre %}
{% if snapshot %}
<p>
<a href="{{ snapshot.get_absolute_url }}">{{ snapshot }}</a>
</p>
{% endif %}
{% endblock %}
{% block tables %}
{% if not form.instance.adding %}
<h2>{% translate "Invoices" %}</h2>

View File

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