def add_workout(request): # check if the request is POST if request.method == 'POST': workout_form = WorkoutForm(request.POST) if workout_form.is_valid(): new_workout = Workout( exercise_date=workout_form.cleaned_data['date'], exercise_name=workout_form.cleaned_data['exercise'], sets=workout_form.cleaned_data['sets'], reps=workout_form.cleaned_data['reps'], weight=workout_form.cleaned_data['weight']) new_workout.save() return render(request, 'gym_app/add_workout.html', {'workout_form': workout_form, 'added': new_workout}) else: return render(request, 'gym_app/add_workout.html', {'workout_form': workout_form}) else: # if the request is a GET, display the form workout_form = WorkoutForm() date_form = DateForm() return render(request, 'gym_app/add_workout.html', {'workout_form': workout_form, 'date_form': date_form})
def test_workout_form_is_valid(self): """ Tests that the data posted on the form is valid """ form_data = {'date': self.date2.pk, 'exercise': self.exercise3.pk, 'sets': self.sets[1], 'reps': self.reps[1], 'weight': self.weight[1]} form = WorkoutForm(data=form_data) self.assertEqual(form.is_valid(), True)
def test_workout_form_is_invalid(self): """ Tests that the data posted on the form is invalid """ form_data = {'date': self.date2.pk, 'exercise': '', 'sets': '', 'reps': 'abc', 'weight': self.weight[1]} form = WorkoutForm(data=form_data) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, {'reps': [u'Enter a whole number.'], 'exercise': [u'This field is required.'], 'sets': [u'This field is required.']})