##// END OF EJS Templates
core: use proper event to bootstrap pylons env....
marcink -
r1309:8a946991 default
parent child Browse files
Show More
@@ -30,6 +30,14 b' from threading import Thread'
30 30
31 31 from rhodecode.translation import _ as tsf
32 32
33 import rhodecode
34
35 from pylons.i18n.translation import _get_translator
36 from pylons.util import ContextObj
37 from routes.util import URLGenerator
38
39 from rhodecode.lib.base import attach_context_attributes, get_auth_user
40
33 41 log = logging.getLogger(__name__)
34 42
35 43
@@ -63,7 +71,8 b' def add_localizer(event):'
63 71
64 72
65 73 def set_user_lang(event):
66 cur_user = getattr(event.request, 'user', None)
74 request = event.request
75 cur_user = getattr(request, 'user', None)
67 76
68 77 if cur_user:
69 78 user_lang = cur_user.get_instance().user_data.get('language')
@@ -72,6 +81,44 b' def set_user_lang(event):'
72 81 event.request._LOCALE_ = user_lang
73 82
74 83
84 def add_pylons_context(event):
85 request = event.request
86
87 config = rhodecode.CONFIG
88 environ = request.environ
89 session = request.session
90
91 if hasattr(request, 'vcs_call'):
92 # skip vcs calls
93 return
94
95 # Setup pylons globals.
96 pylons.config._push_object(config)
97 pylons.request._push_object(request)
98 pylons.session._push_object(session)
99 pylons.translator._push_object(_get_translator(config.get('lang')))
100
101 pylons.url._push_object(URLGenerator(config['routes.map'], environ))
102 session_key = (
103 config['pylons.environ_config'].get('session', 'beaker.session'))
104 environ[session_key] = session
105
106 if hasattr(request, 'rpc_method'):
107 # skip api calls
108 return
109
110 # Get the rhodecode auth user object and make it available.
111 auth_user = get_auth_user(environ)
112 request.user = auth_user
113 environ['rc_auth_user'] = auth_user
114
115 # Setup the pylons context object ('c')
116 context = ContextObj()
117 context.rhodecode_user = auth_user
118 attach_context_attributes(context, request)
119 pylons.tmpl_context._push_object(context)
120
121
75 122 def scan_repositories_if_enabled(event):
76 123 """
77 124 This is subscribed to the `pyramid.events.ApplicationCreated` event. It
@@ -20,33 +20,21 b''
20 20
21 21
22 22 import logging
23 import pylons
24 import rhodecode
25 23
26 from pylons.i18n.translation import _get_translator
27 from pylons.util import ContextObj
28 from routes.util import URLGenerator
29
30 from rhodecode.lib.base import attach_context_attributes, get_auth_user
31 24 from rhodecode.lib.middleware.vcs import (
32 25 detect_vcs_request, VCS_TYPE_KEY, VCS_TYPE_SKIP)
33 from rhodecode.model import meta
34 26
35 27
36 28 log = logging.getLogger(__name__)
37 29
38 30
39 def pylons_compatibility_tween_factory(handler, registry):
31 def vcs_detection_tween_factory(handler, registry):
40 32
41 def pylons_compatibility_tween(request):
33 def vcs_detection_tween(request):
42 34 """
43 While migrating from pylons to pyramid we need to call some pylons code
44 from pyramid. For example while rendering an old template that uses the
45 'c' or 'h' objects. This tween sets up the needed pylons globals.
35 Do detection of vcs type, and save results for other layers to re-use
36 this information
46 37 """
47 config = rhodecode.CONFIG
48 environ = request.environ
49 session = request.session
50 38
51 39 vcs_handler = detect_vcs_request(
52 40 request.environ, request.registry.settings.get('vcs.backends'))
@@ -54,38 +42,15 b' def pylons_compatibility_tween_factory(h'
54 42 if vcs_handler:
55 43 # save detected VCS type for later re-use
56 44 request.environ[VCS_TYPE_KEY] = vcs_handler.SCM
45 request.vcs_call = vcs_handler.SCM
57 46 return handler(request)
58 47
59 48 # mark that we didn't detect an VCS, and we can skip detection later on
60 49 request.environ[VCS_TYPE_KEY] = VCS_TYPE_SKIP
61 50
62 # Setup pylons globals.
63 pylons.config._push_object(config)
64 pylons.request._push_object(request)
65 pylons.session._push_object(session)
66
67 session_key = (
68 config['pylons.environ_config'].get('session', 'beaker.session'))
69 environ[session_key] = session
70 pylons.url._push_object(URLGenerator(config['routes.map'], environ))
71
72 # TODO: Maybe we should use the language from pyramid.
73 translator = _get_translator(config.get('lang'))
74 pylons.translator._push_object(translator)
75
76 # Get the rhodecode auth user object and make it available.
77 auth_user = get_auth_user(environ)
78 request.user = auth_user
79 environ['rc_auth_user'] = auth_user
80
81 # Setup the pylons context object ('c')
82 context = ContextObj()
83 context.rhodecode_user = auth_user
84 attach_context_attributes(context, request)
85 pylons.tmpl_context._push_object(context)
86 51 return handler(request)
87 52
88 return pylons_compatibility_tween
53 return vcs_detection_tween
89 54
90 55
91 56 def includeme(config):
@@ -95,4 +60,7 b' def includeme(config):'
95 60 'pyramid.events.NewRequest')
96 61 config.add_subscriber('rhodecode.subscribers.add_localizer',
97 62 'pyramid.events.NewRequest')
98 config.add_tween('rhodecode.tweens.pylons_compatibility_tween_factory')
63 config.add_subscriber('rhodecode.subscribers.add_pylons_context',
64 'pyramid.events.ContextFound')
65
66 config.add_tween('rhodecode.tweens.vcs_detection_tween_factory')
General Comments 0
You need to be logged in to leave comments. Login now