back.py 808 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
# This snippet is in public domain.
# However, please retain this link in your sources:
# http://flask.pocoo.org/snippets/120/
# Danya Alexeyevsky

import functools
from flask import session, request, redirect as flask_redirect, url_for
import config

cookie = getattr(config, "REDIRECT_BACK_COOKIE", "back")
default_view = getattr(config, "REDIRECT_BACK_DEFAULT", "index")

Robin Sonnabend's avatar
Robin Sonnabend committed
13

14 15 16 17 18 19 20
def anchor(func, cookie=cookie):
    @functools.wraps(func)
    def result(*args, **kwargs):
        session[cookie] = request.url
        return func(*args, **kwargs)
    return result

Robin Sonnabend's avatar
Robin Sonnabend committed
21

22 23 24
def url(default=default_view, cookie=cookie, **url_args):
    return session.get(cookie, url_for(default, **url_args))

Robin Sonnabend's avatar
Robin Sonnabend committed
25

26 27
def redirect(default=default_view, cookie=cookie, **url_args):
    return flask_redirect(url(default, cookie, **url_args))