Esempio n. 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)
Esempio n. 2
0
def populate_database(session):
    """Function to create Shelter and Puppy objects."""

    print "Populating database with Shelters and Puppies..."

    # Create shelters.
    shelter_1 = get_or_create(session, Shelter,
                              name="Oakland Animal Services",
                              address="1101 29th Ave",
                              city="Oakland",
                              state="California",
                              zip_code="94601",
                              website="http://oaklandanimalservices.org")

    shelter_2 = get_or_create(session, Shelter,
                              name="San Francisco SPCA Mission "\
                              "Adoption Center",
                              address="250 Florida St",
                              city="San Francisco",
                              state="California",
                              zip_code="94103",
                              website="http://sfspca.org")

    shelter_3 = get_or_create(session, Shelter,
                              name="Wonder Dog Rescue",
                              address="2926 16th Street",
                              city="San Francisco",
                              state="California",
                              zip_code="94103",
                              website="http://wonderdogrescue.org")

    shelter_4 = get_or_create(session, Shelter,
                              name="Humane Society of Alameda",
                              address="PO Box 1571",
                              city="Alameda",
                              state="California",
                              zip_code="94501",
                              website="http://hsalameda.org")

    shelter_5 = get_or_create(session, Shelter,
                              name="Palo Alto Humane Society",
                              address="1149 Chestnut St.",
                              city="Menlo Park",
                              state="California",
                              zip_code="94025",
                              website="http://paloaltohumane.org")

    # Create male and female puppies. For each, assign one of the
    # available shelters that has capacity for more puppies.
    for i, rand_name in enumerate(MALE_NAMES):
        shelters = get_available_shelters(session)
        rand_shelter_id = randint(0, len(shelters) - 1)
        shelter_for_puppy = shelters[rand_shelter_id]
        new_puppy = get_or_create(session, Puppy,
                                  name=rand_name, gender="Male",
                                  date_of_birth=create_random_age(),
                                  picture=random.choice(PUPPY_IMAGES),
                                  shelter_id=rand_shelter_id,
                                  shelter=shelter_for_puppy,
                                  weight=create_random_weight())
        shelter_for_puppy.current_occupancy = shelter_for_puppy.current_occupancy + 1
        session.add(shelter_for_puppy)
        session.commit()

    for i, rand_name in enumerate(FEMALE_NAMES):
        shelters = get_available_shelters(session)
        rand_shelter_id = randint(0, len(shelters) - 1)
        shelter_for_puppy = shelters[rand_shelter_id]
        new_puppy = get_or_create(session, Puppy,
                                  name=rand_name, gender="Female",
                                  date_of_birth=create_random_age(),
                                  picture=random.choice(PUPPY_IMAGES),
                                  shelter_id=rand_shelter_id,
                                  shelter=shelter_for_puppy,
                                  weight=create_random_weight())
        shelter_for_puppy.current_occupancy = shelter_for_puppy.current_occupancy + 1
        session.add(shelter_for_puppy)
        session.commit()

    print "Database successfully populated!"