def put(self, request, symbol, shares): symbol, shares = symbol, shares stock = self.get_object(symbol) price, name = search_stock(symbol) stock.sell(shares, int(price)) Transaction.create_transaction(self.request.user, 'sold some stock', stock) serializer = StockSerializer(stock, context={'request': request}) return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
def post(self, request, symbol, shares): symbol, shares = symbol, shares price, company_name = search_stock(symbol) total_price = int(price) * int(shares) stock = Stock.objects.filter(owner=self.request.user, symbol=symbol) if stock.exists(): stock = Stock.objects.get(owner=self.request.user, symbol=symbol) stock.add_more(shares, price) Transaction.create_transaction(self.request.user, 'bought more stock', stock) else: stock = Stock.objects.buy_stock(owner=self.request.user, name=company_name, symbol=symbol, unit_price=price, shares=shares, total_price=total_price) Transaction.create_transaction(self.request.user, 'bought a stock', stock) serializer = StockSerializer(stock, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED)