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 CreateGoal(request): storage = messages.get_messages(request) storage.used = True if not checkActiveGoal(request): if request.method=='POST': form=GoalForm(request.POST) if form.is_valid(): goal = Goal() goal.deadline=form.cleaned_data.get('deadline') weightloser=WeightLoser.objects.get(user=request.user) goal.current_weight=float(str(form.cleaned_data['currWeight'])) goal.target_weight=float(str(form.cleaned_data['currWeight'])) - float(str(form.cleaned_data['losepounds'])) charity=Charity.objects.get(charityId=form.cleaned_data['charitylist']) goal = Goal(weightloser=weightloser,deadline=form.cleaned_data['deadline'],status='inprogress') goal.charity=charity goal.current_weight=float(str(form.cleaned_data['currWeight'])) goal.target_weight=float(str(form.cleaned_data['currWeight'])) - float(str(form.cleaned_data['losepounds'])) goal.benchmark=goal.current_weight goal.toLose=(goal.current_weight-goal.target_weight) goal.progress=0 goal.save() # goal=Goal.objects.get(weightloser=weightloser) #check Ali trackGoal=TrackGoal() trackGoal.goal=goal trackGoal.weight=goal.current_weight trackGoal.track_date=goal.creation_date trackGoal.save() setCheckActiveGoal(request,"T") # set that the user has active goal messages.success(request, 'the goal has been created, now it is time to get some sponsors!', 'Created_created') return HttpResponseRedirect('/inviteSponsor/') else: #display the form when not valid return render_to_response('CreateGoal.html',{'form':form}, context_instance=RequestContext(request)) else: #Showing the form '''blank Form''' weightloser=WeightLoser.objects.get(user=request.user) form=GoalForm() context={'form':form} return render_to_response('CreateGoal.html',context, context_instance=RequestContext(request)) else: messages.error(request, "You already have an active Goal, you can't have more than 1 active goal at a time", "Goal_Created") return HttpResponseRedirect('/addWeight/')
def get_context_data(self, *args, **kwargs): goal_pk = kwargs.get("goal_pk", None) ctx = super(PortfolioAssetClasses, self).get_context_data(*args, **kwargs) if goal_pk: goal = get_object_or_404(Goal, pk=goal_pk) ctx["portfolio_set"] = goal.portfolio_set else: ctx["portfolio_set"] = Goal().portfolio_set return ctx
def get(self, request, *args, **kwargs): portfolio_set = Goal().portfolio_set goal_pk = kwargs.get("goal_pk", None) if goal_pk: try: goal = Goal.objects.get(pk=goal_pk, account__primary_owner=self.client) except ObjectDoesNotExist: goal = None if goal: if goal.is_custom_size: if goal.portfolios in [None, "{}", "[]", ""]: try: portfolios = calculate_portfolios(goal) goal.portfolios = ujson.dumps(portfolios, double_precision=2) goal.save() except OptimizationException: goal.custom_regions = None goal.save() portfolios = ujson.loads(goal.portfolio_set.portfolios) else: portfolios = ujson.loads(goal.portfolios) else: portfolios = ujson.loads(goal.portfolio_set.portfolios) ret = [] for k in portfolios: new_pr = { "risk": int(100 * portfolios[k]["risk"]) / 100, "expectedReturn": portfolios[k]["expectedReturn"], "volatility": portfolios[k]["volatility"], 'allocations': portfolios[k]["allocations"] } ret.append(new_pr) return HttpResponse(ujson.dumps(ret), content_type="application/json") ret = [] portfolios = ujson.loads(portfolio_set.portfolios) for k in portfolios: new_pr = { "risk": int(100 * portfolios[k]["risk"]) / 100, "expectedReturn": portfolios[k]["expectedReturn"], "volatility": portfolios[k]["volatility"], 'allocations': portfolios[k]["allocations"] } ret.append(new_pr) return HttpResponse(ujson.dumps(ret), 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()
def __create_goal(self): self.become_a_yogi = Goal() self.become_a_yogi.owner = self.user_marie_houana self.become_a_yogi.title = "Become a yogi" self.become_a_yogi.is_draft = False self.become_a_yogi.save()