예제 #1
0
async def get_currencies(_: User = Depends(get_current_user)):
    query = currency.select()
    records = await database.fetch_all(query)
    currencies = []
    for record in records:
        currencies.append({
            "iso_currency": record.iso_currency,
            "last_rate": record.last_rate
        })
    return {"currencies": currencies}
예제 #2
0
async def update_currency(currency_id: int,
                          user: User = Depends(get_current_user)):
    query = currency.select().where(currency.c.currency_id == currency_id)
    currency_record = await database.fetch_one(query)
    if currency_record.iso_currency == user.base_currency:
        return {
            "iso_currency": currency_record.iso_currency,
            "last_rate": 1,
        }
    currency_info = await call_yahoo_from_view(currency_record.symbol)
    query = currency.update()
    query = query.values(last_rate=currency_info[YAHOO_FIELD_PRICE])
    query = query.where(currency.c.symbol == currency_record.symbol)
    await database.execute(query)
    return {
        "iso_currency": currency_record.iso_currency,
        "last_rate": currency_info[YAHOO_FIELD_PRICE],
    }
예제 #3
0
async def update_currencies(user: User = Depends(get_current_user)):
    query = currency.select().where(
        currency.c.iso_currency != user.base_currency)
    symbol_records = await database.fetch_all(query)
    symbols = []
    for record in symbol_records:
        symbols.append(record.symbol)
    currencies_to_update = await get_yahoo_quote(symbols)
    updated_currencies = []
    for symbol in currencies_to_update:
        last_rate = currencies_to_update[symbol][YAHOO_FIELD_PRICE]
        query = (currency.update().values(last_rate=last_rate).where(
            currency.c.symbol == symbol))
        await database.execute(query)
        updated_currencies.append({
            "iso_currency":
            currencies_to_update[symbol][YAHOO_FIELD_CURRENCY],
            "last_rate":
            last_rate,
        })
    return {"currencies": updated_currencies}
예제 #4
0
파일: utils.py 프로젝트: akita8/santaka
async def update_currency():
    now = datetime.utcnow()
    one_hour_before = now + timedelta(seconds=YAHOO_UPDATE_DELTA)
    timezoned_now = now.astimezone(DEFAULT_TRADING_TIMEZONE)
    if timezoned_now.weekday() in (5, 6):
        return
    query = (currency.select().where(
        currency.c.last_update < one_hour_before, ).order_by(
            asc(currency.c.last_update)))
    oldest_currency = await database.fetch_one(query)
    if oldest_currency:
        logger.info("trying to update %d currency",
                    oldest_currency.currency_id)
        quotes = await get_yahoo_quote([oldest_currency.symbol])
        if currency.symbol not in quotes:
            raise YahooError("yahoo failed to return the quote for %s",
                             oldest_currency.symbol)
        query = (currency.update().values(
            last_rate=quotes[currency.symbol][YAHOO_FIELD_PRICE],
            last_update=now,
        ).where(currency.c.currency_id == oldest_currency.currency_id))
        await database.execute(query)
예제 #5
0
async def create_stock(new_stock: NewStock,
                       user: User = Depends(get_current_user)):
    # query the database to check if stock already exists
    stock_symbol = new_stock.symbol.upper()
    stock_records = await get_stock_records(stock_symbol)

    # create response dict from body model
    stock = new_stock.dict()

    if not stock_records:
        # stock not found in the database, calling yahoo to get stock info
        stock_info = await call_yahoo_from_view(stock_symbol)
        iso_currency = stock_info[YAHOO_FIELD_CURRENCY]

        # check if currency already exists in database
        query = currency.select().where(
            currency.c.iso_currency == iso_currency)
        currency_record = await database.fetch_one(query)

        if currency_record is None:
            # handle if currency does not exist
            # if stock currency is the default one use last rate of 1
            last_rate = 1
            symbol = None
            if iso_currency != user.base_currency:
                # if stock currency is not the default one call yahoo
                #  to get currency info
                symbol = f"{user.base_currency}{iso_currency}=X".upper()
                currency_info = await call_yahoo_from_view(symbol)
                last_rate = currency_info[YAHOO_FIELD_PRICE]
            # save currency record in the database and get the record id
            query = currency.insert().values(
                currency_id=create_random_id(),
                iso_currency=iso_currency,
                last_rate=last_rate,
                symbol=symbol,
                last_update=datetime.utcnow(),
            )
            currency_id = await database.execute(query)
        else:
            # if currency exists just save the id (needed for stock creation)
            currency_id = currency_record.currency_id

        # create stock record
        query = stocks.insert().values(
            stock_id=create_random_id(),
            short_name=stock_info[YAHOO_FIELD_NAME],
            currency_id=currency_id,
            market=stock_info[YAHOO_FIELD_MARKET],
            symbol=stock_symbol,
            last_price=stock_info[YAHOO_FIELD_PRICE],
            last_update=datetime.utcnow(),
            financial_currency=stock_info.get(YAHOO_FIELD_FINANCIAL_CURRENCY),
        )
        stock_id = await database.execute(query)
        stock["short_name"] = stock_info[YAHOO_FIELD_NAME]
        stock["iso_currency"] = iso_currency
        stock["currency_id"] = currency_id
        stock["market"] = stock_info[YAHOO_FIELD_MARKET]
        stock["symbol"] = stock_symbol
        stock["last_price"] = stock_info[YAHOO_FIELD_PRICE]
    else:
        # if stock record exists already
        stock_record = stock_records[0]
        stock_id = stock_record[3]
        stock["short_name"] = stock_record[1]
        stock["iso_currency"] = stock_record[6]
        stock["currency_id"] = stock_record[5]
        stock["market"] = stock_record[4]
        stock["symbol"] = stock_symbol
        stock["last_price"] = stock_record[0]
    stock["stock_id"] = stock_id

    return stock