Exemple #1
0
def new_puppy(shelter_id):
    """Function to return a page to add a new puppy.

    Args:
        shelter_id: ID of the shelter where the new puppy will live.
    """

    shelter = db_session.query(Shelter).filter_by(shelter_id=shelter_id).first()
    if not shelter:
        redirect(url_for('shelter_info', shelter_id=shelter.shelter_id))

    if request.method == 'POST':
        puppy = get_or_create(db_session, Puppy,
                              name=request.form['name'],
                              picture=request.form['picture'],
                              gender=request.form['gender'],
                              weight=request.form['weight'],
                              shelter_id=shelter_id)
        shelter.current_occupancy = shelter.current_occupancy + 1
        db_session.add(shelter)
        db_session.commit()
        flash("New puppy {} created!".format(puppy.name))
        return redirect(url_for('shelter_info', shelter_id=shelter_id))
    else:
        return render_template('new_puppy.html', shelter=shelter)
Exemple #2
0
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)