From aa953f76a1a43249fca4b51d4db8f21bdf27d886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 13 Oct 2016 10:01:29 +0200 Subject: [PATCH] Cache FlaskInternalApi object on request keyed by authentication token. --- pillar/web/system_util.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pillar/web/system_util.py b/pillar/web/system_util.py index ebc27384..0999c638 100644 --- a/pillar/web/system_util.py +++ b/pillar/web/system_util.py @@ -35,24 +35,29 @@ def pillar_server_endpoint_static(): def pillar_api(token=None): - # Cache API objects on the request. - api = getattr(request, 'pillar_api', None) + # Cache API objects on the request per token. + api = getattr(request, 'pillar_api', {}).get(token) if api is not None: return api # Check if current_user is initialized (in order to support manage.py # scripts and non authenticated server requests). + use_token = token if token is None and current_user and current_user.is_authenticated: - token = current_user.id + use_token = current_user.id api = FlaskInternalApi( endpoint=pillar_server_endpoint(), username=None, password=None, - token=token + token=use_token ) - request.pillar_api = api + if token is None: + if not hasattr(request, 'pillar_api'): + request.pillar_api = {} + request.pillar_api[token] = api + return api