Example #1
0
def update_password(current_password=Form(...),
                    new_password=Form(...),
                    user=Depends(manager),
                    db: database.Session = Depends(database.get_db)):
    if not user:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
                            detail='Not authenticated')
    elif len(new_password) < 7:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail='Password must be 8 characters or more')
    elif len(new_password) >= 40:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail='Password is too long. Maximum length: 40 characters')
    elif PasswordStats(new_password).strength() <= float(0.350):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=
            'Password is not strong enough. Try adding some symbols or numbers your password'
        )
    else:
        db_user = db.query(
            database.User).filter_by(username=user.username).first()
        if database.Hash.verify_password(current_password, db_user.password):
            db_user.password = database.Hash.get_password_hash(new_password)
            db.commit()
            db.refresh(db_user)
            return {'detail': 'Passwored changed'}
        else:
            raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                                detail='Current password is incorrect')
Example #2
0
async def create_user(User: UserCreate,
                      db: Session = Depends(get_db),
                      current_user: User = Depends(get_current_active_user)):
    try:
        user = UserModel(**User.dict())
        user.password = encryptPassword(user.password)
        db.add(user)
        db.commit()
        db.refresh(user)
    except SQLAlchemyError as e:
        raise Exception(e)
    return user
Example #3
0
def signup(username=Form(...),
           email=Form(...),
           password=Form(...),
           db: database.Session = Depends(database.get_db)):
    if db.query(database.User).filter_by(username=username).first():
        raise HTTPException(status_code=status.HTTP_302_FOUND,
                            detail='Username taken')
    elif db.query(database.User).filter_by(email=email).first():
        raise HTTPException(status_code=status.HTTP_302_FOUND,
                            detail='Email address already in use')
    elif len(username) <= 3:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail='Username must be longer than 3 characters')
    elif len(username) >= 25:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail='Username is too long. Maximum length: 25 characters')
    elif len(password) < 7:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                            detail='Password must be 8 characters or more')
    elif len(password) >= 40:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail='Password is too long. Maximum length: 40 characters')
    elif PasswordStats(password).strength() <= float(0.350):
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=
            'Password is not strong enough. Try adding some symbols or numbers your password'
        )
    elif len(email) >= 75:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail='Email is too long. Must be less than 75 characters')
    try:
        valid = validate_email(email)
        email = valid.email
    except EmailNotValidError as e:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail='Email address not supported. Please use another email.')
    else:
        pwd_hash = Hash.get_password_hash(str(password))
        db_user = database.User(username=username,
                                email=email,
                                password=pwd_hash)
        db.add(db_user)
        db.commit()
        db.refresh(db_user)
        return models.User(username=username, email=email, password=password)
Example #4
0
def update_email(email=Form(...),
                 user=Depends(manager),
                 db: database.Session = Depends(database.get_db)):
    if not user:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
                            detail='Not authenticated')
    else:
        if len(email) >= 75:
            raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                                detail='Email is too long')
        else:
            db_user = db.query(
                database.User).filter_by(username=user.username).first()
            db_user.email = email
            db.commit()
            db.refresh(db_user)
            return {'detail', 'Email successfully changed'}
Example #5
0
def create_listing(title: str = Form(...),
                   context: str = Form(...),
                   user=Depends(manager),
                   db: database.Session = Depends(database.get_db)):
    if len(title) >= 100:
        raise HTTPException(status.HTTP_403_FORBIDDEN, detail='Title too long')
    elif len(context) >= 1000:
        raise HTTPException(status.HTTP_403_FORBIDDEN,
                            detail='Context too long')
    else:
        db_listing = database.Listing(created_at=time.time(),
                                      title=title,
                                      author=user.username,
                                      context=context)
        db.add(db_listing)
        db.commit()
        db.refresh(db_listing)
    return models.Listing(post_id=str(db_listing.id),
                          created_at=time.time(),
                          title=title,
                          author=user.username,
                          context=context)
Example #6
0
def generate_accounts_and_transactions():
    db = Session()
    list_of_users = get_users(db)

    for _ in range(20):
        from_user = random.choice(list_of_users).id
        to_user = random.choice(list_of_users).id
        db_account = Account(
            name=random.choice(names_pull),
            balance=random.randint(10000, 1000000000),
            currency=random.choice(currencies),
            user_id=from_user,
            type=random.randint(1, 3),
            interest=random.random() * 10,
        )

        db_account2 = Account(
            name=random.choice(names_pull),
            balance=random.randint(10000, 1000000000),
            currency=random.choice(currencies),
            user_id=to_user,
            type=random.randint(1, 3),
            interest=random.random() * 10,
        )

        db.add(db_account)
        db.add(db_account2)
        db.commit()
        db.refresh(db_account)
        db.refresh(db_account2)

        db_transaction = Transaction(
            from_user_id=from_user,
            to_user_id=to_user,
            from_account_id=db_account.id,
            to_account_id=db_account2.id,
            amount=random.randint(1, db_account.balance),
        )

        db.add(db_transaction)

    db.commit()

    for _ in range(2000):
        accounts = db.query(Account).all()

        first_acc = random.choice(accounts)
        second_acc = random.choice(accounts)

        from_user = first_acc.user_id
        to_user = second_acc.user_id

        db_transaction = Transaction(
            from_user_id=from_user,
            to_user_id=to_user,
            from_account_id=first_acc.id,
            to_account_id=second_acc.id,
            amount=random.randint(1, first_acc.balance),
            created_at=datetime.datetime(2019, random.randint(1, 12),
                                         random.randint(1, 27)),
        )
        db.add(db_transaction)
        db.commit()
        db.refresh(db_transaction)