def find_or_create_user(uuid: str, username: str, session: Session, commit: bool = True) -> User: """ Returns a user object representing the referenced journalist UUID. If the user does not already exist in the data, a new instance is created. If the user exists but user fields have changed, the db is updated. """ user = session.query(User).filter_by(uuid=uuid).one_or_none() if not user: new_user = User(username=username) new_user.uuid = uuid session.add(new_user) if commit: session.commit() return new_user if user.username != username: user.username = username if commit: session.commit() return user
def find_or_create_user(uuid, username, session): """ Returns a user object representing the referenced journalist UUID. If the user does not already exist in the data, a new instance is created. If the user exists but the username has changed, the username is updated. """ user = session.query(User).filter_by(uuid=uuid).one_or_none() if user and user.username == username: # User exists in the local database and the username is unchanged. return user elif user and user.username != username: # User exists in the local database but the username is changed. user.username = username session.add(user) session.commit() return user else: # User does not exist in the local database. new_user = User(username) new_user.uuid = uuid session.add(new_user) session.commit() return new_user