def edit (table, index):

	# retrieve form class from forms.py based on URL keyword
	if table == 'adopter':
		form = AdopterForm(request.form)
	elif table == 'puppy':
		shelters = session.query(Shelter).all()
		form = PuppyForm (request.form)
	elif table == 'shelter':
		form = ShelterForm (request.form)
	else:
		flash ('This isn\'t the /edit/ you are looking for! We don\'t recognize that URL. Please try editing a /puppy, /shelter or /adopter.')
		return redirect (url_for('homePage'))

	# edit database on form submission
	if request.method == 'POST' and form.validate():

		# your edit URL ends in /adopter/
		if table == 'adopter':
			mod_row = session.query(Adopter).filter_by(id=index)[0]
			mod_row.name = form.username.data
			mod_row.address = form.address.data
			mod_row.city = form.city.data
			mod_row.state = form.state.data
			mod_row.zipCode = form.zipCode.data
			mod_row.website = form.website.data
			mod_row.email = form.email.data
			mod_row.password = form.password.data
			flash ('Your Adopter profile has been updated!')

		# your edit URL ends in /puppy/
		elif table == 'puppy':
			# update the puppy name and shelter
			mod_row = session.query(Puppy).filter_by(id=index)[0]
			previous_shelter = mod_row.shelter_id
			mod_row.name = form.name.data
			mod_row.shelter_id = form.shelter_id.data
			# if chosen shelter is full, place puppy back in its current shelter
			if session.query(Shelter).filter_by(id=mod_row.shelter_id)[0].occupancy >= session.query(Shelter).filter_by(id=mod_row.shelter_id)[0].capacity:
				mod_row.shelter_id = previous_shelter
				flash("You selected a FULL shelter! Your puppy will stay in the current home shelter.")
			# update the profile entry for this puppy as well
			mod_profile = session.query(Profile).filter_by(id=index)[0]
			mod_profile.puppy_id = index
			mod_profile.breed = form.breed.data
			mod_profile.gender = form.gender.data
			mod_profile.weight = form.weight.data
			mod_profile.picture = form.picture.data
			session.add (mod_profile)
			flash ('Your puppy\'s info has been updated!')

		# your edit URL ends in /shelter/
		elif table == 'shelter':
			mod_row = session.query(Shelter).filter_by(id=index)[0]
			mod_row.name = form.name.data
			mod_row.address = form.address.data
			mod_row.zipCode = form.zipCode.data
			mod_row.city = form.city.data
			mod_row.state = form.state.data
			mod_row.website = form.website.data
			mod_row.capacity = form.capacity.data
			flash ('Your shelter\'s info has been updated!')

		else:
			flash ('Unable to add your info to FluppyBase (*whimper*). Don\'t leave yet! Please check that the form is completely filled out.')
			return redirect (url_for ('edit', table=table, index=index))

		# update row in the db
		session.add (mod_row)
		session.commit()
		# update shelter totals
		curate_shelter_capacity()
		return redirect (url_for('homePage'))

	# check which table user is updating, then build form for that table
	if table == 'shelter':
		this_shelter = session.query(Shelter).filter_by(id=index)[0]
		form.name.data = this_shelter.name
		form.address.data = this_shelter.address
		form.city.data = this_shelter.city
		form.state.data = this_shelter.state
		form.zipCode.data = this_shelter.zipCode
		form.website.data = this_shelter.website
		form.capacity.data = this_shelter.capacity
		return render_template('form.php', form=form, login=logged_in, content='')

	elif table == 'adopter':
		this_adopter = session.query(Adopter).filter_by(id=index)[0]
		form.username.data = this_adopter.name
		form.address.data = this_adopter.address
		form.city.data = this_adopter.city
		form.state.data = this_adopter.state
		form.zipCode.data = this_adopter.zipCode
		form.website.data = this_adopter.website
		form.email.data = this_adopter.email
		return render_template('form.php', form=form, login=logged_in, content='')

	elif table == 'puppy':
		# set the default fields
		this_puppy = session.query(Puppy).filter_by(id=index)[0]
		this_profile = session.query(Profile).filter_by(id=index)[0]
		form.name.data = this_puppy.name
		form.breed.data = this_profile.breed
		form.gender.data = this_profile.gender
		form.weight.data = this_profile.weight
		form.dateOfBirth.data = this_profile.dateOfBirth
		form.picture.data = this_profile.picture
		# set shelter_id radio buttons to a specific button (here the second shelter)
		form.shelter_id.data='%s'%this_puppy.shelter_id
		# render the form
		return render_template('form.php', form=form, login=logged_in, content='')

	# found no such table for this url var
	else:
		return redirect (url_for ('homePage'))

	# # edit database on form submission
	# if request.method=='POST':

	# 	# check if any input fields are empty - send user back to this page
	# 	for i in request.form:
	# 		if request.form[i] == '' or request.form[i] == None:
	# 			return redirect (url_for ('edit', table=table, index=index))

	# 	# add row to shelter table
	# 	if table == 'shelter':
	# 		mod_row = session.query(Shelter).filter_by(id=index)[0]
	# 		mod_row.name = request.form['name']
	# 		mod_row.address = request.form['address']
	# 		mod_row.city = request.form['city']
	# 		mod_row.zipCode = request.form['zipcode']
	# 		mod_row.state = request.form['state']
	# 		mod_row.website = request.form['website']
	# 		mod_row.capacity = request.form['capacity']
		
	# 	# add row to adopter table
	# 	elif table == 'adopter':
	# 		mod_row = session.query(Adopter).filter_by(id=index)[0]
	# 		mod_row.name = request.form['name']
	# 		mod_row.address = request.form['address']
	# 		mod_row.city = request.form['city']
	# 		mod_row.zipCode = request.form['zipcode']
	# 		mod_row.state = request.form['state']
	# 		mod_row.email = request.form['email']
	# 		mod_row.website = request.form['website']
	# 		mod_row.password = request.form['password']
		
	# 	# add row to puppy table
	# 	elif table == 'puppy':
	# 		mod_row = session.query(Puppy).filter_by(id=index)[0]
	# 		mod_row.name = request.form['name']
	# 		last_id = mod_row.shelter_id	# store current shelter in case new one is full
	# 		mod_row.shelter_id = request.form['shelterID']
	# 		mod_profile = session.query(Profile).filter_by(id=index)[0]
	# 		mod_profile.breed = request.form['breed']
	# 		mod_profile.gender = request.form['gender']
	# 		mod_profile.weight = request.form['weight']
	# 		mod_profile.picture = request.form['picture']
	# 		session.add (mod_profile)
	# 		# catch overflow puppies and unassign from full shelter
	# 		if session.query(Shelter).filter_by(id=mod_row.shelter_id)[0].occupancy >= session.query(Shelter).filter_by(id=mod_row.shelter_id)[0].capacity:
	# 			# shelter full - move to previous shelter
	# 			flash("Shelter full! Unable to move puppy - staying in current home shelter!")
	# 			mod_row.shelter_id = last_id				

	# 	# found no table corresponding to url var - return home instead
	# 	else:
	# 		return redirect(url_for('homePage'))

	# 	# edit row updated above in the db
	# 	session.add (mod_row)
	# 	session.commit()
	# 	# update shelter totals
	# 	curate_shelter_capacity()
	# 	return redirect (url_for('homePage'))

	# if method is GET display form for user input
	output = '<form action="" method="POST">'
	
	# check which table user is adding to, then display inputs for that table
	if table == 'shelter':
		# read and display data for this shelter
		s = session.query(Shelter).filter_by(id=index)[0]
		output += '<h2>Edit this shelter!</h2>'
		output += '<p>Name: <input type="text" name="name" value="%s"></p>\
		<p>Address: <input type="text" name="address" value="%s"></p>\
		<p>City: <input type="text" name="city" value="%s"></p>\
		<p>Zip: <input type="text" name="zipcode" value="%s"></p>\
		<p>State: <input type="text" name="state" value="%s"></p>\
		<p>Website: <input type="text" name="website" value="%s"></p>\
		<p>Capacity: <input type="text" name="capacity" value="%s"></p>'%(s.name,s.address,s.city,s.zipCode,s.state,s.website,s.capacity)

	# read and display data for this adopter
	elif table == 'adopter':
		a = session.query(Adopter).filter_by(id=index)[0]
		output += '<h2>Edit this adopter!</h2>'
		output += '<p>Name: <input type="text" name="name" value="%s"><br>\
				   <p>Address: <input type="text" name="address" value="%s"><br>\
				   <p>City: <input type="text" name="city" value="%s"><br>\
				   <p>State: <input type="text" name="state" value="%s"><br>\
				   <p>Zip: <input type="text" name="zipcode" value="%s"><br>\
				   <p>Email: <input type="text" name="email" value="%s"><br>\
				   <p>Website: <input type="text" name="website" value="%s"><br>\
				   <p>Password: <input type="text" name="password"><br>'%(a.name, a.address, a.city, a.state, a.zipCode, a.email, a.website)
	
	# read and display puppy and profile info
	elif table == 'puppy':
		p = session.query(Puppy).filter_by(id=index)[0]
		q = session.query(Profile).filter_by(id=index)[0]
		output += '<h2>Edit this puppy!</h2>'
		output += '<p>Name: <input type="text" name="name" value="%s"></p>\
		<p>Breed: <input type="text" name="breed" value="%s"></p>'%(p.name,q.breed)
		# set the radio button for current profile gender as selected
		if (q.gender == 'male'):
			output += '<p>Gender: <input type="radio" name="gender" value="male" checked> M <input type="radio" name="gender" value="female"> F</p>'
		else:
			output += '<p>Gender: <input type="radio" name="gender" value="male"> M <input type="radio" name="gender" value="female" checked> F</p>'
		# read and display profile
		output += '<p>Weight: <input type="text" name="weight" value="%s"></p>\
		<p>Date of birth: <input type="text" name="dateOfBirth" value="%s"></p>\
		<p>Picture: <input type="text" name="picture" value="%s"></p>\
		<p>Choose a Home Shelter for this puppy: <br>'%(q.weight,q.dateOfBirth,q.picture)
		# select from existing shelters
		shelters = session.query(Shelter).all()
		for s in shelters:
			if (s.id == p.shelter_id):
				output += '<input type="radio" name="shelterID" value="%s" checked>'%s.id
			else:
				output += '<input type="radio" name="shelterID" value="%s">'%s.id
			output += ' &nbsp;%s<br>'%s.name
		output += '</p>'

	# found no table for this url variable - return home instead
	else:
		return redirect (url_for('homePage'))

	# end form and page - submit to reach POST branch above
	output += '<p><input type="submit" value="Modify"></p></form>'

	return render_template('main.php', login=logged_in, content=output)
def add (table):

	# retrieve form class for this URL keyword
	if table == 'adopter':
		form = AdopterForm(request.form)
	elif table == 'puppy':
		shelters = session.query(Shelter).all()
		form = PuppyForm (request.form)
	elif table == 'shelter':
		form = ShelterForm (request.form)
	else:
		flash ('This isn\'t the /add/ you are looking for! We don\'t recognize that URL. Please try adding a /puppy, /shelter or /adopter.')
		return redirect (url_for('homePage'))

	# add to database on form submission
	if request.method == 'POST' and form.validate():

		if table == 'adopter':
			new_row = Adopter (name=form.username.data, address=form.address.data, city=form.city.data, state=form.state.data, zipCode=form.zipCode.data, website=form.website.data, email=form.email.data, password=form.password.data)
			flash ('Thank you for creating an Adopter profile!')

		elif table == 'puppy':
			# try checking puppy in to shelter if shelter not full
			new_row = Puppy (name=form.name.data, shelter_id=form.shelter_id.data)
			if session.query(Shelter).filter_by(id=new_row.shelter_id)[0].occupancy >= session.query(Shelter).filter_by(id=new_row.shelter_id)[0].capacity:
				# HOMELESS! - we can't place the puppy!
				new_row.shelter_id = None
				flash("Unable to place puppy - currently has no home shelter!")
			# create profile entry for this puppy too
			new_profile = Profile (puppy_id=new_row.id, breed=form.breed.data, gender=form.gender.data, weight=form.weight.data, picture=form.picture.data)
			session.add (new_profile)
			flash ('Thank you for adding a puppy!')
			app.logger.info ('Added puppy %s to FluppyBase'%(new_row.name))

		elif table == 'shelter':
			new_row = Shelter (name=form.name.data, address=form.address.data, zipCode=form.zipCode.data, city=form.city.data, state=form.state.data, website=form.website.data, capacity=form.capacity.data)
			flash ('Thank you for adding a shelter!')

		else:
			flash ('Unable to add your info to FluppyBase (*whimper*). Don\'t leave yet! Please check that the form is completely filled out.')
			return redirect (url_for ('add', table=table))

		# add created row to db
		session.add (new_row)
		session.commit()
		# update shelter totals
		curate_shelter_capacity()
		return redirect (url_for('homePage'))

	# elif request.method=='POST':

	# 	# check if any input fields are empty - send user back to this page
	# 	for i in request.form:
	# 		if request.form[i] == '' or request.form[i] == None:
	# 			return redirect (url_for ('add', table=table))

	# 	# add row to shelter table
	# 	if table == 'shelter':
	# 		new_row = Shelter (name=request.form['name'], address=request.form['address'], city=request.form['city'], zipCode=request.form['zipcode'], state=request.form['state'], website=request.form['website'], capacity=request.form['capacity'])
		
	# 	# add row to adopter table
	# 	elif table == 'adopter':
	# 		new_row = Adopter (name=request.form['name'], address=request.form['address'], city=request.form['city'], zipCode=request.form['zipcode'], state=request.form['state'], website=request.form['website'], email=request.form['email'], password=request.form['pwd'])
		
	# 	# add row to puppy table
	# 	elif table == 'puppy':
	# 		# try checking puppy in to shelter if shelter not full
	# 		new_row = Puppy (name=request.form['name'], shelter_id=request.form['shelterID'])
	# 		if session.query(Shelter).filter_by(id=new_row.shelter_id)[0].occupancy >= session.query(Shelter).filter_by(id=new_row.shelter_id)[0].capacity:
	# 			# HOMELESS! - we can't place the puppy!
	# 			new_row.shelter_id = None
	# 			flash("Unable to place puppy - currently has no home shelter!")
	# 		# create profile entry for this puppy too
	# 		new_profile = Profile (puppy_id=new_row.id, breed=request.form['breed'], gender=request.form['gender'], weight=request.form['weight'], picture=request.form['picture'])
	# 		session.add (new_profile)
		
	# 	# found no table for this url variable
	# 	else:
	# 		return redirect (url_for ('homePage'))

	# 	# add whichever row was created above to the db
	# 	session.add (new_row)
	# 	session.commit()
	# 	# update shelter totals
	# 	curate_shelter_capacity()
	# 	# go home
	# 	return redirect (url_for('homePage'))

	# if method is GET display form for user input
	#output = '<form action="" method="POST">'
	
	# check which table user is adding to, then build form for that table
	if table == 'shelter':
		return render_template('form.php', form=form, login=logged_in, content='')
		# output += '<h2>Add one shelter!</h2>'
		# output += '<p>Name: <input type="text" name="name"></p>\
		# <p>Address: <input type="text" name="address"></p>\
		# <p>City: <input type="text" name="city"></p>\
		# <p>Zip: <input type="text" name="zipcode"></p>\
		# <p>State: <input type="text" name="state"></p>\
		# <p>Website: <input type="text" name="website"></p>\
		# <p>Capacity: <input type="text" name="capacity"></p>'

	elif table == 'adopter':
		return render_template('form.php', form=form, login=logged_in, content='')
		# output += '<h2>Become an adopter!</h2>'
		# output += '<p>User: <input type="text" name="name"></p>'
		# output += '<p>Address: <input type="text" name="address"></p>'
		# output += '<p>City: <input type="text" name="city"></p>'
		# output += '<p>State: <input type="text" name="state"></p>'
		# output += '<p>Zip: <input type="text" name="zipcode"></p>'
		# output += '<p>Website: <input type="text" name="website"></p>'
		# output += '<p>Email: <input type="text" name="email"></p>'
		# output += '<p>Password: <input type="text" name="pwd"></p>'

	elif table == 'puppy':
		# set shelter_id radio buttons to a specific button (here the second shelter)
		form.shelter_id.data='%s'%session.query(Shelter).order_by(asc(Shelter.id))[1].id
		# render the form
		return render_template('form.php', form=form, login=logged_in, content='')
		# output += '<h2>Add one puppy!</h2>'
		# output += '<p>Name: <input type="text" name="name"></p>\
		# <p>Breed: <input type="text" name="breed"></p>\
		# <p>Gender: <input type="radio" name="gender" value="male"> M <input type="radio" name="gender" value="female"> F</p>\
		# <p>Weight: <input type="text" name="weight"></p>\
		# <p>Date of birth: <input type="text" name="dateOfBirth"></p>\
		# <p>Picture: <input type="text" name="picture"></p>\
		# <p>Choose a Home Shelter for this puppy: <br>'
		# # radio button select from existing shelters
		# shelters = session.query(Shelter).all()
		# for s in shelters:
		# 	output += '<input type="radio" name="shelterID" value="%s"> %s<br>'%(s.id, s.name)
		# output += '</p>'

	# found no such table for this url var
	else:
		return redirect (url_for ('homePage'))