Modal forms

* adds new POST functionality in NewStateModalForm, which does not use the super classes process_request() method
* adds explaining comments on the is_ajax() call
* updates translations
This commit is contained in:
mipel 2021-08-19 09:06:35 +02:00
parent 7dbf4d5b91
commit 198462b051
4 changed files with 26 additions and 16 deletions

View File

@ -5,11 +5,12 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 04.12.20
"""
from bootstrap_modal_forms.utils import is_ajax
from django import forms
from django.contrib import messages
from django.db import transaction
from django.http import HttpRequest
from django.shortcuts import redirect, render
from django.http import HttpRequest, HttpResponseRedirect
from django.shortcuts import render
from django.utils.translation import gettext_lazy as _
from compensation.models import Payment, CompensationState, CompensationAction, UnitChoices
@ -153,19 +154,24 @@ class NewStateModalForm(BaseModalForm):
template = self.template
if request.method == "POST":
if self.is_valid():
is_before_state = bool(request.GET.get("before", False))
self.save(is_before_state=is_before_state)
messages.success(
request,
msg_success
)
return redirect(redirect_url)
# Modal forms send one POST for checking on data validity. This can be used to return possible errors
# on the form. A second POST (if no errors occured) is sent afterwards and needs to process the
# saving/commiting of the data to the database. is_ajax() performs this check. The first request is
# an ajax call, the second is a regular form POST.
if not is_ajax(request.META):
is_before_state = bool(request.GET.get("before", False))
self.save(is_before_state=is_before_state)
messages.success(
request,
msg_success
)
return HttpResponseRedirect(redirect_url)
else:
messages.info(
request,
msg_error
)
return redirect(redirect_url)
context = {
"form": self,
}
context = BaseContext(request, context).context
return render(request, template, context)
elif request.method == "GET":
context = {
"form": self,

View File

@ -173,6 +173,10 @@ class BaseModalForm(BaseForm, BSModalForm):
if request.method == "POST":
if self.is_valid():
if not is_ajax(request.META):
# Modal forms send one POST for checking on data validity. This can be used to return possible errors
# on the form. A second POST (if no errors occured) is sent afterwards and needs to process the
# saving/commiting of the data to the database. is_ajax() performs this check. The first request is
# an ajax call, the second is a regular form POST.
self.save()
messages.success(
request,

Binary file not shown.

View File

@ -410,7 +410,7 @@ msgstr "Termine und Fristen"
#: compensation/templates/compensation/detail/compensation/includes/deadlines.html:14
#: compensation/templates/compensation/detail/eco_account/includes/deadlines.html:14
msgid "Add new deadline"
msgstr "Neue Frist hinzufügen"
msgstr "Frist/Termin hinzufügen"
#: compensation/templates/compensation/detail/compensation/includes/deadlines.html:28
#: compensation/templates/compensation/detail/eco_account/includes/deadlines.html:28
@ -594,7 +594,7 @@ msgstr "Maßnahme hinzugefügt"
#: compensation/views/compensation_views.py:219
#: compensation/views/eco_account_views.py:265
msgid "Deadline added"
msgstr "Frist hinzugefügt"
msgstr "Frist/Termin hinzugefügt"
#: compensation/views/compensation_views.py:238
msgid "State removed"