Implement generic error pages
authorSebastian Spaeth <Sebastian@SSpaeth.de>
Thu, 29 Nov 2012 07:57:12 +0000 (08:57 +0100)
committerSebastian Spaeth <Sebastian@SSpaeth.de>
Thu, 29 Nov 2012 08:01:08 +0000 (09:01 +0100)
Rather than having a 404.html, a 403.html, a 500.html,...
we have a generic error.html template that we pass in an
error code, a title and a (html'ish) error message.

Implement the common render_404 and render_403 shortcuts. More exotic
cases can be achieved by the generic render_error function.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
mediagoblin/templates/mediagoblin/error.html [moved from mediagoblin/templates/mediagoblin/404.html with 70% similarity]
mediagoblin/tools/response.py

similarity index 70%
rename from mediagoblin/templates/mediagoblin/404.html
rename to mediagoblin/templates/mediagoblin/error.html
index c0fe8b62d81f8926c1d2e13302595ccf1d63c9c7..c16b650f14fe11ea44c7a66b1693aea66d16794d 100644 (file)
 #}
 {% extends "mediagoblin/base.html" %}
 
-{% block title %}404 &mdash; {{ super() }}{% endblock %}
+{% block title %}{{err_code}} &mdash; {{ super() }}{% endblock %}
 
 {% block mediagoblin_content %}
   <img class="right_align" src="{{ request.staticdirect('/images/404.png') }}"
-       alt="{% trans %}Image of 404 goblin stressing out{% endtrans %}" />
-  <h1>{% trans %}Oops!{% endtrans %}</h1>
-  <p>{% trans %}There doesn't seem to be a page at this address. Sorry!{% endtrans %}</p>
-  <p>
-    {%- trans %}If you're sure the address is correct, maybe the page you're looking for has been moved or deleted.{% endtrans -%}
-  </p>
+       alt="{% trans %}Image of goblin stressing out{% endtrans %}" />
+  <h1>{{ title }}</h1>
+  <p>{{ err_msg|safe }}</p>
   <div class="clear"></div>
 {% endblock %}
index a54c32fb5b1caaff8103a0f15d8254d39bc5b913..a77f68b922d658530afb2512f4a2ffb9319b0d5f 100644 (file)
@@ -16,6 +16,7 @@
 
 from webob import Response, exc
 from mediagoblin.tools.template import render_template
+from mediagoblin.tools.translate import fake_ugettext_passthrough as _
 
 
 def render_to_response(request, template, context, status=200):
@@ -25,14 +26,34 @@ def render_to_response(request, template, context, status=200):
         status=status)
 
 
-def render_404(request):
-    """
-    Render a 404.
+def render_error(request, status=500, title=_('Oops!'),
+                 err_msg=_('An error occured')):
+    """Render any error page with a given error code, title and text body
+
+    Title and description are passed through as-is to allow html. Make
+    sure no user input is contained therein for security reasons. The
+    description will be wrapped in a <p> tag.
     """
-    return render_to_response(
-        request, 'mediagoblin/404.html', {}, status=404)
+    return Response(render_template(request, 'mediagoblin/error.html',
+        {'err_code': status, 'title': title, 'err_msg': err_msg}),
+        status=status)
 
 
+def render_403(request):
+    """Render a standard 403 page"""
+    title = _('Operation not allowed')
+    err_msg = _("Sorry Dave, I can't let you do that!</p><p>You have tried "
+                " to perform a function that you are not allowed to. Have you "
+                "been trying to delete all user accounts again?")
+    return render_error(request, 403, title, err_msg)
+
+def render_404(request):
+    """Render a standard 404 page."""
+    err_msg = _("<p>There doesn't seem to be a page at this address. Sorry!</p>"
+                "<p>If you're sure the address is correct, maybe the page "
+                "you're looking for has been moved or deleted.")
+    return render_error(request, 404, err_msg=err_msg)
+
 def redirect(request, *args, **kwargs):
     """Returns a HTTPFound(), takes a request and then urlgen params"""