Commit | Line | Data |
---|---|---|
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 | ||
17 | from webob import Response, exc | |
18 | from mediagoblin.tools.template import render_template | |
00da119e CAW |
19 | from mediagoblin.tools.translate import (lazy_pass_to_ugettext as _, |
20 | pass_to_ugettext) | |
03ae172a | 21 | |
ee91c2b8 | 22 | |
03ae172a AW |
23 | def 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 | |
00da119e CAW |
30 | def render_error(request, status=500, title=_('Oops!'), |
31 | err_msg=_('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 |
43 | def render_403(request): |
44 | """Render a standard 403 page""" | |
00da119e | 45 | _ = pass_to_ugettext |
6b5f1ca7 SS |
46 | title = _('Operation not allowed') |
47 | err_msg = _("Sorry Dave, I can't let you do that!</p><p>You have tried " | |
48 | " to perform a function that you are not allowed to. Have you " | |
49 | "been trying to delete all user accounts again?") | |
50 | return render_error(request, 403, title, err_msg) | |
51 | ||
52 | def render_404(request): | |
53 | """Render a standard 404 page.""" | |
00da119e | 54 | _ = pass_to_ugettext |
26c71029 | 55 | err_msg = _("There doesn't seem to be a page at this address. Sorry!</p>" |
6b5f1ca7 SS |
56 | "<p>If you're sure the address is correct, maybe the page " |
57 | "you're looking for has been moved or deleted.") | |
58 | return render_error(request, 404, err_msg=err_msg) | |
59 | ||
03ae172a AW |
60 | def redirect(request, *args, **kwargs): |
61 | """Returns a HTTPFound(), takes a request and then urlgen params""" | |
ee91c2b8 | 62 | |
03ae172a AW |
63 | querystring = None |
64 | if kwargs.get('querystring'): | |
65 | querystring = kwargs.get('querystring') | |
66 | del kwargs['querystring'] | |
67 | ||
68 | return exc.HTTPFound( | |
69 | location=''.join([ | |
70 | request.urlgen(*args, **kwargs), | |
71 | querystring if querystring else ''])) |