def adopt_puppy(puppy_id): """Function to return a page to adopt a puppy. Args: puppy_id: ID of the puppy to adopt. """ puppy = db_session.query(Puppy)\ .filter_by(puppy_id=puppy_id).first() if not puppy: return render_template('404.html', headline_text='Puppy Not Found!') if request.method == 'POST': # Remove shelter_id and shelter from Puppy object, and add owner. # Update Shelter's occupancy. Commit both objects to DB session. shelter = db_session.query(Shelter)\ .filter_by(shelter_id=puppy.shelter_id).first() if not shelter: return render_template('404.html', headline_text='Shelter Not Found!') puppy.owner = request.form['name'] puppy.shelter_id = '' puppy.shelter = None shelter.current_occupancy = shelter.current_occupancy - 1 db_session.add_all([puppy, shelter]) db_session.commit() flash("{} has been adopted!".format(puppy.name)) return redirect(url_for('puppy_info', puppy_id=puppy_id)) else: return render_template('adopt_puppy.html', puppy=puppy, shelter=puppy.shelter)
def edit_puppy(puppy_id): """Function to return a page to edit a puppy. Args: puppy_id: ID of the puppy to edit. """ puppy = db_session.query(Puppy)\ .filter_by(puppy_id=puppy_id).first() if not puppy: return render_template('404.html', headline_text='Puppy Not Found!') # Get all available shelters. This is used in the template for # populating the dropdown list of available shelters that have # capacity available. shelters = db_session.query(Shelter)\ .filter(Shelter.current_occupancy < Shelter.maximum_capacity).all() if request.method == 'POST': puppy.name = request.form['name'] puppy.picture = request.form['picture'] puppy.gender = request.form['gender'] puppy.weight = request.form['weight'] # The same form is used, but depending on the adoption status, # it will sometimes have shelter_id info, and sometimes not. if 'original_shelter_id' in request.form: original_shelter_id = request.form['original_shelter_id'] shelter_id = request.form['shelter_id'] # If form shelter id doesn't match puppy's original shelter id, # update both the shelter's and original shelter's occupancy. if shelter_id != original_shelter_id: puppy.shelter_id = shelter_id shelter = db_session.query(Shelter)\ .filter_by(shelter_id=shelter_id).first() original_shelter = db_session.query(Shelter)\ .filter_by(shelter_id=original_shelter_id).first() if not shelter or not original_shelter: return render_template('404.html', headline_text='Shelter Not Found!') shelter.current_occupancy = shelter.current_occupancy + 1 original_shelter.current_occupancy = original_shelter.current_occupancy - 1 db_session.add_all([shelter, original_shelter]) db_session.commit() else: puppy.owner = request.form['owner'] db_session.add(puppy) db_session.commit() flash("{} updated!".format(puppy.name)) return redirect(url_for('puppy_info', puppy_id=puppy_id)) else: return render_template('edit_puppy.html', puppy=puppy, shelter=puppy.shelter, shelters=shelters)