From 9150244afa45628dd752a67272129d30d6c72224 Mon Sep 17 00:00:00 2001 From: Elrond Date: Sun, 5 Jun 2011 15:49:08 +0200 Subject: [PATCH] Create redirect shortcut and use it around This is just replacing exc.HTTPFound(location=request.urlgen(...)) by redirect(request, ...). No magic. --- mediagoblin/auth/views.py | 15 +++++---------- mediagoblin/decorators.py | 6 +++--- mediagoblin/edit/views.py | 8 +++----- mediagoblin/submit/views.py | 7 ++----- mediagoblin/util.py | 7 ++++++- 5 files changed, 19 insertions(+), 24 deletions(-) diff --git a/mediagoblin/auth/views.py b/mediagoblin/auth/views.py index d54e673c..36d23e53 100644 --- a/mediagoblin/auth/views.py +++ b/mediagoblin/auth/views.py @@ -18,7 +18,7 @@ import uuid from webob import exc -from mediagoblin.util import render_to_response +from mediagoblin.util import render_to_response, redirect from mediagoblin.db.util import ObjectId from mediagoblin.auth import lib as auth_lib from mediagoblin.auth import forms as auth_forms @@ -54,9 +54,7 @@ def register(request): send_verification_email(entry, request) - # Redirect to register_success - return exc.HTTPFound( - location=request.urlgen("mediagoblin.auth.register_success")) + return redirect(request, "mediagoblin.auth.register_success") return render_to_response(request, 'mediagoblin/auth/register.html', @@ -90,8 +88,7 @@ def login(request): if request.POST.get('next'): return exc.HTTPFound(location=request.POST['next']) else: - return exc.HTTPFound( - location=request.urlgen("index")) + return redirect(request, "index") else: # Prevent detecting who's on this system by testing login @@ -110,8 +107,7 @@ def logout(request): # Maybe deleting the user_id parameter would be enough? request.session.delete() - return exc.HTTPFound( - location=request.urlgen("index")) + return redirect(request, "index") def verify_email(request): @@ -164,8 +160,7 @@ def resend_activation(request): send_verification_email(request.user, request) - return exc.HTTPFound( - location=request.urlgen('mediagoblin.auth.resend_verification_success')) + return redirect(request, 'mediagoblin.auth.resend_verification_success') def resend_activation_success(request): diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py index 34575320..c2fe3f9f 100644 --- a/mediagoblin/decorators.py +++ b/mediagoblin/decorators.py @@ -18,6 +18,7 @@ from bson.errors import InvalidId from webob import exc +from mediagoblin.util import redirect from mediagoblin.db.util import ObjectId @@ -38,9 +39,8 @@ def require_active_login(controller): def new_controller_func(request, *args, **kwargs): if request.user and \ request.user.get('status') == u'needs_email_verification': - return exc.HTTPFound( - location = request.urlgen( - 'mediagoblin.auth.verify_email_notice')) + return redirect(request, + 'mediagoblin.auth.verify_email_notice') elif not request.user or request.user.get('status') != u'active': return exc.HTTPFound( location="%s?next=%s" % ( diff --git a/mediagoblin/edit/views.py b/mediagoblin/edit/views.py index 258f14e3..04b73567 100644 --- a/mediagoblin/edit/views.py +++ b/mediagoblin/edit/views.py @@ -17,7 +17,7 @@ from webob import exc -from mediagoblin.util import render_to_response +from mediagoblin.util import render_to_response, redirect from mediagoblin.edit import forms from mediagoblin.edit.lib import may_edit_media from mediagoblin.decorators import require_active_login, get_user_media_entry @@ -51,10 +51,8 @@ def edit_media(request, media): media['slug'] = request.POST['slug'] media.save() - # redirect - return exc.HTTPFound( - location=request.urlgen("mediagoblin.user_pages.media_home", - user=media.uploader()['username'], media=media['slug'])) + return redirect(request, "mediagoblin.user_pages.media_home", + user=media.uploader()['username'], media=media['slug']) return render_to_response(request, 'mediagoblin/edit/edit.html', diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index 95257b72..d4ecc75a 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -17,10 +17,9 @@ from os.path import splitext from cgi import FieldStorage -from webob import exc from werkzeug.utils import secure_filename -from mediagoblin.util import render_to_response +from mediagoblin.util import render_to_response, redirect from mediagoblin.decorators import require_active_login from mediagoblin.submit import forms as submit_forms from mediagoblin.process_media import process_media_initial @@ -76,9 +75,7 @@ def submit_start(request): # queue it for processing process_media_initial.delay(unicode(entry['_id'])) - # redirect - return exc.HTTPFound( - location=request.urlgen("mediagoblin.submit.success")) + return redirect(request, "mediagoblin.submit.success") return render_to_response(request, 'mediagoblin/submit/start.html', diff --git a/mediagoblin/util.py b/mediagoblin/util.py index f69c91f2..a0a09adf 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -29,7 +29,7 @@ from babel.localedata import exists import jinja2 import translitcodec from paste.deploy.loadwsgi import NicerConfigParser -from webob import Response +from webob import Response, exc from mediagoblin import globals as mgoblin_globals from mediagoblin.db.util import ObjectId @@ -123,6 +123,11 @@ def render_to_response(request, template, context): return Response(render_template(request, template, context)) +def redirect(request, *args, **kwargs): + """Returns a HTTPFound(), takes a request and then urlgen params""" + return exc.HTTPFound(location=request.urlgen(*args, **kwargs)) + + def setup_user_in_request(request): """ Examine a request and tack on a request.user parameter if that's -- 2.25.1