From c82ee8afbc15f88683197595298617205f31c27a Mon Sep 17 00:00:00 2001 From: mipel Date: Tue, 17 Aug 2021 15:27:49 +0200 Subject: [PATCH] Modal and other tweaks * removes WIKI_URL, replaces with HELP_LINK since it was the same before as well * refactors modal form processing (process_request()) * modal form can now display errors directly inside the modal (as intended by the devs) * modals now properly support the GET-POST workflow that is intended by the devs. More information here: https://github.com/trco/django-bootstrap-modal-forms/issues/183 * Improves label-field linking in generic_table_form_body.html * removes isDeleteForm attribute from modal_form_script.html --- konova/contexts.py | 5 +-- konova/forms.py | 34 +++++++--------- konova/settings.py | 3 -- konova/sub_settings/context_settings.py | 2 +- konova/sub_settings/django_settings.py | 5 ++- templates/form/generic_table_form_body.html | 43 +++++++++++---------- templates/modal/modal_form.html | 2 +- templates/modal/modal_form_script.html | 1 - 8 files changed, 44 insertions(+), 51 deletions(-) diff --git a/konova/contexts.py b/konova/contexts.py index e514dd1f..b8ca6d68 100644 --- a/konova/contexts.py +++ b/konova/contexts.py @@ -7,8 +7,7 @@ Created on: 16.11.20 """ from django.http import HttpRequest -from konova.settings import HELP_LINK -from konova.sub_settings.context_settings import BASE_TITLE, WIKI_URL, BASE_FRONTEND_TITLE +from konova.sub_settings.context_settings import BASE_TITLE, HELP_LINK, BASE_FRONTEND_TITLE class BaseContext: @@ -19,11 +18,9 @@ class BaseContext: "base_title": BASE_TITLE, "base_frontend_title": BASE_FRONTEND_TITLE, "language": "en", - "wiki_url": WIKI_URL, "user": None, "current_role": None, "help_link": HELP_LINK, - "modal_reload_page": "true", # must be string and lower case true, since it's used in JS code } def __init__(self, request: HttpRequest, additional_context: dict = {}): diff --git a/konova/forms.py b/konova/forms.py index b5398fa2..d685292e 100644 --- a/konova/forms.py +++ b/konova/forms.py @@ -9,14 +9,15 @@ Created on: 16.11.20 from abc import abstractmethod from bootstrap_modal_forms.forms import BSModalForm +from bootstrap_modal_forms.utils import is_ajax from django import forms from django.contrib import messages from django.contrib.auth.models import User from django.contrib.gis.forms import GeometryField, OSMWidget from django.contrib.gis.geos import Polygon 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 import timezone from django.utils.translation import gettext_lazy as _ @@ -44,6 +45,7 @@ class BaseForm(forms.Form): def __init__(self, *args, **kwargs): self.instance = kwargs.pop("instance", None) self.user = kwargs.pop("user", None) + self.request = kwargs.pop("request", None) super().__init__(*args, **kwargs) # Check for required fields @@ -170,25 +172,19 @@ class BaseModalForm(BaseForm, BSModalForm): template = self.template if request.method == "POST": if self.is_valid(): - self.save() - messages.success( - request, - msg_success - ) - return redirect(redirect_url) - else: - messages.error( - request, - msg_error, - extra_tags="danger" - ) - for field, error in self.errors.items(): - messages.error( + if not is_ajax(request.META): + self.save() + messages.success( request, - "{}: {}".format(self.fields[field].label, _(error[0])), - extra_tags="danger" + msg_success ) - return redirect(redirect_url) + return HttpResponseRedirect(redirect_url) + else: + context = { + "form": self, + } + context = BaseContext(request, context).context + return render(request, template, context) elif request.method == "GET": context = { "form": self, diff --git a/konova/settings.py b/konova/settings.py index d2b14fa3..e6c37d6f 100644 --- a/konova/settings.py +++ b/konova/settings.py @@ -56,9 +56,6 @@ ZB_GROUP = "Registration office" ETS_GROUP = "Conservation office" -# HELP PAGE LINK -HELP_LINK = "https://dienste.naturschutz.rlp.de/doku/doku.php?id=ksp:start" - # Needed to redirect to LANIS ## Values to be inserted are [zoom_level, x_coord, y_coord] LANIS_LINK_TEMPLATE = "https://geodaten.naturschutz.rlp.de/kartendienste_naturschutz/index.php?lang=de&zl={}&x={}&y={}&bl=tk_rlp_tms_grau&bo=1&lo=0.8,0.8,0.8,0.6,0.8,0.8,0.8,0.8,0.8&layers=eiv_f,eiv_l,eiv_p,kom_f,kom_l,kom_p,oek_f,ema_f,mae&service=kartendienste_naturschutz" diff --git a/konova/sub_settings/context_settings.py b/konova/sub_settings/context_settings.py index d31f2e20..53177c75 100644 --- a/konova/sub_settings/context_settings.py +++ b/konova/sub_settings/context_settings.py @@ -9,4 +9,4 @@ Created on: 16.11.20 BASE_TITLE_SHORT = "KSP" BASE_TITLE = "KSP - Kompensationsverzeichnis Service Portal" BASE_FRONTEND_TITLE = "Kompensationsverzeichnis Service Portal" -WIKI_URL = "https://dienste.naturschutz.rlp.de/doku/doku.php?id=ksp:start" +HELP_LINK = "https://dienste.naturschutz.rlp.de/doku/doku.php?id=ksp:start" diff --git a/konova/sub_settings/django_settings.py b/konova/sub_settings/django_settings.py index 0a89afaa..494b3c79 100644 --- a/konova/sub_settings/django_settings.py +++ b/konova/sub_settings/django_settings.py @@ -32,7 +32,10 @@ SECRET_KEY = '5=9-)2)h$u9=!zrhia9=lj-2#cpcb8=#$7y+)l$5tto$3q(n_+' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = [ + "127.0.0.1", + "localhost", +] # Authentication settings LOGIN_URL = "/login/" diff --git a/templates/form/generic_table_form_body.html b/templates/form/generic_table_form_body.html index 5ca9f801..4b46e248 100644 --- a/templates/form/generic_table_form_body.html +++ b/templates/form/generic_table_form_body.html @@ -1,22 +1,23 @@ {% load i18n %} - - - {% for field in form %} - - - - - {% endfor %} - -
- - {{ field.help_text }} - - {{ field }} - {% for error in field.errors %} - {{ error }} - {% endfor %} -
- {% if form.has_required_fields %} - {% trans 'Fields with * are required.' %} - {% endif %} \ No newline at end of file + + + {% for field in form %} + + + + + {% endfor %} + +
+ + {{ field.help_text }} + + {{ field }} + {% for error in field.errors %} +
+ {{ error }} + {% endfor %} +
+{% if form.has_required_fields %} +{% trans 'Fields with * are required.' %} +{% endif %} \ No newline at end of file diff --git a/templates/modal/modal_form.html b/templates/modal/modal_form.html index 9a217172..46e109f1 100644 --- a/templates/modal/modal_form.html +++ b/templates/modal/modal_form.html @@ -5,7 +5,7 @@ {% endcomment %} -
+ {% csrf_token %}