def investor_profile(request, investor_id): """ Create, Edit and View an investor profile. :param request: :param investor_id: Investor ID :return: """ # Gets investor object investor = get_object_or_404(Investor, id=investor_id) if request.method == 'GET': serializer = InvestorSerializer(investor) return Response(serializer.data, status=status.HTTP_200_OK) elif request.method == 'PUT': serializer = InvestorSerializer(investor, data=request.data, partial=True) return helper.update_serializer(serializer) elif request.method == 'DELETE': investor.delete() return Response(CustomJsonResponse.return_successful_delete(), status=status.HTTP_200_OK)
def update_stock_plan(plan_id, stock_total, market_price): """ :param plan_id: :param stock_total: :param market_price: :return: """ try: plan = get_plan_by_id(plan_id) updated_plan = PlanSerializer(plan).data updated_plan['target_sell_price'] = calculate_target_sell_price( 10, stock_total['avg_net_price']) status = check_plan_status(updated_plan, market_price) plan_statues = get_plan_statuses() plan_status = next(item for item in plan_statues if item["status_name"] == status) updated_plan['status'] = plan_status['id'] return helper.update_serializer( PlanSerializer(plan, data=updated_plan, partial=True)) except Exception as e: print(e) raise e
def plan_detail(request, investor_id, portfolio_id, symbol, plan_id): plan = plan_services.get_plan(investor_id, portfolio_id, symbol, plan_id) if request.method == 'PUT': return helper.update_serializer( PlanSerializer(plan, data=request.data, partial=True)) if request.method == 'GET': return Response(PlanSerializer(plan).data) if request.method == 'DELETE': plan.delete() return Response(CustomJsonResponse.return_successful_delete(), status=status.HTTP_200_OK)
def update_transaction(investor_id, portfolio_id, stock_id, transaction_id, transaction_req_data): transaction_req_data.update( get_transaction_calculation_response(transaction_req_data)) transaction_orm_obj = get_transaction(investor_id, portfolio_id, stock_id, transaction_id) transaction = helper.update_serializer( TransactionSerializer(transaction_orm_obj, data=transaction_req_data, partial=True)) plan_id = plan_services.get_plan_id_by_stock_id(stock_id) # update stock plan stock_total = stock_services.get_stock_totals_by_id(stock_id) stock_obj = stock_services.get_stock(investor_id, portfolio_id, stock_id) market_price = jamstockex_api_service.get_market_price(stock_obj['symbol']) plan_services.update_stock_plan(plan_id, stock_total, market_price) return transaction
def portfolio_detail(request, investor_id, portfolio_id): """ Retrieve, update or delete a portfolio. """ # Gets portfolio portfolio = portfolio_services.get_portfolio(investor_id, portfolio_id) if request.method == 'GET': portfolio_response = PortfolioSerializer(portfolio).data portfolio_response.update( stock_services.get_stocks_totals(investor_id, portfolio_id)) return Response(portfolio_response, status=status.HTTP_200_OK) elif request.method == 'PUT': return helper.update_serializer( PortfolioSerializer(portfolio, data=request.data, partial=True)) elif request.method == 'DELETE': portfolio.delete() return Response(CustomJsonResponse.return_successful_delete(), status=status.HTTP_200_OK)
def notification_detail(request, investor_id, portfolio_id, stock_name, notification_id): """ Retrieve, update or delete a portfolio stock notification. """ notification = get_object_or_404(Notification, stock__portfolio__user__id=investor_id, stock__portfolio=portfolio_id, stock__symbol=stock_name, pk=notification_id) if request.method == 'GET': serializer = NotificationSerializer(notification) return Response(serializer.data) elif request.method == 'PUT': serializer = NotificationSerializer(notification, data=request.data, partial=True) return helper.update_serializer(serializer) elif request.method == 'DELETE': notification.delete() return Response(CustomJsonResponse.return_successful_delete(), status=status.HTTP_200_OK)
def update_is_archived(investor_id, portfolio_id, stock_id, is_archived, archived_date): """ :param investor_id: :param portfolio_id: :param stock_id: :param is_archived: :param archived_date: :return: """ try: data = { "is_archived": is_archived, "archived_date": archived_date, } stock = get_stock_serializer(investor_id, portfolio_id, stock_id) return helper.update_serializer( StockSerializer(stock, data=data, partial=True)) except Exception as e: print('Error in update_is_archived => ', e)