core: use proper event to bootstrap pylons env.

- allows to skip this for VCS and API calls in more efficient way.
- fixes #5164
This commit is contained in:
Marcin Kuzminski 2017-01-12 21:47:02 +01:00
parent e02a88e64d
commit d969440caa
2 changed files with 58 additions and 43 deletions

View file

@ -30,6 +30,14 @@ from threading import Thread
from rhodecode.translation import _ as tsf
import rhodecode
from pylons.i18n.translation import _get_translator
from pylons.util import ContextObj
from routes.util import URLGenerator
from rhodecode.lib.base import attach_context_attributes, get_auth_user
log = logging.getLogger(__name__)
@ -63,7 +71,8 @@ def add_localizer(event):
def set_user_lang(event):
cur_user = getattr(event.request, 'user', None)
request = event.request
cur_user = getattr(request, 'user', None)
if cur_user:
user_lang = cur_user.get_instance().user_data.get('language')
@ -72,6 +81,44 @@ def set_user_lang(event):
event.request._LOCALE_ = user_lang
def add_pylons_context(event):
request = event.request
config = rhodecode.CONFIG
environ = request.environ
session = request.session
if hasattr(request, 'vcs_call'):
# skip vcs calls
return
# Setup pylons globals.
pylons.config._push_object(config)
pylons.request._push_object(request)
pylons.session._push_object(session)
pylons.translator._push_object(_get_translator(config.get('lang')))
pylons.url._push_object(URLGenerator(config['routes.map'], environ))
session_key = (
config['pylons.environ_config'].get('session', 'beaker.session'))
environ[session_key] = session
if hasattr(request, 'rpc_method'):
# skip api calls
return
# Get the rhodecode auth user object and make it available.
auth_user = get_auth_user(environ)
request.user = auth_user
environ['rc_auth_user'] = auth_user
# Setup the pylons context object ('c')
context = ContextObj()
context.rhodecode_user = auth_user
attach_context_attributes(context, request)
pylons.tmpl_context._push_object(context)
def scan_repositories_if_enabled(event):
"""
This is subscribed to the `pyramid.events.ApplicationCreated` event. It

View file

@ -20,33 +20,21 @@
import logging
import pylons
import rhodecode
from pylons.i18n.translation import _get_translator
from pylons.util import ContextObj
from routes.util import URLGenerator
from rhodecode.lib.base import attach_context_attributes, get_auth_user
from rhodecode.lib.middleware.vcs import (
detect_vcs_request, VCS_TYPE_KEY, VCS_TYPE_SKIP)
from rhodecode.model import meta
log = logging.getLogger(__name__)
def pylons_compatibility_tween_factory(handler, registry):
def vcs_detection_tween_factory(handler, registry):
def pylons_compatibility_tween(request):
def vcs_detection_tween(request):
"""
While migrating from pylons to pyramid we need to call some pylons code
from pyramid. For example while rendering an old template that uses the
'c' or 'h' objects. This tween sets up the needed pylons globals.
Do detection of vcs type, and save results for other layers to re-use
this information
"""
config = rhodecode.CONFIG
environ = request.environ
session = request.session
vcs_handler = detect_vcs_request(
request.environ, request.registry.settings.get('vcs.backends'))
@ -54,38 +42,15 @@ def pylons_compatibility_tween_factory(handler, registry):
if vcs_handler:
# save detected VCS type for later re-use
request.environ[VCS_TYPE_KEY] = vcs_handler.SCM
request.vcs_call = vcs_handler.SCM
return handler(request)
# mark that we didn't detect an VCS, and we can skip detection later on
request.environ[VCS_TYPE_KEY] = VCS_TYPE_SKIP
# Setup pylons globals.
pylons.config._push_object(config)
pylons.request._push_object(request)
pylons.session._push_object(session)
session_key = (
config['pylons.environ_config'].get('session', 'beaker.session'))
environ[session_key] = session
pylons.url._push_object(URLGenerator(config['routes.map'], environ))
# TODO: Maybe we should use the language from pyramid.
translator = _get_translator(config.get('lang'))
pylons.translator._push_object(translator)
# Get the rhodecode auth user object and make it available.
auth_user = get_auth_user(environ)
request.user = auth_user
environ['rc_auth_user'] = auth_user
# Setup the pylons context object ('c')
context = ContextObj()
context.rhodecode_user = auth_user
attach_context_attributes(context, request)
pylons.tmpl_context._push_object(context)
return handler(request)
return pylons_compatibility_tween
return vcs_detection_tween
def includeme(config):
@ -95,4 +60,7 @@ def includeme(config):
'pyramid.events.NewRequest')
config.add_subscriber('rhodecode.subscribers.add_localizer',
'pyramid.events.NewRequest')
config.add_tween('rhodecode.tweens.pylons_compatibility_tween_factory')
config.add_subscriber('rhodecode.subscribers.add_pylons_context',
'pyramid.events.ContextFound')
config.add_tween('rhodecode.tweens.vcs_detection_tween_factory')