def createShelter(): """ Creates a new shelter at which puppies can be given a temporary home. """ form = CreateShelterForm(request.form) if request.method == 'POST' and form.validate(): new_shelter = Shelter(name=form.name.data, address=form.address.data, city=form.city.data, state=form.state.data, zipCode=form.zipCode.data, website=form.website.data, maximum_capacity=form.maximum_capacity.data) try: session.add(new_shelter) session.commit() print "Successfully created a new Shelter" flash("Successfully added %s to the our shelters!" % new_shelter.name) return redirect(url_for('shelterList')) except: print "The entry was no added to the database, an unexpected error occurred." flash( "The shelter was not added to the database, an error occurred!" ) return redirect(url_for('shelterList')) else: return render_template('new_shelter.html', form=form)
def newShelter(): if request.method == 'POST': newItem = Shelter(name = request.form['name1'],address = request.form['name2'], city =request.form['name3'], state = request.form['name4'], zipCode = request.form['name5'], website = request.form['name6'], maximum_capacity = request.form['name7']) session.add(newItem) session.commit() return redirect(url_for('showShelters')) else: return render_template('newShelter.html')
def addShelter(): if request.method == 'POST': if (len(request.form["name"]) > 0): newShelter = Shelter(name=request.form["name"], address=request.form["address"], city=request.form["city"], state=request.form["city"], zipCode=request.form["zipCode"], website=request.form["webSite"]) session.add(newShelter) session.commit() flash("New shelter added!") return redirect(url_for('mainMenu')) else: flash("The Name field is empty!") return redirect(url_for('addShelter')) else: return render_template('new_shelter.html')
from puppies import Base, Shelter, Puppy from random import randint import datetime import random engine = create_engine('sqlite:///puppyshelter.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Add Shelters shelter1 = Shelter(name="Oakland Animal Services", address="1101 29th Ave", city="Oakland", state="California", zipCode="94601", website="oaklandanimalservices.org") session.add(shelter1) shelter2 = Shelter(name="San Francisco SPCA Mission Adoption Center", address="250 Florida St", city="San Francisco", state="California", zipCode="94103", website="sfspca.org") session.add(shelter2) shelter3 = Shelter(name="Wonder Dog Rescue", address="2926 16th Street", city="San Francisco", state="California", zipCode="94103", website="http://wonderdogrescue.org") session.add(shelter3) shelter4 = Shelter(name="Humane Society of Alameda", address="PO Box 1571", city="Alameda", state="California", zipCode="94501", website="hsalameda.org") session.add(shelter4) shelter5 = Shelter(name="Palo Alto Humane Society", address="1149 Chestnut St.", city="Menlo Park", state="California",
# Bind the engine to the metadata of the Base class so that the # declaratives can be accessed through a DBSession instance Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() session = DBSession() shelter_til = session.query(Shelter).filter_by(name="Hotel hundur").first() if shelter_til is None: shelter1 = Shelter(name="Hotel hundur") session.add(shelter1) session.commit() puppy1 = Puppy(name="Lassy", dob=date(2016,7,20),gender="Female", weight=40, shelter=shelter1) session.add(puppy1) session.commit() puppy2 = Puppy(name="Doppa", dob=date(2016,7,21),gender="Female", weight=25, shelter=shelter1) session.add(puppy2) session.commit() puppy3 = Puppy(name="Rex", dob=date(2016,7,22),gender="Male", weight=55, shelter=shelter1) session.add(puppy3) session.commit() puppy4 = Puppy(name="Poki", dob=date(2016,7,24),gender="Male", weight=60, shelter=shelter1)
def checkin_puppy(add_puppy_name, add_shelter_name): is_valid_add = 0 is_accepting = 0 add_shelter_id = 0 add_puppy_id = 0 print '' try: print ('Locating puppy named %r') % (add_puppy_name) print ('------------------------------------------------') print ('') puppy = session.query(Puppy.id, Puppy.name).\ filter(Puppy.name == add_puppy_name) if (puppy.count() != 0): add_puppy_id = puppy[0].id add_puppy_name = puppy[0].name if (add_puppy_id != 0): print ('Is %r already Checked-In to a shelter') % (add_puppy_name) print ('------------------------------------------------') print ('') found_puppy = session.query(ShelterPuppies.id).\ filter(ShelterPuppies.puppy_id == add_puppy_id) if (found_puppy.count() != 0): print ('Yes, %s is already Checked-In') % (add_puppy_name) add_puppy_id = 0 else: print ('No, %s is not checked into ' 'a shelter yet') % (add_puppy_name) print ('------------------------------------------------') print ('') else: print ('Could not locate puppy named %r') % (add_puppy_name) print ('------------------------------------------------') print ('') print ('Suggested list of five puppy names:') print ('------------------------------------------------') puppies_list = session.query(Puppy.name).\ filter(Puppy.name != add_puppy_name).\ order_by(Puppy.name).limit(5) print ('Suggested puppies count %s') % (puppies_list.count()) for row in puppies_list: print (row.name) print ('------------------------------------------------') print ('') except: raise if (add_puppy_id != 0): try: print ('Locating shelter named %r') % (add_shelter_name) print ('------------------------------------------------') print ('') shelter = session.query(Shelter.id, Shelter.name, Shelter.current_occupancy, Shelter.maximum_capacity).\ filter(Shelter.name == add_shelter_name) if (shelter.count() != 0): add_shelter_id = shelter[0].id add_shelter_name = shelter[0].name if (shelter[0].current_occupancy < shelter[0].maximum_capacity): is_accepting = 1 if (shelter.count() == 0 or is_accepting == 0): if (shelter.count() == 0): print ('Unable to locate %r') % (add_shelter_name) else: print ('%r is at max capacity') % (add_shelter_name) print ('------------------------------------------------') print ('') print ('Locating new shelter for this puppy') print ('------------------------------------------------') print ('') new_shelter = session.query(Shelter.id, Shelter.name).\ filter(Shelter.name != add_shelter_name and Shelter.current_occupancy < Shelter.maximum_capacity).\ order_by(Shelter.maximum_capacity) if (new_shelter is not None and new_shelter.count() != 0): add_shelter_id = new_shelter[0].id add_shelter_name = new_shelter[0].name is_valid_add = 1 print ('Found shelter %s') % (add_shelter_name) else: print ('All shelters are at max capacity') print ('Please add a new shelter for puppies') print ('------------------------------------------------') print ('') else: is_valid_add = 1 except: raise if (is_valid_add == 1): try: add = ShelterPuppies(id=None, shelter_id=add_shelter_id, puppy_id=add_puppy_id) session.add(add) session.commit() print ('%r is checked in at %s') % (add_puppy, add_shelter) print ('------------------------------------------------') print ('') update = Shelter(id=add_shelter_id, current_occupancy=current_occupancy+1) session.add(update) session.commit() print ('Updated current_occupancy of shelter %s') % (add_shelter) print ('------------------------------------------------') print ('') except: session.rollback() raise