summaryrefslogtreecommitdiffstats
path: root/utils/request.py
blob: 8d36ed6885503402b032a60c6e0a5b8c6d68bf25 (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
from flask import g, request

from app import app


def after_this_request(f):
    """
    Decorator to execute methods after the request is handled, to
    modify the response before sending back to the client. This could
    be used to set cookies.
    """

    if not hasattr(g, 'after_request_callbacks'):
        g.after_request_callbacks = []

    g.after_request_callbacks.append(f)
    return f


@app.after_request
def call_after_request_callbacks(response):
    for callback in getattr(g, 'after_request_callbacks', ()):
        callback(response)

    return response