# HG changeset patch # User Daniel Dourvaris # Date 2017-06-14 09:07:54 # Node ID 1c6b274be870a1aaf9b38d4ab27224deddb25c4b # Parent f248a72cc282bcedb685891ae3fc83d0e9193585 api: attach the call context variables to request for later usage in API calls. diff --git a/rhodecode/api/__init__.py b/rhodecode/api/__init__.py --- a/rhodecode/api/__init__.py +++ b/rhodecode/api/__init__.py @@ -35,8 +35,9 @@ from pyramid.httpexceptions import HTTPN from rhodecode.api.exc import ( JSONRPCBaseError, JSONRPCError, JSONRPCForbidden, JSONRPCValidationError) +from rhodecode.apps._base import TemplateArgs from rhodecode.lib.auth import AuthUser -from rhodecode.lib.base import get_ip_addr +from rhodecode.lib.base import get_ip_addr, attach_context_attributes from rhodecode.lib.ext_json import json from rhodecode.lib.utils2 import safe_str from rhodecode.lib.plugins.utils import get_plugin_settings @@ -278,6 +279,11 @@ def request_view(request): 'request': request, 'apiuser': auth_u }) + + # register some common functions for usage + attach_context_attributes(TemplateArgs(), request, request.rpc_user.user_id, + attach_to_request=True) + try: ret_value = func(**call_params) return jsonrpc_response(request, ret_value) diff --git a/rhodecode/lib/base.py b/rhodecode/lib/base.py --- a/rhodecode/lib/base.py +++ b/rhodecode/lib/base.py @@ -265,7 +265,7 @@ class BasicAuth(AuthBasicAuthenticator): __call__ = authenticate -def attach_context_attributes(context, request, user_id): +def attach_context_attributes(context, request, user_id, attach_to_request=False): """ Attach variables into template context called `c`, please note that request could be pylons or pyramid request in here. @@ -388,7 +388,11 @@ def attach_context_attributes(context, r context.backends = rhodecode.BACKENDS.keys() context.backends.sort() context.unread_notifications = NotificationModel().get_unread_cnt_for_user(user_id) - context.pyramid_request = pyramid.threadlocal.get_current_request() + if attach_to_request: + request.call_context = context + else: + context.pyramid_request = pyramid.threadlocal.get_current_request() + def get_auth_user(environ):