Self hosted FLOSS fitness/workout, nutrition and weight tracker
Use custom headers for AJAX forms and redirects
Roland Geider74d1944836bf
diff --git a/wger/core/static/js/workout-manager.js b/wger/core/static/js/workout-manager.jsindex ca2979fe2..056d1020d 100644--- a/wger/core/static/js/workout-manager.js+++ b/wger/core/static/js/workout-manager.js@@ -304,36 +304,30 @@ function modal_dialog_form_edit() {jqXHR.setRequestHeader("X-wger-no-messages", "1");},success: function (data, textStatus, jqXHR) {- if ($(data).find('form .has-error').length > 0) {- // we must do the same with the new form as before, binding the click-event,- // checking for errors etc, so it calls itself here again.- $("#ajax-info-content form").html($(data).find('form').html());- $("#ajax-info-title").html($(data).find("#page-title").html());- modal_dialog_form_edit();- } else {- $("#wger-ajax-info").modal("hide");-- // If there was a redirect we must change the URL of the browser. Otherwise- // a reload would not change the adress bar, but the content would.- // Since it is not possible to get this URL from the AJAX request, we read it out- // from a hidden HTML DIV in the document...- var current_url = $(data).find("#current-url").data('currentUrl');-- // TODO: There seems to be problems sometimes when using the technique below and- // bootstrap's menu bar (drop downs won't open). So just do a normal- // redirect.- window.location.href = current_url;--+ var url = jqXHR.getResponseHeader('X-wger-redirect');+ if (url) {+ window.location.href = url;/*- if(document.URL.indexOf(current_url))- {- history.pushState({}, "", current_url);+ if(document.URL.indexOf(url)) {+ history.pushState({}, "", url);}*/+ }+ else {++ if ($(data).find('form .has-error').length > 0) {+ // we must do the same with the new form as before, binding the click-event,+ // checking for errors etc, so it calls itself here again.+ $("#ajax-info-content form").html($(data).find('form').html());+ $("#ajax-info-title").html($(data).find("#page-title").html());+ modal_dialog_form_edit();+ }+ else {+ console.log('No X-wger-redirect found but also no .has-error!');+ $("#wger-ajax-info").modal("hide");+ $("#ajax-info-content form").html(data);+ }- // Note: loading the new page like this executes all its JS code- //$('body').html(data);}// Call other custom initialisation functionsdiff --git a/wger/manager/templates/tags/render_form_errors.html b/wger/manager/templates/tags/render_form_errors.htmlindex f925ac077..edab32c0d 100644--- a/wger/manager/templates/tags/render_form_errors.html+++ b/wger/manager/templates/tags/render_form_errors.html@@ -1,7 +1,7 @@{% load i18n %}{% if form.is_bound and not form.is_valid or form.non_field_errors or form.non_form_errors %}-<div class="alert alert-danger">+<div class="alert alert-danger has-error">{% blocktrans %}The form contains errors. Please verify and correct thedata in the fields in red and submit the form again.{% endblocktrans %}diff --git a/wger/settings_global.py b/wger/settings_global.pyindex cfef38959..a7207ab23 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -92,6 +92,9 @@ MIDDLEWARE_CLASSES = ('django.contrib.sessions.middleware.SessionMiddleware','django.middleware.csrf.CsrfViewMiddleware',+ # Javascript Header. Sends helper headers for AJAX+ 'wger.utils.middleware.JavascriptAJAXRedirectionMiddleware',+# Custom authentication middleware. Creates users on-the-fly for certain paths'wger.utils.middleware.WgerAuthenticationMiddleware',diff --git a/wger/utils/middleware.py b/wger/utils/middleware.pyindex 5bd54e477..1c7d90fc2 100644--- a/wger/utils/middleware.py+++ b/wger/utils/middleware.py@@ -13,10 +13,7 @@# You should have received a copy of the GNU Affero General Public License'''-Custom authentication middleware.--This is basically Django's AuthenticationMiddleware, but creates a new temporary-user automatically for anonymous users.+Custom middleware'''import logging@@ -107,3 +104,24 @@ class RobotsExclusionMiddleware(object):return responseelse:return response+++class JavascriptAJAXRedirectionMiddleware(object):+ '''+ Middleware that sends helper headers when working with AJAX.++ This is used for AJAX forms due to limitations of javascript. The way it+ was done before was to load the whole redirected page, then read from a DIV+ in the page and redirect to that URL. This now just sends a header when the+ form was called via the JS function form_modal_dialog() and no errors are+ present.+ '''++ def process_response(self, request, response):++ if request.META.get('HTTP_X_WGER_NO_MESSAGES') and b'has-error' not in response.content:++ logger.debug('Sending X-wger-redirect')+ response['X-wger-redirect'] = request.path+ response.content = request.path+ return response