summaryrefslogtreecommitdiff
path: root/app/utils.py
blob: caacea7c6e8d57bf89ad17fec07d3997e696c0b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)