예제 #1
0
def edit_puppy(shelter_id, puppy_id):
    """page to edit a puppy's basic information."""
    if request.method == "POST":
        edited_name = request.form['edited_name']
        old_name = session.query(Puppy).filter_by(id = puppy_id).first().name

        result = session.execute("""
                UPDATE puppy
                SET name=:edited_name
                WHERE id=:edited_puppy_id;
            """,
            {"edited_name": edited_name,
            "edited_puppy_id": puppy_id}
        )
        session.commit()
        flash( "item '" +  old_name + "' edited to '" + edited_name + "'. Jawohl!")
        return redirect(url_for("show_puppies", shelter_id=shelter_id))

    else:
        output = render_template('page_head.html', title = "The Menu Manager", form = 0)
        puppy = session.query(Puppy).filter_by(id = puppy_id).first()
        logging.info( "shelter_id is: ", shelter_id )
        if shelter_id == "n":
            shelter = session.query(Shelter).filter_by(id = puppy.shelter_id).first()
        else:
            shelter = session.query(Shelter).filter_by(id = shelter_id).first()
        output += render_template('edit_puppy.html',
                                  shelter = shelter,
                                  puppy = puppy )
        return output
예제 #2
0
def adopt_puppy(adopter_id):
    """page to adopt puppies, accessed from the Adopter directory."""

    if request.method == "POST":
        adopt_this_puppy_id = request.form["adopt_this_puppy_id"]
        adopt_this_puppy_name = session.query(Puppy).\
                                filter_by(id = adopt_this_puppy_id).first().name
        result = session.execute("""
            UPDATE puppy
            SET adopter_id = :adopter_id,
                shelter_id = NULL
            WHERE id = :adopt_this_puppy_id
        """,
        { "adopter_id": adopter_id,
          "adopt_this_puppy_id": adopt_this_puppy_id }
        )
        session.commit()
        flash("Puppy '" + adopt_this_puppy_name + "' adopted! Congrats!")
        return redirect(url_for("adopt_puppy", adopter_id = adopter_id))

    if request.method == "GET":
        output = render_template('page_head.html', title = "Adopt a Puppy!!!", form = 0)
        adopter = session.query(Adopter).filter_by(id = adopter_id).first()
        puppies = session.execute("""
                SELECT *
                FROM puppy
                WHERE adopter_id ISNULL
            """)
        logging.debug( "puppies length is: ", len(puppies) )
        flash("Please select a puppy to adopt.")
        output += render_template( 'adopt_puppy.html',
                                   adopter=adopter,
                                   puppies=puppies )
        return output
예제 #3
0
def show_puppies_by_adopter(adopter_id):
    adopter = session.query(Adopter).filter_by(id = adopter_id).first()
    puppies = session.query(Puppy).filter_by(adopter_id = adopter_id)

    output = render_template( 'page_head.html',
                              title = "The Puppies of "+adopter.name,
                              form = 0 )
    output += render_template( 'show_puppies_by_adopter.html',
                               adopter=adopter,
                               puppies=puppies )
    return output
예제 #4
0
def show_puppies(shelter_id):
    shelter = session.query(Shelter).filter_by(id = shelter_id).first()
    puppies = session.query(Puppy).filter_by(shelter_id = shelter_id)
    # pdb.set_trace()
    output = render_template( 'page_head.html',
                              title = "The Puppies of "+shelter.name,
                              form = 0 )
    output += render_template( 'show_puppies.html',
                               shelter=shelter,
                               puppies=puppies )
    return output
예제 #5
0
def new_puppy(shelter_id):
    """page to create a new menu item."""
    form = PuppyForm(request.form)
    shelter = session.query(Shelter).filter_by(id = shelter_id).first()
    pdb.set_trace()
    if request.method == "POST" and form.validate():
        if (shelter.maximum_capacity - shelter.current_occupancy) <= 0:
            flash( """
                '{shelter_name}' is full, and the puppy
                '{new_name}' couldn't be added, sorry :(
                """.format(shelter_name=shelter.name, new_name=new_name))
            return redirect(url_for("show_puppies", shelter_id=shelter.id))
        else:
            new_puppy = Puppy()
            form.populate_obj(new_puppy)
            session.add(new_puppy)
            session.commit()
            flash( "new puppy '" + new_puppy.name + "' added!")
            return redirect(url_for("show_puppies", shelter_id=shelter.id))
    else:
        output = render_template('page_head.html', title = "Add a New Puppy! :D", form = 0)
        output += render_template( 'new_puppy.html',
                                   shelter = shelter,
                                   form = form )
        return output
예제 #6
0
def show_adopters():
    output = render_template( 'page_head.html',
                              title = "California State Puppy Adopter Directory",
                              form = 0 )
    adopters = session.query(Adopter).all()
    output += render_template('show_adopters.html', adopters=adopters)
    return output
예제 #7
0
def delete_puppy(shelter_id, puppy_id):
    """page to delete a puppy."""
    if request.method == "POST":
        delete_this_puppy = session.query(Puppy).filter_by(id = puppy_id).first()
        session.delete(delete_this_puppy)
        session.commit()
        flash( "item '" + delete_this_puppy.name + "' deleted. Auf Wiedersehen!")
        return redirect(url_for("show_puppies", shelter_id=shelter_id))

    else:
        output = render_template('page_head.html', title = "The Menu Manager", form = 0)
        puppy = session.query(Puppy).filter_by(id = puppy_id).first()
        if shelter_id == "n":
            shelter = session.query(Shelter).filter_by(id = puppy.shelter_id).first()
        else:
            shelter = session.query(Shelter).filter_by(id = shelter_id).first()
        output += render_template( 'delete_puppy.html',
                                   puppy = puppy,
                                   shelter = shelter )
        return output
예제 #8
0
def delete_shelter(shelter_id):
    """page to delete a puppy."""
    shelter = session.query(Shelter).filter_by(id=shelter_id).first()
    form = ShelterForm(request.form, shelter)
    if request.method == "POST":
        session.delete(shelter)
        session.commit()
        flash("Shelter '" + shelter.name + "' deleted. Auf Wiedersehen!")
        return redirect(url_for("show_shelters"))

    else:
        output = render_template("page_head.html", title="Delete a Shelter", form=form)
        output += render_template("delete_shelter.html", form=form)
        return output
예제 #9
0
def edit_shelter(shelter_id):
    """page to edit a shelter's basic information."""
    shelter = session.query(Shelter).filter_by(id=shelter_id).first()
    form = ShelterForm(request.form, shelter)
    if request.method == "POST":
        old_name = shelter.name
        form.populate_obj(shelter)
        session.add(shelter)
        session.commit()
        flash("Shelter '" + old_name + "' renamed to '" + shelter.name + "'. Jawohl!")
        return redirect(url_for("show_shelters"))

    else:
        output = render_template("page_head.html", title="Rename Your Shelter", form=form)
        output += render_template("edit_shelter.html", form=form)
        return output
예제 #10
0
def delete_adopter(adopter_id):
    """page to delete a puppy."""
    adopter = session.query(Adopter).filter_by(id=adopter_id).first()
    form = AdopterForm( request.form, adopter )
    if request.method == "POST":
        session.delete(adopter)
        session.commit()
        flash( "Adopter '" + adopter.name + "' deleted. Auf Wiedersehen!")
        return redirect(url_for("show_adopters"))

    else:
        output = render_template( 'page_head.html',
                                  title = "Delete an Adopter",
                                  form = form )
        output += render_template( 'delete_adopter.html',
                                   form = form )
        return output
예제 #11
0
def edit_adopter(adopter_id):
    """page to edit a adopter's basic information."""
    adopter = session.query(Adopter).filter_by(id=adopter_id).first()
    form = AdopterForm( request.form, adopter )
    if request.method == "POST":
        old_name = adopter.name
        form.populate_obj(adopter)
        session.add(adopter)
        session.commit()
        flash( "Adopter '"+old_name+"' renamed to '"+adopter.name+"'. Jawohl!")
        return redirect(url_for("show_adopters"))

    else:
        output = render_template( 'page_head.html',
                                  title = "Edit an Adopter",
                                  form = form )
        output += render_template( 'edit_adopter.html',
                                   form = form )
        return output
예제 #12
0
def show_shelters():
    output = render_template("page_head.html", title="California State Puppy Shelters", form=0)
    shelters = session.query(Shelter).all()
    output += render_template("show_shelters.html", shelters=shelters)
    return output
예제 #13
0
def show_puppies_JSON(shelter_id):
    shelter = session.query(Shelter).filter_by(id = shelter_id).one()
    puppies = session.query(Puppy).filter_by(shelter_id = shelter_id).all()
    return jsonify(Puppies = [puppy.serialize for puppy in puppies])
예제 #14
0
def puppyJSON(shelter_id, puppy_id):
    shelter = session.query(Shelter).filter_by(id = shelter_id).one()
    puppy = session.query(Puppy).filter_by(shelter_id = shelter_id).filter_by(id = puppy_id).one()
    return jsonify(Puppy = puppy.serialize)