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 _
,
23 class Response(wz_Response
):
24 """Set default response mimetype to HTML, otherwise we get text/plain"""
25 default_mimetype
= u
'text/html'
28 def render_to_response(request
, template
, context
, status
=200):
29 """Much like Django's shortcut.render()"""
31 render_template(request
, template
, context
),
35 def render_error(request
, status
=500, title
=_('Oops!'),
36 err_msg
=_('An error occured')):
37 """Render any error page with a given error code, title and text body
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
41 description will be wrapped in <p></p> tags.
43 return Response(render_template(request
, 'mediagoblin/error.html',
44 {'err_code': status
, 'title': title
, 'err_msg': err_msg
}),
48 def render_403(request
):
49 """Render a standard 403 page"""
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
)
57 def render_404(request
):
58 """Render a standard 404 page."""
60 err_msg
= _("There doesn't seem to be a page at this address. Sorry!</p>"
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
)
66 def render_http_exception(request
, exc
, description
):
67 """Return Response() given a werkzeug.HTTPException
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
)
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
)
80 return render_error(request
, title
=exc
.args
[0],
85 def redirect(request
, *args
, **kwargs
):
86 """Redirects to an URL, using urlgen params or location string
88 :param querystring: querystring to be appended to the URL
89 :param location: If the location keyword is given, redirect to the URL
91 querystring
= kwargs
.pop('querystring', None)
93 # Redirect to URL if given by "location=..."
94 if 'location' in kwargs
:
95 location
= kwargs
.pop('location')
97 location
= request
.urlgen(*args
, **kwargs
)
100 location
+= querystring
101 return werkzeug
.utils
.redirect(location
)
104 def redirect_obj(request
, obj
):
105 """Redirect to the page for the given object.
107 Requires obj to have a .url_for_self method."""
108 return redirect(request
, location
=obj
.url_for_self(request
.urlgen
))