def _fake_request_context(self): '''Create a minimal (fake) web context for this command. This is commonly needed e.g. for toolkit.render() to work as it references several Pylons context (thread-local) objects. ''' import pylons from pylons.util import AttribSafeContextObj req1 = AttribSafeContextObj() self.registry.register(pylons.request, req1) pylons.request.environ = dict() pylons.request.params = dict() res1 = AttribSafeContextObj() self.registry.register(pylons.response, res1) pylons.response.headers = dict() ses1 = AttribSafeContextObj() self.registry.register(pylons.session, ses1) url1 = AttribSafeContextObj() self.registry.register(pylons.url, url1) self.logger.debug('Created a fake request context for Pylons') return
def fake_request(pylons_config, server_name='mediadrop.example', language='en', method='GET', request_uri='/', post_vars=None, registry=None): paste_registry = registry or _paste_registry(pylons.request) app_globals = pylons_config['pylons.app_globals'] if post_vars and method.upper() != 'POST': raise ValueError( 'You must not specify post_vars for request method %r' % method) wsgi_environ = create_wsgi_environ('http://%s%s' % (server_name, request_uri), method.upper(), request_body=post_vars) wsgi_environ['paste.registry'] = paste_registry request = Request(wsgi_environ, charset='utf-8') request.language = language request.settings = app_globals.settings register_instance(paste_registry, 'request', request) response = Response(content_type='application/xml', charset='utf-8') register_instance(paste_registry, 'response', response) setup_session(paste_registry, if_not_registered=True) routes_url = URLGenerator(pylons_config['routes.map'], wsgi_environ) register_instance(paste_registry, 'url', routes_url) # Use ContextObj() when we get rid of 'pylons.strict_tmpl_context=False' in # mediadrop.lib.environment tmpl_context = AttribSafeContextObj() tmpl_context.paginators = Bunch() register_instance(paste_registry, 'tmpl_context', tmpl_context) # some parts of Pylons (e.g. Pylons.controllers.core.WSGIController) # use the '.c' alias instead. pylons.c = pylons.tmpl_context register_instance(paste_registry, 'c', tmpl_context) engines = create_tw_engine_manager(app_globals) host_framework = PylonsHostFramework(engines=engines) paste_registry.register(tw.framework, host_framework) setup_translator(language=language, registry=paste_registry, locale_dirs=pylons_config.get('locale_dirs')) wsgi_environ.update({ 'pylons.pylons': pylons, 'paste.registry': paste_registry, 'routes.url': routes_url, }) return request
def fake_request(pylons_config, server_name='mediacore.example', language='en', method='GET', request_uri='/', post_vars=None): app_globals = pylons_config['pylons.app_globals'] pylons.app_globals._push_object(app_globals) if post_vars and method.upper() != 'POST': raise ValueError( 'You must not specify post_vars for request method %r' % method) wsgi_environ = create_wsgi_environ('http://%s%s' % (server_name, request_uri), method.upper(), request_body=post_vars) request = Request(wsgi_environ, charset='utf-8') request.language = language request.settings = app_globals.settings pylons.request._push_object(request) response = Response(content_type='application/xml', charset='utf-8') pylons.response._push_object(response) session = SessionObject(wsgi_environ) pylons.session._push_object(session) routes_url = URLGenerator(pylons_config['routes.map'], wsgi_environ) pylons.url._push_object(routes_url) # Use ContextObj() when we get rid of 'pylons.strict_tmpl_context=False' in # mediacore.lib.environment tmpl_context = AttribSafeContextObj() tmpl_context.paginators = Bunch() pylons.tmpl_context._push_object(tmpl_context) # some parts of Pylons (e.g. Pylons.controllers.core.WSGIController) # use the '.c' alias instead. pylons.c = pylons.tmpl_context paste_registry = Registry() paste_registry.prepare() engines = create_tw_engine_manager(app_globals) host_framework = PylonsHostFramework(engines=engines) paste_registry.register(tw.framework, host_framework) setup_translator(language=language, registry=paste_registry) wsgi_environ.update({ 'pylons.pylons': pylons, 'paste.registry': paste_registry, }) return request
def fake_request(pylons_config, server_name='mediadrop.example', language='en', method='GET', request_uri='/', post_vars=None): app_globals = pylons_config['pylons.app_globals'] pylons.app_globals._push_object(app_globals) if post_vars and method.upper() != 'POST': raise ValueError('You must not specify post_vars for request method %r' % method) wsgi_environ = create_wsgi_environ('http://%s%s' % (server_name, request_uri), method.upper(), request_body=post_vars) request = Request(wsgi_environ, charset='utf-8') request.language = language request.settings = app_globals.settings pylons.request._push_object(request) response = Response(content_type='application/xml', charset='utf-8') pylons.response._push_object(response) session = SessionObject(wsgi_environ) pylons.session._push_object(session) routes_url = URLGenerator(pylons_config['routes.map'], wsgi_environ) pylons.url._push_object(routes_url) # TODO: Use ContextObj() for Pylons 0.10 tmpl_context = AttribSafeContextObj() tmpl_context.paginators = Bunch() pylons.tmpl_context._push_object(tmpl_context) # some parts of Pylons (e.g. Pylons.controllers.core.WSGIController) # use the '.c' alias instead. pylons.c = pylons.tmpl_context paste_registry = Registry() paste_registry.prepare() engines = create_tw_engine_manager(app_globals) host_framework = PylonsHostFramework(engines=engines) paste_registry.register(tw.framework, host_framework) mediacore_i18n_path = os.path.join(os.path.dirname(mediacore.__file__), 'i18n') translator = Translator(language, dict(mediacore=mediacore_i18n_path)) # not sure why but sometimes pylons.translator is not a StackedObjectProxy # but just a regular Translator. if not hasattr(pylons.translator, '_push_object'): pylons.translator = StackedObjectProxy() paste_registry.replace(pylons.translator, translator) wsgi_environ.update({ 'pylons.pylons': pylons, 'paste.registry': paste_registry, }) return request
def setup_class(cls): cls.registry = Registry() cls.registry.prepare() cls.context_obj = AttribSafeContextObj() cls.registry.register(pylons.c, cls.context_obj) cls.app_globals_obj = app_globals.Globals() cls.registry.register(pylons.g, cls.app_globals_obj) cls.request_obj = Request(dict(HTTP_HOST="nohost")) cls.registry.register(pylons.request, cls.request_obj) cls.translator_obj = MockTranslator() cls.registry.register(pylons.translator, cls.translator_obj) cls.buffet = pylons.templating.Buffet('genshi', template_root='ckan.templates') cls.registry.register(pylons.buffet, cls.buffet) cls.registry.register(pylons.response, Response()) mapper = make_map() cls.registry.register(pylons.url, URLGenerator(mapper, {})) cls.registry.register(pylons.session, TestSession()) # Templates often want to find out the request's routes info, so put # some dummy values into the routes_dict, so the templates that do # this don't cause an exception. pylons.request.environ.update({ 'pylons.routes_dict': { 'action': 'test-action', 'controller': 'test-package::', } })
def mock_pylons(): from paste.registry import Registry import pylons from pylons.util import AttribSafeContextObj import ckan.lib.app_globals as app_globals from ckan.lib.cli import MockTranslator from ckan.config.routing import make_map from pylons.controllers.util import Request, Response from routes.util import URLGenerator class TestPylonsSession(dict): last_accessed = None def save(self): pass registry = Registry() registry.prepare() context_obj = AttribSafeContextObj() registry.register(pylons.c, context_obj) app_globals_obj = app_globals.app_globals registry.register(pylons.g, app_globals_obj) request_obj = Request(dict(HTTP_HOST="nohost", REQUEST_METHOD="GET")) registry.register(pylons.request, request_obj) translator_obj = MockTranslator() registry.register(pylons.translator, translator_obj) registry.register(pylons.response, Response()) mapper = make_map() registry.register(pylons.url, URLGenerator(mapper, {})) registry.register(pylons.session, TestPylonsSession())
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 fake_request(pylons_config, server_name='mediacore.example', language='en', method='GET', request_uri='/', post_vars=None): app_globals = pylons_config['pylons.app_globals'] pylons.app_globals._push_object(app_globals) if post_vars and method.upper() != 'POST': raise ValueError('You must not specify post_vars for request method %r' % method) wsgi_environ = create_wsgi_environ('http://%s%s' % (server_name, request_uri), method.upper(), request_body=post_vars) request = Request(wsgi_environ, charset='utf-8') request.language = language request.settings = app_globals.settings pylons.request._push_object(request) response = Response(content_type='application/xml', charset='utf-8') pylons.response._push_object(response) session = SessionObject(wsgi_environ) pylons.session._push_object(session) routes_url = URLGenerator(pylons_config['routes.map'], wsgi_environ) pylons.url._push_object(routes_url) # Use ContextObj() when we get rid of 'pylons.strict_tmpl_context=False' in # mediacore.lib.environment tmpl_context = AttribSafeContextObj() tmpl_context.paginators = Bunch() pylons.tmpl_context._push_object(tmpl_context) # some parts of Pylons (e.g. Pylons.controllers.core.WSGIController) # use the '.c' alias instead. pylons.c = pylons.tmpl_context paste_registry = Registry() paste_registry.prepare() engines = create_tw_engine_manager(app_globals) host_framework = PylonsHostFramework(engines=engines) paste_registry.register(tw.framework, host_framework) setup_translator(language=language, registry=paste_registry, locale_dirs=pylons_config.get('locale_dirs')) wsgi_environ.update({ 'pylons.pylons': pylons, 'paste.registry': paste_registry, }) return request
def fake_request( pylons_config, server_name="mediadrop.example", language="en", method="GET", request_uri="/", post_vars=None ): app_globals = pylons_config["pylons.app_globals"] pylons.app_globals._push_object(app_globals) if post_vars and method.upper() != "POST": raise ValueError("You must not specify post_vars for request method %r" % method) wsgi_environ = create_wsgi_environ( "http://%s%s" % (server_name, request_uri), method.upper(), request_body=post_vars ) request = Request(wsgi_environ, charset="utf-8") request.language = language request.settings = app_globals.settings pylons.request._push_object(request) response = Response(content_type="application/xml", charset="utf-8") pylons.response._push_object(response) session = SessionObject(wsgi_environ) pylons.session._push_object(session) routes_url = URLGenerator(pylons_config["routes.map"], wsgi_environ) pylons.url._push_object(routes_url) # Use ContextObj() when we get rid of 'pylons.strict_tmpl_context=False' in # mediadrop.lib.environment tmpl_context = AttribSafeContextObj() tmpl_context.paginators = Bunch() pylons.tmpl_context._push_object(tmpl_context) # some parts of Pylons (e.g. Pylons.controllers.core.WSGIController) # use the '.c' alias instead. pylons.c = pylons.tmpl_context paste_registry = Registry() paste_registry.prepare() engines = create_tw_engine_manager(app_globals) host_framework = PylonsHostFramework(engines=engines) paste_registry.register(tw.framework, host_framework) setup_translator(language=language, registry=paste_registry, locale_dirs=pylons_config.get("locale_dirs")) wsgi_environ.update({"pylons.pylons": pylons, "paste.registry": paste_registry}) return request
def setup_class(cls): ''' Setup database ''' model.repo.rebuild_db() harvest_model.setup() kata_model.setup() # The Pylons globals are not available outside a request. This is a hack to provide context object. c = AttribSafeContextObj() py_obj = PylonsContext() py_obj.tmpl_context = c pylons.tmpl_context._push_object(c)
def setup_class(cls): cls.registry = Registry() cls.registry.prepare() cls.context_obj = AttribSafeContextObj() cls.registry.register(pylons.c, cls.context_obj) cls.app_globals_obj = app_globals.Globals() cls.registry.register(pylons.g, cls.app_globals_obj) cls.request_obj = Request(dict(HTTP_HOST="nohost")) cls.registry.register(pylons.request, cls.request_obj) cls.translator_obj = MockTranslator() cls.registry.register(pylons.translator, cls.translator_obj) cls.buffet = pylons.templating.Buffet('genshi', template_root='ckan.templates') cls.registry.register(pylons.buffet, cls.buffet) cls.registry.register(pylons.response, Response()) mapper = make_map() cls.registry.register(pylons.url, URLGenerator(mapper, {})) cls.registry.register(pylons.session, TestSession())
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']])