From 19ce197ab98bbcca224b434032e2d011f354184e Mon Sep 17 00:00:00 2001 From: Oleg Pidsadnyi Date: Fri, 18 Mar 2016 20:01:10 +0100 Subject: [PATCH] Documented the usage of the LazyCryptConfig. --- sqlalchemy_utils/types/password.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/sqlalchemy_utils/types/password.py b/sqlalchemy_utils/types/password.py index 6202024..aa39c3d 100644 --- a/sqlalchemy_utils/types/password.py +++ b/sqlalchemy_utils/types/password.py @@ -82,7 +82,9 @@ class PasswordType(types.TypeDecorator, ScalarCoercible): verifying them using a pythonic interface. All keyword arguments (aside from max_length) are forwarded to the - construction of a `passlib.context.LazyCryptContext` object. + construction of a `passlib.context.LazyCryptContext` object, which + also supports deferred configuration via the `onload` callback. + The following usage will create a password column that will automatically hash new passwords as `pbkdf2_sha512` but still compare @@ -115,6 +117,29 @@ class PasswordType(types.TypeDecorator, ScalarCoercible): target.password == 'b' # True + + Lazy configuration of the type with Flask config: + + :: + + + import flask + + class User(db.Model): + __tablename__ = 'user' + + password = db.Column( + PasswordType( + # The returned dictionary is forwarded to the CryptContext + onload=lambda **kwargs: dict( + schemes=flask.current_app.config['PASSWORD_SCHEMES'], + **kwargs + ), + ), + unique=False, + nullable=False, + ) + """ impl = types.VARBINARY(1024)