From 2c59dcacc2163ac48e36f1fde5fa6b10f769442f Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 19 Nov 2014 03:14:29 +0100 Subject: utils: add after_this_request With after_this_request you can simple set cookies. Used as a decorator, it registers a function as callback to be executed just before sending the response to the client. The callback will be executed with the response object as argument. --- utils.py | 10 +++++++++- views.py | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/utils.py b/utils.py index fbec4c9..c424850 100644 --- a/utils.py +++ b/utils.py @@ -1,5 +1,5 @@ from functools import wraps -from flask import request, render_template +from flask import g, request, render_template from wtforms import Field, ValidationError from widgets import Static @@ -21,6 +21,14 @@ def templated(template=None): return decorated_function return decorator + +def after_this_request(f): + if not hasattr(g, 'after_request_callbacks'): + g.after_request_callbacks = [] + g.after_request_callbacks.append(f) + return f + + class Unique(object): """ validator that checks field uniqueness """ def __init__(self, model, field, message=None): diff --git a/views.py b/views.py index 51b17ef..eed49df 100644 --- a/views.py +++ b/views.py @@ -13,6 +13,14 @@ def get_group_or_404(*query): Member.user == g.user, *query) return group + +@app.after_request +def call_after_request_callbacks(response): + for callback in getattr(g, 'after_request_callbacks', ()): + callback(response) + return response + + @app.route('/', methods=['GET', 'POST']) @templated('index.html') @auth.login_required -- cgit v1.2.3-1-g7c22