Skip to content

Commit

Permalink
Adding custom 500 error page
Browse files Browse the repository at this point in the history
  • Loading branch information
bohare committed Aug 29, 2016
1 parent 5f4f629 commit aa2b375
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
6 changes: 5 additions & 1 deletion cadasta/config/urls/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down
27 changes: 20 additions & 7 deletions cadasta/core/tests/test_views_default.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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
24 changes: 18 additions & 6 deletions cadasta/core/views/default.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
Expand 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)))
15 changes: 15 additions & 0 deletions cadasta/templates/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% load i18n %}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>{% trans 'Platform Error' %}</title>
</head>
<body>
<h1>{% trans 'Platform Error' %}</h1>

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

<p>{% trans 'Our developers have been notified.' %}</p>
</body>
</html>

0 comments on commit aa2b375

Please sign in to comment.