def create_request(path, environ=None): """Helper used in test cases to quickly setup a request obj. ``path`` The path will become PATH_INFO ``environ`` Additional environment Returns an instance of the `webob.Request` object. """ # setup the environ if environ is None: environ = {} environ.update(default_environ) # create a "blank" WebOb Request object # using Pylon's Request which is a webob Request plus # some compatibility methods req = Request.blank(path, environ) # setup a Registry reg = environ.setdefault('paste.registry', Registry()) reg.prepare() # setup pylons.request to point to our Registry reg.register(pylons.request, req) # setup tmpl context tmpl_context._push_object(ContextObj()) url._push_object(URLGenerator(default_map, environ)) return req
def setup_app_env(self, environ, start_response): """Setup and register all the Pylons objects with the registry After creating all the global objects for use in the request, :meth:`~PylonsApp.register_globals` is called to register them in the environment. """ if self.log_debug: log.debug("Setting up Pylons stacked object globals") # Setup the basic pylons global objects req_options = self.request_options req = Request(environ, charset=req_options['charset'], unicode_errors=req_options['errors'], decode_param_names=req_options['decode_param_names']) req.language = req_options['language'] req.config = self.config req.link, req.route_dict = environ['wsgiorg.routing_args'] response = Response(content_type=self.response_options['content_type'], charset=self.response_options['charset']) response.headers.update(self.response_options['headers']) # Store a copy of the request/response in environ for faster access pylons_obj = PylonsContext() pylons_obj.config = self.config pylons_obj.request = req pylons_obj.response = response pylons_obj.app_globals = self.globals pylons_obj.h = self.helpers if 'routes.url' in environ: pylons_obj.url = environ['routes.url'] environ['pylons.pylons'] = pylons_obj environ['pylons.environ_config'] = self.environ_config # Setup the translator object lang = self.config['lang'] pylons_obj.translator = _get_translator(lang, pylons_config=self.config) if self.config['pylons.strict_tmpl_context']: tmpl_context = ContextObj() else: tmpl_context = AttribSafeContextObj() pylons_obj.tmpl_context = req.tmpl_context = tmpl_context if self._session_key in environ: pylons_obj.session = req.session = environ[self._session_key] if self._cache_key in environ: pylons_obj.cache = environ[self._cache_key] # Load the globals with the registry if around if 'paste.registry' in environ: self.register_globals(environ)
def setUp(self): tmpl_options = {} tmpl_options['genshi.search_path'] = ['tests'] self._ctx = ContextObj() tmpl_context._push_object(self._ctx) warnings.simplefilter("ignore") pylons.config.push_process_config(default_config) warnings.resetwarnings() setup_session_dir()
def init_stack(config=None): if not config: config = pylons.test.pylonsapp.config url._push_object(URLGenerator(config['routes.map'], environ)) pylons.app_globals._push_object(config['pylons.app_globals']) pylons.config._push_object(config) pylons.tmpl_context._push_object(ContextObj()) # Initialize a translator for tests that utilize i18n translator = _get_translator(pylons.config.get('lang')) pylons.translator._push_object(translator)
def setUp(self): tmpl_options = {} tmpl_options['genshi.search_path'] = ['tests'] self._ctx = ContextObj() tmpl_context._push_object(self._ctx) self._buffet = pylons.templating.Buffet( default_engine='genshi',tmpl_options=tmpl_options ) pylons.buffet._push_object(self._buffet) setup_session_dir()
def test_get_visual_attr(pylonsapp): c = ContextObj() assert None is helpers.get_visual_attr(c, 'fakse') # emulate the c.visual behaviour c.visual = AttributeDict({}) assert None is helpers.get_visual_attr(c, 'some_var') c.visual.some_var = 'foobar' assert 'foobar' == helpers.get_visual_attr(c, 'some_var')
def setUp(self): c = ContextObj() py_obj = PylonsContext() py_obj.tmpl_context = c py_obj.request = py_obj.response = None self.environ = { 'pylons.routes_dict': dict(action='index'), 'paste.config': dict(global_conf=dict(debug=True)), 'pylons.pylons': py_obj } pylons.tmpl_context._push_object(c)
def setUp(self): if not os.path.exists(session_dir): os.makedirs(session_dir) # Setting TG2 up: c = ContextObj() py_obj = PylonsContext() py_obj.c = c py_obj.request = py_obj.response = None environ = {'pylons.routes_dict': dict(action='index'), 'pylons.pylons': py_obj} pylons.tmpl_context._push_object(c) self.app = make_app(DefaultLessTGController, environ, with_errors=True)
def _init_stack(config=None, environ=None): if not config: config = pylons.test.pylonsapp.config if not environ: environ = {} pylons.url._push_object(URLGenerator(config['routes.map'], environ or {})) pylons.app_globals._push_object(config['pylons.app_globals']) pylons.config._push_object(config) pylons.tmpl_context._push_object(ContextObj()) # Initialize a translator for tests that utilize i18n translator = _get_translator(pylons.config.get('lang')) pylons.translator._push_object(translator) pylons.session._push_object(SessionObject(environ or {})) pylons.request._push_object(webob.Request.blank('', environ=environ))
def setUp(self): # Creating the session dir: if not os.path.exists(session_dir): os.makedirs(session_dir) # Setting TG2 up: c = ContextObj() py_obj = PylonsContext() py_obj.c = c py_obj.request = py_obj.response = None environ = {'pylons.routes_dict': dict(action='index'), 'pylons.pylons': py_obj} pylons.tmpl_context._push_object(c) # Finally, the app: self.app = make_app(self.controller, environ)
def setUp(self): # Creating the session dir: if not os.path.exists(session_dir): os.makedirs(session_dir) # Setting Pylons up: c = ContextObj() py_obj = PylonsContext() py_obj.c = c py_obj.request = py_obj.response = None self.environ = { 'pylons.routes_dict': dict(action='index'), 'paste.config': dict(global_conf=dict(debug=True)), 'pylons.pylons': py_obj, } pylons.c._push_object(c) # Finally, the app: self.app = make_app(self.controller, self.environ)
class ModelTest(object): config = appconfig('config:test.ini', relative_to=conf_dir) root_path = os.path.join(conf_dir, 'r2') paths = { 'root': root_path, 'controllers': os.path.join(root_path, 'controllers'), 'templates': tmpl_dirs, 'static_files': os.path.join(root_path, 'public') } registry = Registry() registry.prepare() globals = Globals(config.global_conf, config.local_conf, paths) registry.register(pylons.g, globals) registry.register(pylons.translator, NullTranslations()) context_obj=ContextObj() registry.register(pylons.c, context_obj) r2.config.cache = globals.cache
def __call__(self, environ, start_response): registry = environ['paste.registry'] py_obj = PylonsContext() environ_config = environ.setdefault('pylons.environ_config', {}) if self.setup_cache: py_obj.cache = environ['beaker.cache'] registry.register(pylons.cache, environ['beaker.cache']) environ_config['cache'] = 'beaker.cache' if self.setup_session: py_obj.session = environ['beaker.session'] registry.register(pylons.session, environ['beaker.session']) environ_config['session'] = 'beaker.session' if self.setup_g: py_obj.g = py_obj.app_globals = self.g registry.register(pylons.g, self.g) registry.register(pylons.app_globals, self.g) translator = gettext.NullTranslations() py_obj.translator = translator registry.register(pylons.translator, translator) # Update the environ req = Request( environ, charset=request_defaults['charset'], unicode_errors=request_defaults['errors'], decode_param_names=request_defaults['decode_param_names']) req.language = request_defaults['language'] response = Response(content_type=response_defaults['content_type'], charset=response_defaults['charset']) response.headers.update(response_defaults['headers']) environ.update(self.environ) py_obj.config = pylons.config._current_obj() py_obj.request = req py_obj.response = response py_obj.c = ContextObj() environ['pylons.pylons'] = py_obj registry.register(pylons.request, req) registry.register(pylons.response, response) if 'routes.url' in environ: registry.register(pylons.url, environ['routes.url']) return self.app(environ, start_response)
def pylons_compatibility_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. """ try: config = rhodecode.CONFIG environ = request.environ session = request.session session_key = (config['pylons.environ_config'].get( 'session', 'beaker.session')) # Setup pylons globals. pylons.config._push_object(config) pylons.request._push_object(request) pylons.session._push_object(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) pylons.tmpl_context._push_object(context) return handler(request) finally: # Dispose current database session and rollback uncommitted # transactions. meta.Session.remove()
def setup_app_env(self, environ, start_response): """Setup and register all the Pylons objects with the registry""" if self.log_debug: log.debug("Setting up Pylons stacked object globals") registry = environ['paste.registry'] registry.register(WSGIRequest.defaults, self.request_options) registry.register(WSGIResponse.defaults, self.response_options) req = WSGIRequest(environ) # Setup the basic pylons global objects registry.register(pylons.request, req) registry.register(pylons.response, WSGIResponse()) registry.register(pylons.buffet, self.buffet) registry.register(pylons.g, self.globals) registry.register(pylons.config, self.config) registry.register(pylons.h, self.helpers or \ pylons.legacy.load_h(self.package_name)) # Setup the translator global object registry.register(pylons.translator, gettext.NullTranslations()) lang = self.config.get('lang') if lang: set_lang(lang) if self.config['pylons.strict_c']: registry.register(pylons.c, ContextObj()) else: registry.register(pylons.c, AttribSafeContextObj()) econf = environ['pylons.environ_config'] if econf.get('session'): registry.register(pylons.session, environ[econf['session']]) if econf.get('cache'): registry.register(pylons.cache, environ[econf['cache']])
def setUp(self): self.environ = {'pylons.routes_dict':dict(action='index'), 'paste.config':dict(global_conf=dict(debug=True))} pylons.c._push_object(ContextObj())