diff options
Diffstat (limited to 'mediagoblin/tools/response.py')
-rw-r--r-- | mediagoblin/tools/response.py | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/mediagoblin/tools/response.py b/mediagoblin/tools/response.py index 6d14b8b7..80df1f5a 100644 --- a/mediagoblin/tools/response.py +++ b/mediagoblin/tools/response.py @@ -14,9 +14,15 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -from webob import Response, exc +import werkzeug.utils +from werkzeug.wrappers import Response as wz_Response from mediagoblin.tools.template import render_template -from mediagoblin.tools.translate import fake_ugettext_passthrough as _ +from mediagoblin.tools.translate import (lazy_pass_to_ugettext as _, + pass_to_ugettext) + +class Response(wz_Response): + """Set default response mimetype to HTML, otherwise we get text/plain""" + default_mimetype = u'text/html' def render_to_response(request, template, context, status=200): @@ -41,6 +47,7 @@ def render_error(request, status=500, title=_('Oops!'), def render_403(request): """Render a standard 403 page""" + _ = pass_to_ugettext title = _('Operation not allowed') err_msg = _("Sorry Dave, I can't let you do that!</p><p>You have tried " " to perform a function that you are not allowed to. Have you " @@ -49,20 +56,46 @@ def render_403(request): def render_404(request): """Render a standard 404 page.""" + _ = pass_to_ugettext err_msg = _("There doesn't seem to be a page at this address. Sorry!</p>" "<p>If you're sure the address is correct, maybe the page " "you're looking for has been moved or deleted.") return render_error(request, 404, err_msg=err_msg) + +def render_http_exception(request, exc, description): + """Return Response() given a werkzeug.HTTPException + + :param exc: werkzeug.HTTPException or subclass thereof + :description: message describing the error.""" + # If we were passed the HTTPException stock description on + # exceptions where we have localized ones, use those: + stock_desc = (description == exc.__class__.description) + + if stock_desc and exc.code == 403: + return render_403(request) + elif stock_desc and exc.code == 404: + return render_404(request) + + return render_error(request, title=exc.args[0], + err_msg=description, + status=exc.code) + + def redirect(request, *args, **kwargs): - """Returns a HTTPFound(), takes a request and then urlgen params""" + """Redirects to an URL, using urlgen params or location string + + :param querystring: querystring to be appended to the URL + :param location: If the location keyword is given, redirect to the URL + """ + querystring = kwargs.pop('querystring', None) - querystring = None - if kwargs.get('querystring'): - querystring = kwargs.get('querystring') - del kwargs['querystring'] + # Redirect to URL if given by "location=..." + if 'location' in kwargs: + location = kwargs.pop('location') + else: + location = request.urlgen(*args, **kwargs) - return exc.HTTPFound( - location=''.join([ - request.urlgen(*args, **kwargs), - querystring if querystring else ''])) + if querystring: + location += querystring + return werkzeug.utils.redirect(location) |