Extend redirect helper to take optional location keyword

In order to move away from webob with its redirect(location=...) we
need to provide a redirect function that allows to directly specify
the URL rather than the urlgen parameters that we now use.

Extend our MG.tools:redirect helper so we can pass in the direct URL
via the optional "location" keyword.

This commit does not switch over any redirect consumers yet.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2012-11-16 09:12:34 +01:00
parent 30bb4109bc
commit 4487d51c81

View File

@ -14,7 +14,8 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from webob import Response, exc import werkzeug.utils
from webob import Response
from mediagoblin.tools.template import render_template from mediagoblin.tools.template import render_template
from mediagoblin.tools.translate import (lazy_pass_to_ugettext as _, from mediagoblin.tools.translate import (lazy_pass_to_ugettext as _,
pass_to_ugettext) pass_to_ugettext)
@ -57,15 +58,21 @@ def render_404(request):
"you're looking for has been moved or deleted.") "you're looking for has been moved or deleted.")
return render_error(request, 404, err_msg=err_msg) return render_error(request, 404, err_msg=err_msg)
def redirect(request, *args, **kwargs): 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
querystring = None :param querystring: querystring to be appended to the URL
if kwargs.get('querystring'): :param location: If the location keyword is given, redirect to the URL
querystring = kwargs.get('querystring') """
del kwargs['querystring'] querystring = kwargs.pop('querystring', None)
return exc.HTTPFound( # Redirect to URL if given by "location=..."
location=''.join([ if 'location' in kwargs:
request.urlgen(*args, **kwargs), location = kwargs.pop('location')
querystring if querystring else ''])) else:
location = request.urlgen(*args, **kwargs)
if querystring:
location += querystring
return werkzeug.utils.redirect(location)