1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
18 from werkzeug.wrappers import Response as wz_Response
19 from mediagoblin.tools.template import render_template
20 from mediagoblin.tools.translate import (lazy_pass_to_ugettext as _,
22 from mediagoblin.db.models import UserBan
24 class Response(wz_Response):
25 """Set default response mimetype to HTML, otherwise we get text/plain"""
26 default_mimetype = u'text/html'
29 def render_to_response(request, template, context, status=200):
30 """Much like Django's shortcut.render()"""
32 render_template(request, template, context),
36 def render_error(request, status=500, title=_('Oops!'),
37 err_msg=_('An error occured')):
38 """Render any error page with a given error code, title and text body
40 Title and description are passed through as-is to allow html. Make
41 sure no user input is contained therein for security reasons. The
42 description will be wrapped in <p></p> tags.
44 return Response(render_template(request, 'mediagoblin/error.html',
45 {'err_code': status, 'title': title, 'err_msg': err_msg}),
49 def render_403(request):
50 """Render a standard 403 page"""
52 title = _('Operation not allowed')
53 err_msg = _("Sorry Dave, I can't let you do that!</p><p>You have tried "
54 " to perform a function that you are not allowed to. Have you "
55 "been trying to delete all user accounts again?")
56 return render_error(request, 403, title, err_msg)
58 def render_404(request):
59 """Render a standard 404 page."""
61 err_msg = _("There doesn't seem to be a page at this address. Sorry!</p>"
62 "<p>If you're sure the address is correct, maybe the page "
63 "you're looking for has been moved or deleted.")
64 return render_error(request, 404, err_msg=err_msg)
66 def render_user_banned(request):
67 """Renders the page which tells a user they have been banned, for how long
68 and the reason why they have been banned"
70 user_ban = UserBan.query.get(request.user.id)
71 return render_to_response(request,
72 'mediagoblin/banned.html',
73 {'reason':user_ban.reason,
74 'expiration_date':user_ban.expiration_date})
76 def render_http_exception(request, exc, description):
77 """Return Response() given a werkzeug.HTTPException
79 :param exc: werkzeug.HTTPException or subclass thereof
80 :description: message describing the error."""
81 # If we were passed the HTTPException stock description on
82 # exceptions where we have localized ones, use those:
83 stock_desc = (description == exc.__class__.description)
85 if stock_desc and exc.code == 403:
86 return render_403(request)
87 elif stock_desc and exc.code == 404:
88 return render_404(request)
90 return render_error(request, title=exc.args[0],
95 def redirect(request, *args, **kwargs):
96 """Redirects to an URL, using urlgen params or location string
98 :param querystring: querystring to be appended to the URL
99 :param location: If the location keyword is given, redirect to the URL
101 querystring = kwargs.pop('querystring', None)
103 # Redirect to URL if given by "location=..."
104 if 'location' in kwargs:
105 location = kwargs.pop('location')
107 location = request.urlgen(*args, **kwargs)
110 location += querystring
111 return werkzeug.utils.redirect(location)
114 def redirect_obj(request, obj):
115 """Redirect to the page for the given object.
117 Requires obj to have a .url_for_self method."""
118 return redirect(request, location=obj.url_for_self(request.urlgen))