def get_user_days(user): # Get all the days between today and 10 days before # user signed up days = list(user.days.all()) if not days: used_dates = [] used_dates = [day.date for day in days] today = datetime.date.today() # set first date checked currently_checked_date = user.date_joined.date() - datetime.timedelta( days=10) while currently_checked_date < today: if currently_checked_date not in used_dates: day = Day(user=user, date=currently_checked_date) day.save() days.append(day) currently_checked_date = currently_checked_date + datetime.timedelta( days=1) return days
def user_data(request): form = UserDataForm(request.POST or None) if request.method == 'POST': if form.is_valid(): user = request.user user.userprofile.age = form.cleaned_data['age'] user.userprofile.weight = form.cleaned_data['weight'] user.userprofile.height = form.cleaned_data['height'] user.userprofile.sex = form.cleaned_data['sex'] user.userprofile.kcal = form.cleaned_data['kcal'] user.userprofile.protein = form.cleaned_data['protein'] user.userprofile.carbohydrates = form.cleaned_data['carbohydrates'] user.userprofile.fats = form.cleaned_data['fats'] user.userprofile.water = form.cleaned_data['water'] user.userprofile.steps = form.cleaned_data['steps'] user.save() if user.userprofile.first_login == 1: user.userprofile.first_login = 0 user.userprofile.pulse = 60 user.save() meal_set = MealSet() meal_set.save() day = Day( date=datetime.datetime.now(), weight=user.userprofile.weight, pulse=user.userprofile.pulse, expected_kcal=user.userprofile.kcal, expected_protein=user.userprofile.protein, expected_carbohydrates=user.userprofile.carbohydrates, expected_fats=user.userprofile.fats, expected_water=user.userprofile.water, expected_steps=user.userprofile.steps, meal_set=meal_set, user=user) day.save() else: day = user.day_set.filter(date=datetime.datetime.now()).first() day.weight = form.cleaned_data['weight'] day.expected_kcal = form.cleaned_data['kcal'] day.expected_protein = form.cleaned_data['protein'] day.expected_carbohydrates = form.cleaned_data['carbohydrates'] day.expected_fats = form.cleaned_data['fats'] day.expected_water = form.cleaned_data['water'] day.expected_steps = form.cleaned_data['steps'] day.save() return redirect('/user_panel') return redirect('/index') if request.user.userprofile.first_login == 0: form = UserDataForm(initial={'sex': request.user.userprofile.sex}) context = {'form': form} return render(request, 'user_data.html', context)
def get_user_days(user): # Get all the days between today and 10 days before # user signed up days = list(user.days.all()) if not days: used_dates = [] used_dates = [day.date for day in days] today = datetime.date.today() # set first date checked currently_checked_date = user.date_joined.date() - datetime.timedelta(days=10) while currently_checked_date < today: if currently_checked_date not in used_dates: day = Day(user=user, date=currently_checked_date) day.save() days.append(day) currently_checked_date = currently_checked_date + datetime.timedelta(days=1) return days
def index(request): user = request.user date = datetime.datetime.now() if request.GET.dict(): day_param = request.GET.dict()['date'] year = int(day_param.split('-')[0]) month = int(day_param.split('-')[1]) day = int(day_param.split('-')[2]) date = datetime.date(year, month, day) day = user.day_set.filter(date=date).first() if day is None: previous_day = user.day_set.filter( date__lte=date).order_by('-date').first() next_day = user.day_set.filter(date__gte=date).order_by('date').first() meal_set = MealSet() meal_set.save() if previous_day is not None: day = Day( date=date, weight=previous_day.weight, pulse=previous_day.pulse, expected_kcal=previous_day.expected_kcal, expected_protein=previous_day.expected_protein, expected_carbohydrates=previous_day.expected_carbohydrates, expected_fats=previous_day.expected_fats, expected_water=previous_day.expected_water, expected_steps=previous_day.expected_steps, meal_set=meal_set, user=user) elif next_day is not None: day = Day(date=date, weight=next_day.weight, pulse=next_day.pulse, expected_kcal=next_day.expected_kcal, expected_protein=next_day.expected_protein, expected_carbohydrates=next_day.expected_carbohydrates, expected_fats=next_day.expected_fats, expected_water=next_day.expected_water, expected_steps=next_day.expected_steps, meal_set=meal_set, user=user) else: day = Day(date=date, weight=user.userprofile.weight, pulse=user.userprofile.pulse, expected_kcal=user.userprofile.kcal, expected_protein=user.userprofile.protein, expected_carbohydrates=user.userprofile.carbohydrates, expected_fats=user.userprofile.fats, expected_water=user.userprofile.water, expected_steps=user.userprofile.steps, meal_set=meal_set, user=user) day.save() date = add_zero_if_needed(day.date.day) + '/' + add_zero_if_needed( day.date.month) + '/' + str(day.date.year) activites = PhysicalActivity.objects.all() empty_bottles = math.ceil((day.expected_water - day.water) / 250) full_bottles = math.ceil(day.expected_water / 250) - empty_bottles trainings = day.training_set.all() meals = day.meal_set.meal_set.all() breakfast = meals.filter(meal_type=MealType.objects.filter( name='Breakfast').first()).first() lunch = meals.filter(meal_type=MealType.objects.filter( name='Lunch').first()).first() dinner = meals.filter(meal_type=MealType.objects.filter( name='Dinner').first()).first() others = meals.filter(meal_type=MealType.objects.filter( name='Other').first()).first() if breakfast is not None: breakfast = breakfast.mealproduct_set.all() if lunch is not None: lunch = lunch.mealproduct_set.all() if dinner is not None: dinner = dinner.mealproduct_set.all() if others is not None: others = others.mealproduct_set.all() context = { 'empty_loop_times': range(1, empty_bottles + 1), 'full_loop_times': range(1, full_bottles + 1), 'day': day, 'date': date, 'workout_time': convert_seconds_to_time_string(day.activity_time), 'activities': activites, 'meals': meals, 'breakfast': breakfast, 'lunch': lunch, 'dinner': dinner, 'others': others, 'trainings': trainings } return render(request, 'index.html', context)