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 %} {% block h1 %}{{ instance }}{% endblock %}
</h1> </h1>
{% endif %} {% endif %}
{% block pre %}{% endblock %}
<form method="post"> <form method="post">
{% csrf_token %} {% csrf_token %}
{% if instance.adding %}<input hidden name="next" value="{{ request.path }}" />{% endif %} {% 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.sum|check:form.instance.diff }}
{{ form.instance }} {{ form.instance }}
{% endblock %} {% endblock %}
{% block pre %}
{% if account %}
<p>
<a href="{{ account.get_absolute_url }}">{{ account.icon|remix }}{{ account }}</a>
</p>
{% endif %}
{% endblock %}
{% block tables %} {% block tables %}
{% if categories %} {% if categories %}
<h2>{% translate "Categories" %}</h2> <h2>{% translate "Categories" %}</h2>

View File

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

View File

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