Refactored PasswordType
This commit is contained in:
@@ -106,6 +106,9 @@ def sort_query(query, *args):
|
|||||||
|
|
||||||
The examples use the following model definition:
|
The examples use the following model definition:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
|
||||||
>>> import sqlalchemy as sa
|
>>> import sqlalchemy as sa
|
||||||
>>> from sqlalchemy import create_engine
|
>>> from sqlalchemy import create_engine
|
||||||
>>> from sqlalchemy.orm import sessionmaker
|
>>> from sqlalchemy.orm import sessionmaker
|
||||||
|
@@ -77,7 +77,8 @@ class PasswordType(types.TypeDecorator):
|
|||||||
# Bail if passlib is not found.
|
# Bail if passlib is not found.
|
||||||
if passlib is None:
|
if passlib is None:
|
||||||
raise ImproperlyConfigured(
|
raise ImproperlyConfigured(
|
||||||
"'passlib' is required to use 'PasswordType'")
|
"'passlib' is required to use 'PasswordType'"
|
||||||
|
)
|
||||||
|
|
||||||
# Construct the passlib crypt context.
|
# Construct the passlib crypt context.
|
||||||
self.context = CryptContext(**kwargs)
|
self.context = CryptContext(**kwargs)
|
||||||
@@ -95,7 +96,7 @@ class PasswordType(types.TypeDecorator):
|
|||||||
if value is not None:
|
if value is not None:
|
||||||
return Password(value, self.context)
|
return Password(value, self.context)
|
||||||
|
|
||||||
def coercion_listener(self, target, value, oldvalue, initiator):
|
def _coerce(self, value):
|
||||||
if not isinstance(value, Password):
|
if not isinstance(value, Password):
|
||||||
# Hash the password using the default scheme.
|
# Hash the password using the default scheme.
|
||||||
value = self.context.encrypt(value).encode('utf8')
|
value = self.context.encrypt(value).encode('utf8')
|
||||||
@@ -106,3 +107,6 @@ class PasswordType(types.TypeDecorator):
|
|||||||
value.context = weakref.proxy(self.context)
|
value.context = weakref.proxy(self.context)
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def coercion_listener(self, target, value, oldvalue, initiator):
|
||||||
|
return self._coerce(value)
|
||||||
|
Reference in New Issue
Block a user