def get(self, request, *args, **kwargs): data = dict() # TODO: Cache queries companies = Company.objects.select_related("keyfigures").order_by( "-id")[:5] bond_rate = InterestRate.get_latest_rate() companies_count = Company.objects.count() orders = Order.objects.aggregate( sell_count=Count("id", filter=Q(typ=Order.type_sell())), buy_count=Count("id", filter=Q(typ=Order.type_buy())), ) sell_orders_count = orders.get("sell_count") buy_orders_count = orders.get("buy_count") serializer = CompanySidebarSerializer(companies, many=True) data["companies"] = serializer.data data["bond_rate"] = bond_rate data["companies_count"] = companies_count data["buy_orders_count"] = buy_orders_count data["sell_orders_count"] = sell_orders_count return Response(data=data)
def test_sidebar(self): url = reverse("core:sidebar") rsp = self.client.get(url) cb = Company.get_centralbank() should_be = { "companies": [ { "name": self.company.name, "isin": self.company.isin, "id": self.company.id, "share_price": float(self.company.keyfigures.share_price), }, { "name": cb.name, "isin": cb.isin, "id": cb.id, "share_price": float(cb.keyfigures.share_price) }, ], "bond_rate": float(InterestRate.get_latest_rate()), "companies_count": Company.objects.count(), "buy_orders_count": Order.objects.filter(typ=Order.type_buy()).count(), "sell_orders_count": Order.objects.filter(typ=Order.type_sell()).count(), } self.assertDictEqual(should_be, rsp.json())
def get(self): commodities = Commoditie.query().order(Commoditie.code).fetch() exchanges = Exchange.query().order(Exchange.code).fetch() stocks = Stock.query().order(Stock.code).fetch() interests = InterestRate.query().order(InterestRate.code).fetch() self.render('main_page.html', commodities=commodities, exchanges=exchanges, stocks=stocks, interests=interests)
def create_bond(value, company_isin, runtime) -> Bond: """ Celery-Task for buying a bond. :param value: :param company_isin: :param runtime: :return: TODO: Actually not needed. Delete it! """ rate = InterestRate.calc_rate(value) company_id = Company.get_id_from_isin(company_isin) bond = Bond.objects.create(company_id=company_id, value=value, rate=rate, runtime=runtime) return bond
def create(self, validated_data): company_isin = validated_data.get("company_isin") value = validated_data.get("value") runtime = validated_data.get("runtime") rate = InterestRate.calc_rate(value) company_id = Company.get_id_from_isin(company_isin) with transaction.atomic(): # TODO: I think this can be replaced by simply writing: # c.cash = c.cash - value # as this inside in a transaction atomic block but I am not 100% sure # Need to check the docs Company.objects.filter(id=company_id).update(cash=F("cash") - value) bond = Bond.objects.create(company_id=company_id, value=value, rate=rate, runtime=runtime) return bond
def get(self): Commoditie.populate() Exchange.populate() Stock.populate() InterestRate.populate() commodities = Commoditie.query().fetch() for commoditie in commodities: params = QuandlHelper.get_basic_info(commoditie.code) params = QuandlHelper.get_day_variation(commoditie.code, params) params = QuandlHelper.get_month_variation(commoditie.code, params) params = QuandlHelper.get_year_variation(commoditie.code, params) if 'error' not in params: commoditie.name = params['name'] commoditie.dayVariation = params['day_variation'] commoditie.monthVariation = params['month_variation'] commoditie.yearVariation = params['year_variation'] commoditie.updated = params['last_date'] commoditie.value = params['value'] commoditie.put() exchanges = Exchange.query().fetch() for exchange in exchanges: params = QuandlHelper.get_basic_info(exchange.code) params = QuandlHelper.get_day_variation(exchange.code, params) params = QuandlHelper.get_month_variation(exchange.code, params) params = QuandlHelper.get_year_variation(exchange.code, params) if 'error' not in params: exchange.name = params['name'] exchange.dayVariation = params['day_variation'] exchange.monthVariation = params['month_variation'] exchange.yearVariation = params['year_variation'] exchange.updated = params['last_date'] exchange.value = params['value'] exchange.put() stocks = Stock.query().fetch() for stock in stocks: params = QuandlHelper.get_basic_info(stock.code) params = QuandlHelper.get_day_variation(stock.code, params) params = QuandlHelper.get_month_variation(stock.code, params) params = QuandlHelper.get_year_variation(stock.code, params) if 'error' not in params: stock.name = params['name'] stock.dayVariation = params['day_variation'] stock.monthVariation = params['month_variation'] stock.yearVariation = params['year_variation'] stock.updated = params['last_date'] stock.value = params['value'] stock.put() interestRates = InterestRate.query().fetch() for interest in interestRates: params = QuandlHelper.get_basic_info(interest.code) params = QuandlHelper.get_day_variation(interest.code, params) params = QuandlHelper.get_month_variation(interest.code, params) params = QuandlHelper.get_year_variation(interest.code, params) if 'error' not in params: interest.name = params['name'] interest.dayVariation = params['day_variation'] interest.monthVariation = params['month_variation'] interest.yearVariation = params['year_variation'] interest.updated = params['last_date'] interest.value = params['value'] interest.put()
def test_latest_rate(self): before = InterestRate.get_latest_rate() InterestRate.objects.create(rate=before + 1) after = InterestRate.get_latest_rate() self.assertTrue(after > before)