Skip to content

Commit

Permalink
Make roles in user model query optimization "optional".
Browse files Browse the repository at this point in the history
  • Loading branch information
nfvs authored and jirikuncar committed Jul 18, 2017
1 parent fd29734 commit f254c96
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions flask_security/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,17 @@ def __init__(self, db, user_model, role_model):

def get_user(self, identifier):
from sqlalchemy import func as alchemyFn
from sqlalchemy.orm import joinedload
user_model_query = self.user_model.query
if hasattr(self.user_model, 'roles'):
from sqlalchemy.orm import joinedload
user_model_query = user_model_query.options(joinedload('roles'))

if self._is_numeric(identifier):
return self.user_model.query\
.options(joinedload('roles'))\
.get(identifier)
return user_model_query.get(identifier)
for attr in get_identity_attributes():
query = alchemyFn.lower(getattr(self.user_model, attr)) \
== alchemyFn.lower(identifier)
rv = self.user_model.query\
.options(joinedload('roles'))\
.filter(query)\
.first()
rv = user_model_query.filter(query).first()
if rv is not None:
return rv

Expand All @@ -257,11 +256,12 @@ def _is_numeric(self, value):
return True

def find_user(self, **kwargs):
from sqlalchemy.orm import joinedload
return self.user_model.query\
.options(joinedload('roles'))\
.filter_by(**kwargs)\
.first()
query = self.user_model.query
if hasattr(self.user_model, 'roles'):
from sqlalchemy.orm import joinedload
query = query.options(joinedload('roles'))

return query.filter_by(**kwargs).first()

def find_role(self, role):
return self.role_model.query.filter_by(name=role).first()
Expand Down

0 comments on commit f254c96

Please sign in to comment.