def post(self, requests, *args, **kwargs): if "HTTP_X_HTTP_METHOD_OVERRIDE" in requests.META: if requests.META["HTTP_X_HTTP_METHOD_OVERRIDE"] == "DELETE": return self.delete(requests, *args, **kwargs) model = ujson.loads(requests.POST.get("model", '{}')) goal = Goal() goal.account = self.client.accounts.first() goal.name = model.get("name") goal.type = model.get("goalType") if "ETHICAL" in goal.type: goal.custom_portfolio_set = PortfolioSet.objects.get(name="Ethical") goal.account_type = model.get("accountType") goal.completion_date = datetime.strptime( model.get("goalCompletionDate"), '%Y%m%d%H%M%S') goal.allocation = model.get("allocation") goal.target = model.get("goalAmount", None) if goal.target is None: goal.target = 0 goal.income = model.get("income", False) goal.save() return HttpResponse(ujson.dumps({"id": goal.pk}), content_type='application/json')
def calculate_portfolios(): # calculate default portfolio yahoo_api = get_api("YAHOO") for ps in PortfolioSet.objects.all(): goal = Goal() goal.custom_portfolio_set = ps ps.portfolios = json.dumps( calculate_portfolios_for_goal(goal, api=yahoo_api)) ps.save() # calculate portfolios for goal in Goal.objects.all(): if goal.is_custom_size: goal.portfolios = json.dumps( calculate_portfolios_for_goal(goal, api=yahoo_api)) goal.save()