Exemplo n.º 1
0
def update_diet(request, restrictions, calories, fat, sugar, protein):
	if request.user.is_authenticated():
		try:
			diet = UserDiet.objects.get(user=request.user)
			diet.diet_description = restrictions
			diet.calories = calories
			diet.fat = fat
			diet.sugar = sugar
			diet.protein = protein
			diet.save()
			return json.dumps({'success': True})
		except:
			diet = UserDiet(user=request.user, diet_description=restrictions, calories=calories, fat=fat, sugar=sugar, protein=protein)
			diet.save()
			return json.dumps({'success': True})
	return json.dumps({'success': False})
Exemplo n.º 2
0
def save_settings(request):
	if request.user.is_authenticated() and request.method == 'POST':
		form = Settings(request.POST)
		form.is_valid()
		if 'old_password' in form.cleaned_data:
			if not check_password(form.cleaned_data['old_password'],request.user.password):
				messages.error(request, "Incorrect password.")
				return redirect('/settings')
			if form.cleaned_data['new_password'] == form.cleaned_data['new_password_confirm']:
				request.user.set_password(form.cleaned_data['new_password'])
				request.user.save()
				messages.success(request, "Save successful.")
				return redirect('/settings')
			else:
				messages.error(request, "Your new passwords did not match.")
				return redirect('/settings')
		if form['fat'].errors or form['calories'].errors or form['protein'].errors or form['carbs'].errors:
			messages.error(request, "Diet parameters must be integers.")
			return redirect('/settings')
		try:
			diet = UserDiet.objects.get(user=request.user)
			diet.halal = form.cleaned_data['halal']
			diet.lacto = form.cleaned_data['lacto']
			diet.lactoovo = form.cleaned_data['lactoovo']
			diet.vegan = form.cleaned_data['vegan']
			diet.diabetes = form.cleaned_data['diabetes']
			diet.hypertension = form.cleaned_data['hypertension']
			diet.nuts = form.cleaned_data['nuts']
			diet.lactose = form.cleaned_data['lactose']
			diet.eggs = form.cleaned_data['eggs']
			diet.soy = form.cleaned_data['soy']
			diet.shellfish = form.cleaned_data['shellfish']
			diet.fish = form.cleaned_data['fish']
		except:
			diet = UserDiet(user=request.user, halal=form.cleaned_data['halal'], lacto=form.cleaned_data['lacto'],
				lactoovo=form.cleaned_data['lactoovo'], vegan=form.cleaned_data['vegan'], diabetes=form.cleaned_data['diabetes'],
				hypertension=form.cleaned_data['hypertension'], nuts=form.cleaned_data['nuts'], lactose=form.cleaned_data['lactose'],
				eggs=form.cleaned_data['eggs'], soy=form.cleaned_data['soy'], shellfish=form.cleaned_data['shellfish'], fish=form.cleaned_data['fish'])
		diet.calories = form.cleaned_data['calories']
		diet.fat = form.cleaned_data['fat']
		diet.sugar = form.cleaned_data['carbs']
		diet.protein = form.cleaned_data['protein']
		diet.save()
		messages.success(request, "Save successful.")
		return redirect('/settings')
	return redirect('/home')