From aa2b375012adc6eb1f3fb5ec6e9eae9fbd3be005 Mon Sep 17 00:00:00 2001 From: bohare Date: Mon, 29 Aug 2016 15:12:43 +0100 Subject: [PATCH] Adding custom 500 error page --- cadasta/config/urls/default.py | 6 +++++- cadasta/core/tests/test_views_default.py | 27 ++++++++++++++++++------ cadasta/core/views/default.py | 24 +++++++++++++++------ cadasta/templates/500.html | 15 +++++++++++++ 4 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 cadasta/templates/500.html diff --git a/cadasta/config/urls/default.py b/cadasta/config/urls/default.py index 59c0edc2f..da3a07e48 100644 --- a/cadasta/config/urls/default.py +++ b/cadasta/config/urls/default.py @@ -13,7 +13,11 @@ 1. Add an import: from blog import urls as blog_urls 2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) """ -from django.conf.urls import include, url +from django.conf.urls import include, url, handler500 + +server_error_handler = handler500 + +server_error_handler = 'core.views.default.server_error' api_v1 = [ url(r'^account/', include('accounts.urls.api', namespace='accounts')), diff --git a/cadasta/core/tests/test_views_default.py b/cadasta/core/tests/test_views_default.py index 66d65140d..1d051cdc7 100644 --- a/cadasta/core/tests/test_views_default.py +++ b/cadasta/core/tests/test_views_default.py @@ -1,21 +1,22 @@ import json -from django.http import HttpRequest +from accounts.tests.factories import UserFactory +from core.tests.base_test_case import UserTestCase from django.contrib.auth.models import AnonymousUser +from django.http import HttpRequest from django.template import RequestContext from django.template.loader import render_to_string - -from core.tests.base_test_case import UserTestCase -from tutelary.models import Role -from accounts.tests.factories import UserFactory -from organization.tests.factories import OrganizationFactory, ProjectFactory +from django.test import TestCase from organization.models import OrganizationRole, Project from organization.serializers import ProjectGeometrySerializer +from organization.tests.factories import OrganizationFactory, ProjectFactory +from tutelary.models import Role -from ..views.default import IndexPage, Dashboard +from ..views.default import Dashboard, IndexPage, server_error class IndexPageTest(UserTestCase): + def setUp(self): super().setUp() self.view = IndexPage.as_view() @@ -37,6 +38,7 @@ def test_page_is_rendered_when_user_is_not_signed_in(self): class DashboardTest(UserTestCase): + def setUp(self): super().setUp() self.view = Dashboard.as_view() @@ -117,3 +119,14 @@ def test_get_with_superuser(self): response = self.view(self.request).render() assert response.status_code == 200 self._test_projects_rendered(response, superuser=True) + + +class ServerErrorTest(TestCase): + + def setUp(self): + super().setUp() + self.request = HttpRequest() + + def test_server_error(self): + response = server_error(self.request, template_name='500.html') + assert response.status_code == 500 diff --git a/cadasta/core/views/default.py b/cadasta/core/views/default.py index acd83e688..a3bdeb226 100644 --- a/cadasta/core/views/default.py +++ b/cadasta/core/views/default.py @@ -1,11 +1,10 @@ import json -from django.shortcuts import redirect from core.views.generic import TemplateView -from organization.models import Project, Organization -from tutelary.models import Role - +from django.shortcuts import redirect +from organization.models import Organization, Project from organization.serializers import ProjectGeometrySerializer +from tutelary.models import Role class IndexPage(TemplateView): @@ -29,8 +28,8 @@ def get(self, request, *args, **kwargs): projects = [] superuser = Role.objects.filter(name='superuser') if len(superuser) and hasattr(self.request.user, 'assigned_policies'): - if superuser[0] in self.request.user.assigned_policies(): - projects = Project.objects.filter(extent__isnull=False) + if superuser[0] in self.request.user.assigned_policies(): + projects = Project.objects.filter(extent__isnull=False) if projects == []: if hasattr(self.request.user, 'organizations'): user_orgs = self.request.user.organizations.all() @@ -45,3 +44,16 @@ def get(self, request, *args, **kwargs): extent__isnull=False)) context = self.get_context_data(projects=projects) return super(TemplateView, self).render_to_response(context) + + +def server_error(request, template_name='500.html'): + """ + 500 error handler. + + Templates: `500.html` + Context: None + """ + from django.template import RequestContext, loader + from django.http import HttpResponseServerError + t = loader.get_template(template_name) + return HttpResponseServerError(t.render(RequestContext(request))) diff --git a/cadasta/templates/500.html b/cadasta/templates/500.html new file mode 100644 index 000000000..b202b3cd1 --- /dev/null +++ b/cadasta/templates/500.html @@ -0,0 +1,15 @@ +{% load i18n %} + + + + {% trans 'Platform Error' %} + + +

{% trans 'Platform Error' %}

+ +

{% trans "We're sorry, our platform encountered an error while processing your request." %}

+ +

{% trans 'Our developers have been notified.' %}

+ +