From 6b5f1ca79b42b977ea5f436ac7ab329fd2da1b6b Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Thu, 29 Nov 2012 08:57:12 +0100 Subject: Implement generic error pages 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 --- mediagoblin/templates/mediagoblin/404.html | 31 ---------------------------- mediagoblin/templates/mediagoblin/error.html | 28 +++++++++++++++++++++++++ mediagoblin/tools/response.py | 31 +++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 36 deletions(-) delete mode 100644 mediagoblin/templates/mediagoblin/404.html create mode 100644 mediagoblin/templates/mediagoblin/error.html diff --git a/mediagoblin/templates/mediagoblin/404.html b/mediagoblin/templates/mediagoblin/404.html deleted file mode 100644 index c0fe8b62..00000000 --- a/mediagoblin/templates/mediagoblin/404.html +++ /dev/null @@ -1,31 +0,0 @@ -{# -# GNU MediaGoblin -- federated, autonomous media hosting -# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -#} -{% extends "mediagoblin/base.html" %} - -{% block title %}404 — {{ super() }}{% endblock %} - -{% block mediagoblin_content %} - {% trans %}Image of 404 goblin stressing out{% endtrans %} -

{% trans %}Oops!{% endtrans %}

-

{% trans %}There doesn't seem to be a page at this address. Sorry!{% endtrans %}

-

- {%- trans %}If you're sure the address is correct, maybe the page you're looking for has been moved or deleted.{% endtrans -%} -

-
-{% endblock %} diff --git a/mediagoblin/templates/mediagoblin/error.html b/mediagoblin/templates/mediagoblin/error.html new file mode 100644 index 00000000..c16b650f --- /dev/null +++ b/mediagoblin/templates/mediagoblin/error.html @@ -0,0 +1,28 @@ +{# +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +#} +{% extends "mediagoblin/base.html" %} + +{% block title %}{{err_code}} — {{ super() }}{% endblock %} + +{% block mediagoblin_content %} + {% trans %}Image of goblin stressing out{% endtrans %} +

{{ title }}

+

{{ err_msg|safe }}

+
+{% endblock %} diff --git a/mediagoblin/tools/response.py b/mediagoblin/tools/response.py index a54c32fb..a77f68b9 100644 --- a/mediagoblin/tools/response.py +++ b/mediagoblin/tools/response.py @@ -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

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!

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 = _("

There doesn't seem to be a page at this address. Sorry!

" + "

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""" -- cgit v1.2.3 From 60de3209b923db71fc126bca4c34e0bf34752726 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Thu, 15 Nov 2012 15:41:06 +0100 Subject: Return code 403 when accessing admin pages without being an admin. Previously we were just returning a 404 page and this confused the heck out of me, as I did not understand why the admin pages were not there at all (I was no admin). Signed-off-by: Sebastian Spaeth --- mediagoblin/admin/views.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mediagoblin/admin/views.py b/mediagoblin/admin/views.py index e6a3eac3..9c14c55c 100644 --- a/mediagoblin/admin/views.py +++ b/mediagoblin/admin/views.py @@ -14,18 +14,19 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from mediagoblin.tools.response import render_to_response, render_404 from mediagoblin.db.util import DESCENDING from mediagoblin.decorators import require_active_login - +from mediagoblin.tools.response import (render_to_response, render_403, + render_404) @require_active_login def admin_processing_panel(request): ''' Show the global processing panel for this instance ''' + # TODO: Why not a "require_admin_login" decorator throwing a 403 exception? if not request.user.is_admin: - return render_404(request) + return render_403(request) processing_entries = request.db.MediaEntry.find( {'state': u'processing'}).sort('created', DESCENDING) -- cgit v1.2.3 From 26c71029b44d4384020b2f928fb18f59b1d1356b Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Thu, 29 Nov 2012 14:51:24 +0100 Subject: Fix error page text Thanks to Elrond for noticing. We wrap error messages in

tags, so there is no need to start the error message with

. DOH Signed-off-by: Sebastian Spaeth --- mediagoblin/tools/response.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mediagoblin/tools/response.py b/mediagoblin/tools/response.py index a77f68b9..6d14b8b7 100644 --- a/mediagoblin/tools/response.py +++ b/mediagoblin/tools/response.py @@ -32,7 +32,7 @@ def render_error(request, status=500, title=_('Oops!'), 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

tag. + description will be wrapped in

tags. """ return Response(render_template(request, 'mediagoblin/error.html', {'err_code': status, 'title': title, 'err_msg': err_msg}), @@ -49,7 +49,7 @@ def render_403(request): def render_404(request): """Render a standard 404 page.""" - err_msg = _("

There doesn't seem to be a page at this address. Sorry!

" + err_msg = _("There doesn't seem to be a page at this address. Sorry!

" "

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) -- cgit v1.2.3