Ticket #2432 (closed defect: fixed)
call_on_startup functions do not successfully modify things in tg.config
| Reported by: | toshio | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 2.1b1 |
| Component: | TurboGears | Version: | 2.0.3 |
| Severity: | normal | Keywords: | |
| Cc: | lmacken |
Description
I have a library that does a few things for a TG2 app. To make things easy on the people who will be using my library in their apps I have a function to call on application startup to set a few things up. Among other things, this function attempts to modify base_config.variable_providers and base_config.ignore_parameters. Here's the code:
def enable_csrf():
# Ignore the _csrf_token parameter
ignore = config.get('ignore_parameters', [])
print 'before', config.get('ignore_parameters', [])
print 'before', config.get('variable_provider', [])
if '_csrf_token' not in ignore:
ignore.append('_csrf_token')
config.update['ignore_parameters'] = ignore
# Add a function to the template tg stdvars that looks up a template.
var_provider = config.get('variable_provider', None)
if var_provider:
config['variable_provider'] = lambda: \
var_provider().update({'fedora_template': fedora_template})
else:
config['variable_provider'] = lambda: {'fedora_template':
fedora_template}
print 'after', config.get('ignore_parameters', [])
print 'after', config.get('variable_provider', [])
config/app_cfg.py:
base_config.call_on_startup = [enable_csrf]
When used like this, call_on_startup runs the enable_csrf() function but the modifications of tg.configignore_parameters? and tg.configvariable_providers? do not show up outside of the enable_csrf() function.
elpargo took an initial look at the function over IRC and had these comments:
right. It indeed seems like a) a bug in startup or b) we need a separate hook. take a look at tg/configuration. make_load_environment sets up the functions at setup_startup_and_shutdown the comment stays it will Register the functions however it is actually calling them. however in setup_tg_wsgi_app/make_base_app which is when things are actually created there is no call for that. to be honest I have never needed those, but this code doesn't seems right. all those setup_* functions are supposed to pull things in rather than execute them.
I have reworked my code to call my startup function in an overridden AppConfig? which may work out better for me in the long-run but thought this should either be addressed as a code or documentation bug. I can provide more code examples if needed but I'm out of town until January 4th.
Attachments
Change History
comment:2 Changed 2 years ago by toshio
With current tip, ignore_parameters appears to be broken altogether.
Fresh paster quickstart with this addition to app_cfg.py:
base_config.ignore_parameters = yum?
browse to http://localhost:8080/?yum=1234 Get a traceback:
TypeError?: index() got an unexpected keyword argument 'yum'
Will atach the full traceback.
Changed 2 years ago by toshio
-
attachment
traceback
added
traceback with current tip when giving a parameter specified via base_config.ignore_parameters