def session_detail(request, session_id): #Define the local variables if request.method == 'POST': form = SetForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/workouts/sessions/' + session_id) else: raise Http404 else: #Create the form #Set default values for the fields (0x0 for the rep/weight info is used so it can be ignored later in other filters) form = SetForm(initial={'session':session_id, 'user':request.user.id, 'reps_mins':0, 'weight_resistance':0}) form.fields['session'].widget = forms.HiddenInput() form.fields['user'].widget = forms.HiddenInput() form.fields['reps_mins'].widget = forms.HiddenInput() form.fields['weight_resistance'].widget = forms.HiddenInput() try: #Get the session session = Session.objects.get(pk=session_id, user=request.user.id) exercise_library = [] #Get the full list of exercises for exercise_object in Exercise.objects.all(): #exercise_library.append({'label': str(exercise_object.name), 'value': str(exercise_object.id)}) exercise_library.append(str(exercise_object.name)) #Create a dictionary to store each set in the session by exercise sets_by_exercise = {} for workout_set in session.set_set.all(): if workout_set.exercise in sets_by_exercise: sets_by_exercise[workout_set.exercise].append([workout_set.reps_mins, workout_set.weight_resistance]) else: sets_by_exercise[workout_set.exercise] = [[workout_set.reps_mins, workout_set.weight_resistance], ] except Session.DoesNotExist: raise Http404 return render(request, 'workouts/session_detail.html', {'sets_by_exercise':sets_by_exercise, 'session':session, 'exercise_library':exercise_library, 'form':form})
def add_set(request, session_id): try: session = Session.objects.get(pk=session_id, user=request.user.id) if request.method == 'POST': form = SetForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/workouts/sessions/'+ str(session.id) +'/') else: form = SetForm(initial={'session':session_id, 'user':request.user.id}) form.fields['session'].widget = forms.HiddenInput() form.fields['user'].widget = forms.HiddenInput() except Session.DoesNotExist: raise Http404 return render(request, 'workouts/add_set.html', {'form':form, 'session':session})