Esempio n. 1
0
def create_user(user_to_create):
    session = Session()

    try:
        session.query(User).filter_by(email=user_to_create.get('email')).one()
    except NoResultFound:
        password = generate_password_hash(user_to_create.get('password'))
        user = User(email=user_to_create.get('email'), username=user_to_create.get('username'), password=password)
        session.add(user)
        session.commit()
        session.refresh(user)

        return UserData().dump(user)
    else:
        raise ValidationError("Email must be unique")
Esempio n. 2
0
def create_wallet(wallet_to_create):

    if auth.current_user().id != wallet_to_create.get('user_id'):
        raise NoResultFound('You don\'t have required permission')

    session = Session()
    try:
        session.query(Wallet).filter_by(name=wallet_to_create.get('name')).one()
    except NoResultFound:
        wallet = Wallet(name=wallet_to_create.get('name'), currency=wallet_to_create.get('currency'),
                        user_id=wallet_to_create.get('user_id'))
        session.add(wallet)
        session.commit()
        session.refresh(wallet)

        return WalletData().dump(wallet)
    else:
        raise ValidationError("Wallet name must be unique")
Esempio n. 3
0
def send_money(wallet_name, transaction):
    session = Session()

    try:
        sender = session.query(Wallet).filter_by(name=wallet_name).one()
        if sender.user_id != auth.current_user().id:
            raise NoResultFound('You don\'t have required permission')

        recipient = session.query(Wallet).filter_by(name=transaction.get('wallet_recipient')).one()
    except NoResultFound:
        raise NoResultFound('Invalid wallet name')

    if sender.balance < transaction.get('cost'):
        raise ValidationError("Not enough balance")

    amount_received = convert_currency(transaction.get('cost'), sender.currency, recipient.currency)

    sender.balance -= transaction.get('cost')
    recipient.balance += amount_received

    session.commit()
    session.refresh(sender)

    return WalletData().dump(sender)