Exemplo n.º 1
0
def create_user(name: str,
                email: str,
                password: str,
                profile_image_url: str = None) -> Optional[User]:

    if not name or \
       not email or \
       not password:
        return None

    # Could use find_user to avoid committing an existing user, but I'm using the db to handle this.  Which is better?

    user = User()
    user.name = name
    user.email = email
    user.hashed_password = hash_text(password)
    user.profile_image_url = profile_image_url

    s = create_session()
    s.add(user)
    try:
        # Commit data to the DB but keep an unexpired version to pass back to caller
        s.expire_on_commit = False
        s.commit()
    except exc.IntegrityError:
        # Record already exists or cannot be added

        user = None
    s.close()
    return user
Exemplo n.º 2
0
def create_user(name: str, email: str, password: str) -> Optional[User]:
    if find_user_by_email(email):
        return None
    user = User()
    user.email = email
    user.name = name
    user.hashed_password = hash_text(password)

    session = db_session.create_session()
    session.add(user)
    session.commit()

    return user
Exemplo n.º 3
0
def do_user_import(user_lookup: Dict[str, str]) -> Dict[str, User]:
    print('Importing users ... ', flush=True)
    for idx, (email, name) in enumerate(user_lookup.items()):
        session = db_session.create_session()
        session.expire_on_commit = False

        user = User()
        user.email = email
        user.name = name
        session.add(user)

        session.commit()

    print()
    sys.stderr.flush()
    sys.stdout.flush()

    session = db_session.create_session()
    return {u.email: u for u in session.query(User)}
Exemplo n.º 4
0
def do_user_import(user_lookup: Dict[str, str]) -> Dict[str, User]:
    print("Importing users ... ", flush=True)
    with progressbar.ProgressBar(max_value=len(user_lookup)) as bar:
        for idx, (email, name) in enumerate(user_lookup.items()):
            session = db_session.create_session()
            session.expire_on_commit = False

            user = User()
            user.email = email
            user.name = name
            session.add(user)

            session.commit()
            bar.update(idx)

    print()
    sys.stderr.flush()
    sys.stdout.flush()

    session = db_session.create_session()
    return {u.email: u for u in session.query(User)}
Exemplo n.º 5
0
def do_user_import(user_lookup: Dict[str, str]) -> Dict[str, User]:
    print("Importing users ... ", flush=True)
    with progressbar.ProgressBar(max_value=len(user_lookup)) as bar:
        for idx, (email, name) in enumerate(user_lookup.items()):
            session = db_session.create_session()
            session.expire_on_commit = False

            user = User()
            user.email = email
            user.name = name
            session.add(user)

            session.commit()
            bar.update(idx)

    print()
    sys.stderr.flush()
    sys.stdout.flush()

    # Why do we create another one down here?  Can't we use the one above?  And do they close themselves when going out
    # of scope?
    session = db_session.create_session()
    return {u.email: u for u in session.query(User)}