Add comments explaining how translitcodec *is* used
[mediagoblin.git] / mediagoblin / tools / response.py
CommitLineData
03ae172a 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
03ae172a
AW
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from webob import Response, exc
18from mediagoblin.tools.template import render_template
18a52dac
SS
19from mediagoblin.tools.translate import (lazy_pass_to_ugettext as L_,
20 pass_to_ugettext as _)
03ae172a 21
ee91c2b8 22
03ae172a
AW
23def render_to_response(request, template, context, status=200):
24 """Much like Django's shortcut.render()"""
25 return Response(
26 render_template(request, template, context),
27 status=status)
28
ee91c2b8 29
18a52dac
SS
30def render_error(request, status=500, title=L_('Oops!'),
31 err_msg=L_('An error occured')):
6b5f1ca7
SS
32 """Render any error page with a given error code, title and text body
33
34 Title and description are passed through as-is to allow html. Make
35 sure no user input is contained therein for security reasons. The
26c71029 36 description will be wrapped in <p></p> tags.
03ae172a 37 """
6b5f1ca7
SS
38 return Response(render_template(request, 'mediagoblin/error.html',
39 {'err_code': status, 'title': title, 'err_msg': err_msg}),
40 status=status)
03ae172a 41
ee91c2b8 42
6b5f1ca7
SS
43def render_403(request):
44 """Render a standard 403 page"""
45 title = _('Operation not allowed')
46 err_msg = _("Sorry Dave, I can't let you do that!</p><p>You have tried "
47 " to perform a function that you are not allowed to. Have you "
48 "been trying to delete all user accounts again?")
49 return render_error(request, 403, title, err_msg)
50
51def render_404(request):
52 """Render a standard 404 page."""
26c71029 53 err_msg = _("There doesn't seem to be a page at this address. Sorry!</p>"
6b5f1ca7
SS
54 "<p>If you're sure the address is correct, maybe the page "
55 "you're looking for has been moved or deleted.")
56 return render_error(request, 404, err_msg=err_msg)
57
03ae172a
AW
58def redirect(request, *args, **kwargs):
59 """Returns a HTTPFound(), takes a request and then urlgen params"""
ee91c2b8 60
03ae172a
AW
61 querystring = None
62 if kwargs.get('querystring'):
63 querystring = kwargs.get('querystring')
64 del kwargs['querystring']
65
66 return exc.HTTPFound(
67 location=''.join([
68 request.urlgen(*args, **kwargs),
69 querystring if querystring else '']))