def edit(id):
	exercise = Exercise.query.get_or_404(id)
	bodyparts=[bodypart.id for bodypart in exercise.bodyparts]
	form = ExerciseForm(bodyparts=bodyparts)
	form.set_choices()
	form.description.data = exercise.description
	return render_template('exercises/edit.html', form=form, exercise=exercise)
def index():
	exercises = Exercise.query.order_by(Exercise.name).all()
	form = ExerciseForm(request.form)
	form.set_choices()
	if request.method == 'POST':
		if form.validate():
			new_exercise = Exercise(form.name.data, form.description.data)
			for bodypart in form.bodyparts.data:
				new_exercise.bodyparts.append(BodyPart.query.get_or_404(bodypart))
			db.session.add(new_exercise)
			db.session.commit()
			return redirect(url_for('exercises.index'))
		else:
			return render_template('exercises/new.html', form=form)
	return render_template('exercises/index.html', exercises=exercises)
def show(id):
	exercise = Exercise.query.get_or_404(id)
	form = ExerciseForm(request.form)
	form.set_choices()
	if request.method == b"PATCH":
		if form.validate():
			exercise.name = form.name.data
			exercise.description = form.description.data
			exercise.bodyparts = []
			for bodypart in form.bodyparts.data:
				exercise.bodyparts.append(BodyPart.query.get_or_404(bodypart))
			db.session.add(exercise)
			db.session.commit()
			return redirect(url_for('exercises.index'))
		else:
			return render_template('exercises/edit.html', form=form, exercise=exercise)
	return render_template('exercises/show.html', exercise = exercise)
def new():
	form = ExerciseForm(request.form)
	form.set_choices()
	return render_template('exercises/new.html', form=form)