Remove get_create_url, redirect createform to same page using hidden form field

This commit is contained in:
Edgar P. Burkhart 2023-04-20 14:39:45 +02:00
parent c744c87baa
commit 600a79c3b7
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
4 changed files with 6 additions and 19 deletions

View file

@ -53,9 +53,6 @@ class Account(NummiModel):
def __str__(self):
return str(self.name)
def get_create_url(self):
return reverse("new_account")
def get_absolute_url(self):
return reverse("account", kwargs={"pk": self.pk})
@ -102,9 +99,6 @@ class Category(NummiModel):
def __str__(self):
return str(self.name)
def get_create_url(self):
return reverse("new_category")
def get_absolute_url(self):
return reverse("category", kwargs={"pk": self.pk})
@ -189,9 +183,6 @@ class Snapshot(AccountModel):
self.file.delete()
super().delete(*args, **kwargs)
def get_create_url(self):
return reverse("new_snapshot")
def get_absolute_url(self):
return reverse("snapshot", kwargs={"pk": self.pk})
@ -254,9 +245,6 @@ class Transaction(NummiModel):
def __str__(self):
return f"{self.name}"
def get_create_url(self):
return reverse("new_transaction")
def get_absolute_url(self):
return reverse("transaction", kwargs={"pk": self.pk})
@ -306,9 +294,6 @@ class Invoice(NummiModel):
self.file.delete()
super().delete(*args, **kwargs)
def get_create_url(self):
return reverse("new_invoice", kwargs={"transaction_pk": self.transaction.pk})
def get_absolute_url(self):
return reverse(
"invoice", kwargs={"transaction_pk": self.transaction.pk, "pk": self.pk}

View file

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

View file

@ -34,7 +34,7 @@
<tfoot>
<tr>
<td colspan="3">
<a href="{% url "invoice" transaction.pk %}">{% translate "New invoice" %}</a>
<a href="{% url "new_invoice" transaction.pk %}">{% translate "New invoice" %}</a>
</td>
</tr>
</tfoot>

View file

@ -71,13 +71,14 @@ class UserFormMixin:
class NummiCreateView(UserMixin, UserFormMixin, CreateView):
def get_success_url(self):
return self.object.get_create_url()
def form_valid(self, form):
form.instance.user = self.request.user
self.next = form.data.get("next", None)
return super().form_valid(form)
def get_success_url(self):
return self.next or super().get_success_url()
class NummiUpdateView(UserMixin, UserFormMixin, UpdateView):
pass