Pylons config has a different content in websetup.py
In my pylons application I want to add some data on setup. ( a user)
To secure passwords in the database i've hashed the passwords with a salt, this salt is stored in the configuration file.
If I want to get the saltkey from configuration I do this (shortened example):
from pylons import config
saltkey = config.get("saltkey")
If this code is placed in for example a model, it returns the saltkey. In the User-model this code is used to create a hash with the salt.
However if I want to create an instance of this model in "websetup.py" the config has a different contents and it cannot retrieve the saltkey (resulting in an error)
def setup_app(command, conf, vars):
load_environment(conf.global_conf, conf.local_conf)
Base.metadata.create_all(bind=Session.bind)
user = User('admin', 'password123', 'test@test.com')
Session.add(user)
Session.commit()
My question is: Why has the config a different开发者_开发问答 content? And how do I fix this problem without an ugly hack?
You can access your config file in this step. The from pylons import config
method is best suited to doing so in the context of a WSGI request. However, you're not dealing with a WSGI request, so it's unavailable. Fortunately, you have a very easy way of accessing config during websetup.py
's operation. The setup_app()
function already consumes the config file, and Paster has already parsed it and turned it into a dictionary for you.
You can access your config file as the conf.local_conf
dictionary, and that will make the data you want available.
With all of that said - you should not store the salt in your config.ini file, that's a bad idea and you should avoid wheel-reinvention like that.
精彩评论