Media URLs with ids in them are now like /u/cwebber/m/id:4112/ rather than /u/cwebber...
[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
4487d51c 17import werkzeug.utils
b745bb50 18from werkzeug.wrappers import Response as wz_Response
03ae172a 19from mediagoblin.tools.template import render_template
00da119e
CAW
20from mediagoblin.tools.translate import (lazy_pass_to_ugettext as _,
21 pass_to_ugettext)
03ae172a 22
b745bb50
SS
23class Response(wz_Response):
24 """Set default response mimetype to HTML, otherwise we get text/plain"""
25 default_mimetype = u'text/html'
26
ee91c2b8 27
03ae172a
AW
28def render_to_response(request, template, context, status=200):
29 """Much like Django's shortcut.render()"""
30 return Response(
31 render_template(request, template, context),
32 status=status)
33
ee91c2b8 34
00da119e
CAW
35def render_error(request, status=500, title=_('Oops!'),
36 err_msg=_('An error occured')):
6b5f1ca7
SS
37 """Render any error page with a given error code, title and text body
38
39 Title and description are passed through as-is to allow html. Make
40 sure no user input is contained therein for security reasons. The
26c71029 41 description will be wrapped in <p></p> tags.
03ae172a 42 """
6b5f1ca7
SS
43 return Response(render_template(request, 'mediagoblin/error.html',
44 {'err_code': status, 'title': title, 'err_msg': err_msg}),
45 status=status)
03ae172a 46
ee91c2b8 47
6b5f1ca7
SS
48def render_403(request):
49 """Render a standard 403 page"""
00da119e 50 _ = pass_to_ugettext
6b5f1ca7
SS
51 title = _('Operation not allowed')
52 err_msg = _("Sorry Dave, I can't let you do that!</p><p>You have tried "
53 " to perform a function that you are not allowed to. Have you "
54 "been trying to delete all user accounts again?")
55 return render_error(request, 403, title, err_msg)
56
57def render_404(request):
58 """Render a standard 404 page."""
00da119e 59 _ = pass_to_ugettext
26c71029 60 err_msg = _("There doesn't seem to be a page at this address. Sorry!</p>"
6b5f1ca7
SS
61 "<p>If you're sure the address is correct, maybe the page "
62 "you're looking for has been moved or deleted.")
63 return render_error(request, 404, err_msg=err_msg)
64
4487d51c 65
785b287f
SS
66def render_http_exception(request, exc, description):
67 """Return Response() given a werkzeug.HTTPException
68
69 :param exc: werkzeug.HTTPException or subclass thereof
70 :description: message describing the error."""
71 # If we were passed the HTTPException stock description on
72 # exceptions where we have localized ones, use those:
73 stock_desc = (description == exc.__class__.description)
74
75 if stock_desc and exc.code == 403:
76 return render_403(request)
77 elif stock_desc and exc.code == 404:
78 return render_404(request)
79
80 return render_error(request, title=exc.args[0],
81 err_msg=description,
82 status=exc.code)
83
84
03ae172a 85def redirect(request, *args, **kwargs):
4487d51c
SS
86 """Redirects to an URL, using urlgen params or location string
87
88 :param querystring: querystring to be appended to the URL
89 :param location: If the location keyword is given, redirect to the URL
90 """
91 querystring = kwargs.pop('querystring', None)
ee91c2b8 92
4487d51c
SS
93 # Redirect to URL if given by "location=..."
94 if 'location' in kwargs:
95 location = kwargs.pop('location')
96 else:
97 location = request.urlgen(*args, **kwargs)
03ae172a 98
4487d51c
SS
99 if querystring:
100 location += querystring
101 return werkzeug.utils.redirect(location)