SQLAlchemy插件相当简单;您可以对其进行猴子补丁或实现您自己的插件版本,例如
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
import repoze.who.plugins.sa
class _EmailBaseSQLAlchemyPlugin(object)
default_translations = {'user_name': "user_name", 'email': 'email',
'validate_password': 'validate_password'}
def get_user(self, login):
login_type = 'email' if '@' in login else 'user_name'
login_attr = getattr(self.user_class, self.translations[login_type])
query = self.dbsession.query(self.user_class)
query = query.filter(login_attr == login)
try:
return query.one()
except (NoResultFound, MultipleResultsFound):
# As recommended in the docs for repoze.who, it's important to
# verify that there's only _one_ matching userid.
return None
class EmailSQLAlchemyAuthenticatorPlugin(_EmailBaseSQLAlchemyPlugin,
repoze.who.plugins.sa.SQLAlchemyAuthenticatorPlugin):
pass
class EmailSQLAlchemyUserMDPlugin(_EmailBaseSQLAlchemyPlugin,
repoze.who.plugins.sa.SQLAlchemyUserMDPlugin):
pass
sqlauth = EmailSQLAlchemyAuthenticatorPlugin(model.User, model.meta.Session)
sql_user_md = SQLAlchemyUserMDPlugin(model.User, model.meta.Session)