Skip to content

Commit

Permalink
Update get_user_by_email (#143)
Browse files Browse the repository at this point in the history
* Update get_user_by_email

add para make_new_user_if_none:boolean defaults to True
is added to if user is None

add test for make_new_user_if_none=False in
tests.read.test_get_user_by_email

Signed-off-by: Rudolf Braun <[email protected]>

* fix linting

Signed-off-by: Rudolf Braun <[email protected]>

---------

Signed-off-by: Rudolf Braun <[email protected]>
  • Loading branch information
BraunRudolf authored Jul 12, 2024
1 parent a34c447 commit cb8bc65
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pvsite_datamodel/read/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
logger = logging.getLogger(__name__)


def get_user_by_email(session: Session, email: str):
def get_user_by_email(session: Session, email: str, make_new_user_if_none: bool = True):
"""
Get user by email. If user does not exist, make one.
:param session: database session
:param email: email of user
:param make_new_user_if_none: make user with email if doesn't exist
:return: user object
"""

user = session.query(UserSQL).filter(UserSQL.email == email).first()

if user is None:
if user is None and make_new_user_if_none is True:
logger.info(f"User with email {email} not found, so making one")

# making a new site group
Expand Down
7 changes: 7 additions & 0 deletions tests/read/test_get_user_by_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ def test_get_user_by_email_with_users(self, db_session):
user = get_user_by_email(session=db_session, email="[email protected]")
assert user.email == "[email protected]"
assert len(db_session.query(UserSQL).all()) == 2

def test_get_user_by_email_no_user_maker_user_false(self, db_session):
user = get_user_by_email(
session=db_session, email="[email protected]", make_new_user_if_none=False
)
assert user is None
assert len(db_session.query(UserSQL).all()) == 0

0 comments on commit cb8bc65

Please sign in to comment.