def index(request): # Query the stock table, filter for top ranked stocks and order by their rank. top_stocks = stock_api._get_top_stocks() return render(request, 'index.html', { 'page_title': 'Main', 'data': top_stocks })
def update_top_stocks(self): print('requesting stocks from api ...') top_stocks = stock_api._get_top_stocks() print('requesting crypto from api ...') top_crypto = stock_api.get_top_crypto() print('Writing to django DB') self.add_stocks(top_stocks) self.add_crypto(top_crypto) print('You are all set!')
def update_top_stocks(self): top_stocks = stock_api._get_top_stocks() index = 1 for stock in top_stocks: # This searches for a stock with the given 'symbol' (the primary key) # and updates/create it with the values specified in the 'defaults' parameter stock_model, created = Stock.objects.update_or_create(symbol=stock['symbol'], defaults={ 'name': stock['companyName'], 'top_rank': index, 'price': stock['latestPrice'], 'change': stock['change'], 'change_percent': stock['changePercent'], 'market_cap': stock['marketCap'], 'primary_exchange': stock['primaryExchange'], }) stock_model.save() index += 1
def test_get_top_stocks(self): response = stock_api._get_top_stocks() self.assertIsNotNone(response) # function name get_top_stocks actually returns top 20 stocks # that is why the following line checks with 20 self.assertEqual(len(response), 20)