From d7dcacb899bf7bc13cfd4176748282a2e9e3ac1c Mon Sep 17 00:00:00 2001 From: Aaron Kable Date: Fri, 17 Apr 2020 06:45:01 +0000 Subject: [PATCH] Add 500 and 400, 403, 404 error redirects back to dashboard with basic message --- .../project_template/project_name/urls.py | 5 +++ .../modules/discord/tests/test_views.py | 7 +++- allianceauth/views.py | 42 ++++++++++++++++++- tests/urls.py | 5 ++- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/allianceauth/project_template/project_name/urls.py b/allianceauth/project_template/project_name/urls.py index ecff76d8..e9afaaf0 100644 --- a/allianceauth/project_template/project_name/urls.py +++ b/allianceauth/project_template/project_name/urls.py @@ -4,3 +4,8 @@ from allianceauth import urls urlpatterns = [ url(r'', include(urls)), ] + +handler500 = 'allianceauth.views.Generic500Redirect' +handler404 = 'allianceauth.views.Generic404Redirect' +handler403 = 'allianceauth.views.Generic403Redirect' +handler400 = 'allianceauth.views.Generic400Redirect' diff --git a/allianceauth/services/modules/discord/tests/test_views.py b/allianceauth/services/modules/discord/tests/test_views.py index 8de0e2eb..68103078 100644 --- a/allianceauth/services/modules/discord/tests/test_views.py +++ b/allianceauth/services/modules/discord/tests/test_views.py @@ -27,7 +27,12 @@ class DiscordViewsTestCase(WebTest): self.login() manager.generate_oauth_redirect_url.return_value = '/example.com/oauth/' response = self.app.get('/discord/activate/', auto_follow=False) - self.assertRedirects(response, expected_url='/example.com/oauth/', target_status_code=404) + self.assertRedirects( + response, + expected_url="/example.com/oauth/", + target_status_code=404, + fetch_redirect_response=False, + ) @mock.patch(MODULE_PATH + '.tasks.DiscordOAuthManager') def test_callback(self, manager): diff --git a/allianceauth/views.py b/allianceauth/views.py index f6f91304..2f830bc7 100644 --- a/allianceauth/views.py +++ b/allianceauth/views.py @@ -1,13 +1,15 @@ from django.views.generic.base import View from django.http import HttpResponseRedirect +from django.shortcuts import redirect +from django.contrib import messages class NightModeRedirectView(View): - SESSION_VAR = 'NIGHT_MODE' + SESSION_VAR = "NIGHT_MODE" def get(self, request, *args, **kwargs): request.session[self.SESSION_VAR] = not self.night_mode_state(request) - return HttpResponseRedirect(request.GET.get('next', '/')) + return HttpResponseRedirect(request.GET.get("next", "/")) @classmethod def night_mode_state(cls, request): @@ -17,3 +19,39 @@ class NightModeRedirectView(View): # Session is middleware # Sometimes request wont have a session attribute return False + + +def Generic500Redirect(request): + messages.error( + request, + "Auth encountered an error processing your request, please try again. " + "If the error persists, please contact the administrators. (500 Internal Server Error)", + ) + return redirect("authentication:dashboard") + + +def Generic404Redirect(request, exception): + messages.error( + request, + "Page does not exist. If you believe this is in error please contact the administrators. " + "(404 Page Not Found)", + ) + return redirect("authentication:dashboard") + + +def Generic403Redirect(request, exception): + messages.error( + request, + "You do not have permission to access the requested page. " + "If you believe this is in error please contact the administrators. (403 Permission Denied)", + ) + return redirect("authentication:dashboard") + + +def Generic400Redirect(request, exception): + messages.error( + request, + "Auth encountered an error processing your request, please try again. " + "If the error persists, please contact the administrators. (400 Bad Request)", + ) + return redirect("authentication:dashboard") diff --git a/tests/urls.py b/tests/urls.py index 8c2b2233..12f563e6 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -12,4 +12,7 @@ urlpatterns += [ url(r'^second-page/$', views.page, name='p1'), ] - +handler500 = 'allianceauth.views.Generic500Redirect' +handler404 = 'allianceauth.views.Generic404Redirect' +handler403 = 'allianceauth.views.Generic403Redirect' +handler400 = 'allianceauth.views.Generic400Redirect'