async def add_new_rate(request): app = request.app router = app.router logger = app['logger'] engine = app['db'] user = app['user'] form = await request.post() bank = form['bank'] rate_buy = form['rate_buy'] rate_sale = form['rate_sale'] logger.info('Inserting new rate item for %s' % bank) async with engine.acquire() as conn: rate_id = await insert_new_rate( conn, bank=bank, rate_buy=rate_buy, rate_sale=rate_sale, ) # todo: add event? flash(request, 'Inserted new rate item [#%s]' % rate_id) return web.HTTPFound(router['rates'].url_for())
async def add_new_investment(request): app = request.app router = app.router logger = app['logger'] engine = app['db'] user = app['user'] form = await request.post() amount = float(form['amount']) currency = get_enum_by_value(Currency, value=form['currency']) bank = form['bank'] logger.info('Adding new investment for user %s' % user.email) async with engine.acquire() as conn: investment_id = await insert_new_investment( conn, amount=amount, currency=currency, bank=bank, user_id=user.id, ) # todo: add event? flash( request, 'Investment [#%d] of %.2f added for user %s' % (investment_id, amount, user.email)) return web.HTTPFound(router['investments'].url_for())
async def set_transaction_bought(request): app = request.app router = app.router logger = app['logger'] engine = app['db'] user = app['user'] t_id = request.match_info.get('t_id') logger.info('Confirming transaction #%s bought' % t_id) async with engine.acquire() as conn: # todo: get or 404 buy_result = await process_buy(conn, t_id=t_id) if buy_result is True: await set_transaction_status( conn, t_id=t_id, status=TransactionStatus.HANGING, ) flash(request, 'Set bought for transaction [#%s]' % t_id) else: flash(request, 'Cannot buy transaction [#%s]. Insufficiend fund' % t_id) # todo: add event? return web.HTTPFound(router['index'].url_for())
async def logout(request): app = request.app router = app.router logger = app['logger'] user = app['user'] session = await get_session(request) del session['user_id'] logger.info('Sign out user %s', user.email) flash(request, 'Logged out') return web.HTTPFound(router['login'].url_for())
async def delete_transaction(request): app = request.app router = app.router logger = app['logger'] engine = app['db'] user = app['user'] t_id = request.match_info.get('t_id') async with engine.acquire() as conn: await remove_transaction(conn, t_id=t_id) flash(request, 'Transaction [#%s] was removed' % t_id) return web.HTTPFound(router['index'].url_for())
async def delete_config(request): app = request.app router = app.router logger = app['logger'] engine = app['db'] user = app['user'] config_id = request.match_info.get('config_id') async with engine.acquire() as conn: await remove_config(conn, config_id=config_id) flash(request, 'Removed config [#%s]' % config_id) return web.HTTPFound(router['settings'].url_for())
async def save_config(request): app = request.app router = app.router logger = app['logger'] engine = app['db'] user = app['user'] data = await request.post() value = config_trafaret.check(data) logger.info('Saving new config initiated by user %s' % user.email) async with engine.acquire() as conn: await insert_new_config(conn, new_config=value, user_id=user.id) flash(request, 'New config was saved. Using it from this point') return web.HTTPFound(router['settings'].url_for())
async def set_transaction_sold(request): app = request.app router = app.router logger = app['logger'] engine = app['db'] t_id = request.match_info.get('t_id') logger.info('Confirming transaction #%s sold' % t_id) async with engine.acquire() as conn: # todo: get or 404 sale_result = await process_sale(conn, t_id=t_id) await set_transaction_status( conn, t_id=t_id, status=TransactionStatus.COMPLETED, ) # todo: add event? flash(request, 'Set sold for transaction [#%s]' % t_id) return web.HTTPFound(router['index'].url_for())
async def do_login(request): app = request.app router = app.router logger = app['logger'] engine = app['db'] form = await request.post() email = form['email'] password = form['password'] async with engine.acquire() as conn: user = await get_user(conn, email) if user is None: flash(request, 'No user with such email: %s' % email) logger.warning('Cannot find user %s', email) return web.HTTPFound(router['login'].url_for()) if not check_password(password, user.password): flash(request, 'Incorrect password') logger.warning('Wrong password for user %s', email) return web.HTTPFound(router['login'].url_for()) flash(request, 'Successfully logged in') session = await get_session(request) session['user_id'] = user.id return web.HTTPFound(router['index'].url_for())