From 4ad1b29d831633de1430a683c4ad37873007d34c Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Fri, 31 Jul 2015 13:20:55 +0200 Subject: redoing the etag matching mechanism --- pyaggr3g470r/lib/view_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 pyaggr3g470r/lib/view_utils.py (limited to 'pyaggr3g470r/lib/view_utils.py') diff --git a/pyaggr3g470r/lib/view_utils.py b/pyaggr3g470r/lib/view_utils.py new file mode 100644 index 00000000..fa5e1eec --- /dev/null +++ b/pyaggr3g470r/lib/view_utils.py @@ -0,0 +1,20 @@ +from functools import wraps +from flask import request, Response, make_response +from pyaggr3g470r.lib.utils import to_hash + + +def etag_match(func): + @wraps(func) + def wrapper(*args, **kwargs): + response = func(*args, **kwargs) + if not type(response) is str: + return response + etag = to_hash(response) + if request.headers.get('if-none-match') == etag: + response = Response(status=304, headers={'etag': etag, + 'Cache-Control': 'pragma: no-cache'}) + else: + response = make_response(response) + response.headers['etag'] = etag + return response + return wrapper -- cgit From 0caffceec8b58bc3f78c0d8ea36d2f7e9da668ec Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Mon, 3 Aug 2015 14:36:13 +0200 Subject: sqlalchemy was requesting icons everytime feed where listed so i choosed to move the icons into their own table --- pyaggr3g470r/lib/view_utils.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'pyaggr3g470r/lib/view_utils.py') diff --git a/pyaggr3g470r/lib/view_utils.py b/pyaggr3g470r/lib/view_utils.py index fa5e1eec..0cfe62c4 100644 --- a/pyaggr3g470r/lib/view_utils.py +++ b/pyaggr3g470r/lib/view_utils.py @@ -7,14 +7,20 @@ def etag_match(func): @wraps(func) def wrapper(*args, **kwargs): response = func(*args, **kwargs) - if not type(response) is str: + if isinstance(response, Response): + etag = to_hash(response.data) + headers = response.headers + elif type(response) is str: + etag = to_hash(response) + headers = {} + else: return response - etag = to_hash(response) if request.headers.get('if-none-match') == etag: - response = Response(status=304, headers={'etag': etag, - 'Cache-Control': 'pragma: no-cache'}) - else: + response = Response(status=304) + response.headers['Cache-Control'] \ + = headers.get('Cache-Control', 'pragma: no-cache') + elif not isinstance(response, Response): response = make_response(response) - response.headers['etag'] = etag + response.headers['etag'] = etag return response return wrapper -- cgit