diff options
author | René 'Necoro' Neumann <necoro@necoro.net> | 2013-04-15 23:56:53 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.net> | 2013-04-15 23:56:53 +0200 |
commit | 02172e39c15272f9567bd39a28ec24d0270ea70f (patch) | |
tree | fb0fb3f42ccf41d4406135802b2c7a40c5867266 /app/utils.py | |
parent | 527937a9f6de546fa47270d64f9009ebdbe6fc45 (diff) | |
download | kosten-02172e39c15272f9567bd39a28ec24d0270ea70f.tar.gz kosten-02172e39c15272f9567bd39a28ec24d0270ea70f.tar.bz2 kosten-02172e39c15272f9567bd39a28ec24d0270ea70f.zip |
'templated' and wrapped 'redirect'
Diffstat (limited to 'app/utils.py')
-rw-r--r-- | app/utils.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..caacea7 --- /dev/null +++ b/app/utils.py @@ -0,0 +1,29 @@ +from functools import wraps +from flask import request, render_template, url_for +from flask import redirect as _redirect + +def templated(template=None): + def decorator(f): + @wraps(f) + def decorated_function(*args, **kwargs): + template_name = template + if template_name is None: + template_name = request.endpoint \ + .replace('.', '/') + '.jinja' + ctx = f(*args, **kwargs) + if ctx is None: + ctx = {} + elif not isinstance(ctx, dict): + return ctx + return render_template(template_name, **ctx) + return decorated_function + return decorator + +def redirect (target, **kwargs): + code = kwargs.pop("_code", None) + url = url_for(target, **kwargs) + + if code is None: + return _redirect(url) + else: + return _redirect(url, code) |