Example #1
0
def db_profile_available(profile_id: str) -> bool:
    """Update profile availability info in DB"""
    from main import logger
    if not db_duplicate_check([Profiles], Profiles.profile_id == profile_id):
        # profile not exist in DB
        return False
    db_session = Session()
    if profile_deleted(profile_id=profile_id):
        # profile deleted on site
        update_q = update(Profiles).where(
                Profiles.profile_id == profile_id). \
            values(available=False)
        db_session.execute(update_q)
        db_session.commit()
        db_session.close()
        logger.info(f'User {current_user} updated profile_id: {profile_id} '
                    f'and changed availability to 0,'
                    f'because profile deleted on site')
        return False
    else:
        # profile not deleted on site
        update_q = update(Profiles).where(
                Profiles.profile_id == profile_id). \
            values(available=True)
        db_session.execute(update_q)
        db_session.commit()
        db_session.close()
        logger.info(f'User {current_user} updated profile_id: {profile_id} '
                    f'and changed availability to 1,'
                    f'because profile not deleted on site')
        return True
Example #2
0
def db_add_profile(profile_id: str, profile_password: str):
    """Add profile to DB"""

    from main import logger
    if profile_deleted(profile_id=profile_id):
        logger.info(f'User {current_user} tried to add profile info, '
                    f'for profile_id: {profile_id}, '
                    f'but this profile deleted on site')
        return False
    db_session = Session()
    if db_duplicate_check([Profiles], Profiles.profile_id == profile_id):
        # profile already in DB, update password
        update_q = update(Profiles). \
            where(Profiles.profile_id == profile_id). \
            values(profile_password=profile_password)
        db_session.execute(update_q)
        logger.info(f'User {current_user} updated profile info, '
                    f'for profile_id: {profile_id}')
    else:
        # create new profile
        new_profile = Profiles(profile_id=profile_id,
                               profile_password=profile_password,
                               available=True,
                               can_receive=True)
        db_session.add(new_profile)
        logger.info(f'User {current_user} added profile, '
                    f'with profile_id: {profile_id}')
    db_session.commit()
    db_session.close()

    db_load_profile_description(profile_id=profile_id)
    return True
Example #3
0
def register_user(login: str, user_password: str):
    from main import logger

    db_session = Session()
    users = db_session.query(Users).filter(Users.login == login).all()
    if len(users) == 0:
        logger.info(f'User signup with wrong e-mail: {login}')
        return None
    db_invites = db_session.query(Invites).all()
    for db_invite in db_invites:
        if check_password_hash(users[0].user_password, db_invite.invite_id):
            invite_id = db_invite.invite_id
            update_q = update(Users).where(
                    Users.login == login). \
                values(user_password=generate_password_hash(
                    user_password,
                    "sha256",
                    salt_length=8))
            db_session.execute(update_q)
            db_session.commit()
            db_session.close()
            logger.info(f'User {login} successful registered '
                        f'with invite_id: {invite_id}')
            return None

    role = db_session.query(
        RolesOfUsers.user_role).filter(RolesOfUsers.login == login).all()
    if role[0][0] == 'deleted':
        logger.info(f'User signup with e-mail: {login}, '
                    f'but this account deleted')
        return None
    db_session.close()
    logger.info(f'User signup with e-mail: {login}, '
                f'but this account already exists')
    return 'Such account already exists'
Example #4
0
def db_change_user_role(user_login: str, role: str):
    db_session = Session()
    update_q = update(RolesOfUsers).where(
            RolesOfUsers.login == user_login). \
        values(user_role=role)
    db_session.execute(update_q)
    db_session.commit()
    db_session.close()

    return None
Example #5
0
def db_template_update(text_id: bytes, text: str) -> bool:
    if not db_duplicate_check([Texts.text_id], Texts.text_id == text_id):
        return False
    db_session = Session()
    update_q = update(Texts).where(
            Texts.text_id == text_id). \
        values(text=text)
    db_session.execute(update_q)
    db_session.commit()
    db_session.close()
    return True
Example #6
0
def db_delete_user(user_login: str) -> bool:
    """Delete user by login in database,
    changes user role to 'deleted',
    also delete all invites and visibility statuses for this user"""
    db_session = Session()
    update_q = update(RolesOfUsers).where(
            RolesOfUsers.login == user_login). \
        values(user_role='deleted')
    db_session.execute(update_q)
    db_session.commit()

    update_q = update(Users).where(
            Users.login == user_login). \
        values(user_password='******')
    db_session.execute(update_q)
    db_session.commit()
    db_session.close()
    db_delete_rows([Visibility], Visibility.login == user_login)
    invite_id = bytes((bytearray(
        db_get_rows([SentInvites.invite_id],
                    SentInvites.login == user_login)[0][0])))
    db_delete_rows([SentInvites], SentInvites.invite_id == invite_id)
    db_delete_rows([Invites], Invites.invite_id == invite_id)
    return True
Example #7
0
def create_invite(creator: User, invited_email: str, role: str) -> bool:
    from main import logger
    db_session = Session()
    invite = Invites(invite_id=uuid4().bytes)

    # new user creating
    new_user = Users(login=invited_email,
                     user_password=generate_password_hash(invite.invite_id,
                                                          "sha256",
                                                          salt_length=8))
    # assign role to user
    new_user_role = RolesOfUsers(login=invited_email, user_role=role)
    # create invite from user
    sent_invite_from = SentInvites(invite_id=invite.invite_id,
                                   login=creator.login)
    # create invite to user
    sent_invite_to = SentInvites(invite_id=invite.invite_id,
                                 login=new_user.login)

    # database duplicate check
    users = db_get_users(Users.login == invited_email)
    if len(users) > 0:
        if users[0]['role'] == 'deleted':
            db_session = Session()
            # create new invite id
            db_session.add(invite)
            db_session.commit()
            # create new users-invite link
            db_session.add(sent_invite_from)
            db_session.add(sent_invite_to)
            db_session.commit()
            # change password from deleted to new, based on invite_id
            update_q = update(Users).where(
                    Users.login == invited_email). \
                values(user_password=new_user.user_password)
            db_session.execute(update_q)
            db_session.commit()
            db_session.close()
            # reload users, for next checks
            users = db_get_users(Users.login == invited_email)
        # user already created
        if users[0]['register_status']:
            # user already registered
            logger.info(f'User {current_user.login} '
                        f'tried to create invite for '
                        f'already registered user: {invited_email}')
            return False
        if users[0]['role'] != role:
            # user role another from db role
            # check user role is valid
            query = db_session.query(UserRoles).filter(
                UserRoles.user_role == role)
            if len(query.all()) == 0:
                return False
            update_q = update(RolesOfUsers).where(
                    RolesOfUsers.login == invited_email). \
                values(user_role=role)
            db_session.execute(update_q)
            db_session.commit()
            logger.info(f'User {current_user.login} '
                        f'update role for unregistered user: {invited_email}')

        logger.info(f'User {current_user.login} '
                    f'resend invite to: {invited_email}')
        return True
    else:
        # no user in DB
        db_session.add(invite)
        db_session.commit()
        db_session.add(new_user)
        db_session.commit()
        db_session.add(new_user_role)
        db_session.commit()
        db_session.add(sent_invite_from)
        db_session.add(sent_invite_to)
        db_session.commit()
        db_session.close()
        logger.info(f'created invite for e-mail: {invited_email}')
        return True