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 | ||
4990b47c | 17 | import json |
18 | ||
4487d51c | 19 | import werkzeug.utils |
b745bb50 | 20 | from werkzeug.wrappers import Response as wz_Response |
03ae172a | 21 | from mediagoblin.tools.template import render_template |
00da119e CAW |
22 | from mediagoblin.tools.translate import (lazy_pass_to_ugettext as _, |
23 | pass_to_ugettext) | |
03ae172a | 24 | |
b745bb50 SS |
25 | class Response(wz_Response): |
26 | """Set default response mimetype to HTML, otherwise we get text/plain""" | |
27 | default_mimetype = u'text/html' | |
28 | ||
ee91c2b8 | 29 | |
03ae172a AW |
30 | def render_to_response(request, template, context, status=200): |
31 | """Much like Django's shortcut.render()""" | |
32 | return Response( | |
33 | render_template(request, template, context), | |
34 | status=status) | |
35 | ||
00da119e CAW |
36 | def render_error(request, status=500, title=_('Oops!'), |
37 | err_msg=_('An error occured')): | |
6b5f1ca7 SS |
38 | """Render any error page with a given error code, title and text body |
39 | ||
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 | |
26c71029 | 42 | description will be wrapped in <p></p> tags. |
03ae172a | 43 | """ |
6b5f1ca7 SS |
44 | return Response(render_template(request, 'mediagoblin/error.html', |
45 | {'err_code': status, 'title': title, 'err_msg': err_msg}), | |
46 | status=status) | |
03ae172a | 47 | |
d41c6a53 | 48 | def render_400(request, err_msg=None): |
49 | """ Render a standard 400 page""" | |
50 | _ = pass_to_ugettext | |
51 | title = _("Bad Request") | |
52 | if err_msg is None: | |
53 | err_msg = _("The request sent to the server is invalid, please double check it") | |
54 | ||
55 | return render_error(request, 400, title, err_msg) | |
56 | ||
6b5f1ca7 SS |
57 | def render_403(request): |
58 | """Render a standard 403 page""" | |
00da119e | 59 | _ = pass_to_ugettext |
6b5f1ca7 SS |
60 | title = _('Operation not allowed') |
61 | err_msg = _("Sorry Dave, I can't let you do that!</p><p>You have tried " | |
62 | " to perform a function that you are not allowed to. Have you " | |
63 | "been trying to delete all user accounts again?") | |
64 | return render_error(request, 403, title, err_msg) | |
65 | ||
66 | def render_404(request): | |
67 | """Render a standard 404 page.""" | |
00da119e | 68 | _ = pass_to_ugettext |
26c71029 | 69 | err_msg = _("There doesn't seem to be a page at this address. Sorry!</p>" |
6b5f1ca7 SS |
70 | "<p>If you're sure the address is correct, maybe the page " |
71 | "you're looking for has been moved or deleted.") | |
72 | return render_error(request, 404, err_msg=err_msg) | |
73 | ||
4487d51c | 74 | |
785b287f SS |
75 | def render_http_exception(request, exc, description): |
76 | """Return Response() given a werkzeug.HTTPException | |
77 | ||
78 | :param exc: werkzeug.HTTPException or subclass thereof | |
79 | :description: message describing the error.""" | |
80 | # If we were passed the HTTPException stock description on | |
81 | # exceptions where we have localized ones, use those: | |
82 | stock_desc = (description == exc.__class__.description) | |
83 | ||
84 | if stock_desc and exc.code == 403: | |
85 | return render_403(request) | |
86 | elif stock_desc and exc.code == 404: | |
87 | return render_404(request) | |
88 | ||
75ee3de3 | 89 | return render_error(request, title='{0} {1}'.format(exc.code, exc.name), |
785b287f SS |
90 | err_msg=description, |
91 | status=exc.code) | |
92 | ||
93 | ||
03ae172a | 94 | def redirect(request, *args, **kwargs): |
4487d51c SS |
95 | """Redirects to an URL, using urlgen params or location string |
96 | ||
97 | :param querystring: querystring to be appended to the URL | |
98 | :param location: If the location keyword is given, redirect to the URL | |
99 | """ | |
100 | querystring = kwargs.pop('querystring', None) | |
ee91c2b8 | 101 | |
4487d51c SS |
102 | # Redirect to URL if given by "location=..." |
103 | if 'location' in kwargs: | |
104 | location = kwargs.pop('location') | |
105 | else: | |
106 | location = request.urlgen(*args, **kwargs) | |
03ae172a | 107 | |
4487d51c SS |
108 | if querystring: |
109 | location += querystring | |
110 | return werkzeug.utils.redirect(location) | |
2e6ee596 E |
111 | |
112 | ||
113 | def redirect_obj(request, obj): | |
114 | """Redirect to the page for the given object. | |
115 | ||
116 | Requires obj to have a .url_for_self method.""" | |
117 | return redirect(request, location=obj.url_for_self(request.urlgen)) | |
4990b47c | 118 | |
119 | def json_response(serializable, _disable_cors=False, *args, **kw): | |
120 | ''' | |
121 | Serializes a json objects and returns a werkzeug Response object with the | |
122 | serialized value as the response body and Content-Type: application/json. | |
123 | ||
124 | :param serializable: A json-serializable object | |
125 | ||
126 | Any extra arguments and keyword arguments are passed to the | |
127 | Response.__init__ method. | |
128 | ''' | |
129 | ||
130 | response = wz_Response(json.dumps(serializable), *args, content_type='application/json', **kw) | |
131 | ||
132 | if not _disable_cors: | |
133 | cors_headers = { | |
134 | 'Access-Control-Allow-Origin': '*', | |
135 | 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS', | |
136 | 'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With'} | |
137 | for key, value in cors_headers.iteritems(): | |
138 | response.headers.set(key, value) | |
139 | ||
140 | return response | |
2b60a56c | 141 | |
142 | def form_response(data, *args, **kwargs): | |
143 | """ | |
144 | Responds using application/x-www-form-urlencoded and returns a werkzeug | |
145 | Response object with the data argument as the body | |
146 | and 'application/x-www-form-urlencoded' as the Content-Type. | |
147 | ||
148 | Any extra arguments and keyword arguments are passed to the | |
149 | Response.__init__ method. | |
150 | """ | |
151 | ||
152 | response = wz_Response( | |
153 | data, | |
154 | content_type="application/x-www-form-urlencoded", | |
155 | *args, | |
156 | **kwargs | |
157 | ) | |
158 | ||
159 | return response |