Commit 3dfc2b71 authored by Robin Sonnabend's avatar Robin Sonnabend

Add back.py to common web modules

parent f9dbd00f
# 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")
def anchor(func, cookie=cookie):
@functools.wraps(func)
def result(*args, **kwargs):
session[cookie] = request.url
return func(*args, **kwargs)
return result
def default_url(default, **url_args):
return url_for(default, **url_args)
def url(default=default_view, cookie=cookie, **url_args):
return session.get(cookie, default_url(default, **url_args))
def redirect(default=default_view, cookie=cookie, **url_args):
print(request.url, request.url_rule, default, session.get(cookie))
target = url(default, cookie, **url_args)
if target == request.url:
target = default_url(default, **url_args)
return flask_redirect(target)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment