def createPuppy(shelter_id): if request.method == "GET": return render_template("create_puppy.html", shelter_id=shelter_id) else: newPup = Puppy() newPup.shelter_id = shelter_id mapRequestToPuppy(request, newPup) session.add(newPup) session.commit() return redirect(url_for("shelter", shelter_id=shelter_id))
def newPuppy(shelter): """Add new puppy to the shelter""" if request.method == 'POST': if request.form['name'] and request.form['sex']: name = request.form['name'] sex = request.form['sex'] dateOfBirth = datetime.strptime(request.form['dob'], "%Y-%m-%d") picture = request.form['picture'] newPuppy = Puppy(name=name, sex=sex, dateOfBirth=dateOfBirth, picture=picture, shelter_id=shelter.id) session.add(newPuppy) session.commit() flash("%s is now looking for a forever home." % name) return redirect(url_for('listPuppy', shelter_id=shelter.id)) else: if not request.form['name']: flash("Please name the puppy \ so we'll know who's been a good dog.") if not request.form['sex']: flash("Not trying to assume their gender, \ but the sex of the puppy would really help.") return render_template("puppy-new.html", shelter=shelter) else: return render_template("puppy-new.html", shelter=shelter)
def item_new(list_type): if request.method == 'GET': puppies = session.query(Puppy).all() shelters = session.query(Shelter).all() owners = session.query(Owner).all() if list_type =='puppies': template = 'puppies_add.html' form = puppyForm() form.shelter.choices = [ (g.id, g.name) for g in session.query(Shelter).all() ] elif list_type =='shelters': template = 'shelters_add.html' form = shelterForm() elif list_type == 'owners': template = 'owners_add.html' form = ownerForm() else: template = 'error404.html' form = puppyForm() list_type = 'puppies' return render_template( template, list_type=list_type, puppies = puppies, shelters = shelters, owners = owners, form = form) #handles POST elif request.method == 'POST': if list_type == 'puppies': form = puppyForm(request.form) form.shelter.choices = [ (g.id, g.name) for g in session.query(Shelter).all() ] if form.validate(): thisshelter = session.query(Shelter).filter(Shelter.id==form.shelter.data).first() newItem = Puppy(name=form.name.data, dateOfBirth=form.dateOfBirth.data, gender=form.gender.data, weight=form.weight.data, picture=form.picture.data, shelter_id=thisshelter.id ) else: for fieldName, errorMessages in form.errors.iteritems(): for err in errorMessages: flash('Error in '+fieldName+' field. '+err) return redirect(url_for('item_new', list_type = list_type)) elif list_type =='shelters': form = shelterForm(request.form) if form.validate(): newItem = 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) else: for fieldName, errorMessages in form.errors.iteritems(): for err in errorMessages: flash('Error in '+fieldName+' field. '+err) return redirect(url_for('item_new', list_type = list_type)) elif list_type == 'owners': form = ownerForm(request.form) if form.validate(): newItem = Owner(name=form.name.data, surname=form.surname.data, gender =form.gender.data, age = form.age.data) else: for fieldName, errorMessages in form.errors.iteritems(): for err in errorMessages: flash('Error in '+fieldName+' field. '+err) return redirect(url_for('item_new', list_type = list_type)) session.add(newItem) session.commit() #app.logger.info('Added %s %s to the database.', list_type, form.name.data) flash("Succesfully added "+form.name.data+"!") return redirect(url_for('list_view', list_type=list_type))
def newPuppy(shelter_id): form = NewPuppyForm(request.form) if request.method == 'POST' and form.validate(): shelter = session.query(Shelter).filter_by(id=shelter_id).one() if shelter.current_occupancy >= shelter.maximum_capacity: flash('Shelter already at maximum capacity. Please try \ another one') # reload page and display the flash messaage return render_template( 'newpuppy.html', form=form, shelter_id=shelter_id ) shelter.current_occupancy += 1 session.add(shelter) puppy = Puppy() puppy.name = form.name.data puppy.gender = form.gender.data puppy.weight = form.weight.data puppy.shelter_id = shelter_id puppy.dateOfBirth = datetime.strptime(form.dob.data, '%Y-%m-%d').date() session.add(puppy) session.commit() flash('new puppy added', 'warn') # Go back to shelter page return redirect(url_for('shelterPage', shelter_id=shelter_id)) else: # Show form return render_template( 'newpuppy.html', form=form, shelter_id=shelter_id )
def addNewPuppy(shelter_id): shelter = session.query(Shelter).filter_by(id=shelter_id).one() if request.method == 'POST': newPuppy = Puppy(name=request.form['name'], weight=float(request.form['weight']), age=int(request.form['age']), breed=request.form['breed'], gender=request.form['gender'], shelter=shelter) session.add(newPuppy) session.commit() return redirect(url_for('allPuppies', shelter_id=shelter.id)) return render_template('addpuppy.html', shelter=shelter)
def CreateRandomAge(): today = datetime.date.today() days_old = randint(0, 540) birthday = today - datetime.timedelta(days=days_old) return birthday #This method will create a random weight between 1.0-40.0 pounds (or whatever unit of measure you prefer) def CreateRandomWeight(): return random.uniform(1.0, 40.0) for i, x in enumerate(male_names): new_puppy = Puppy(name=x, gender="male", dateOfBirth=CreateRandomAge(), picture=random.choice(puppy_images), shelter_id=randint(1, 5), weight=CreateRandomWeight()) session.add(new_puppy) session.commit() for i, x in enumerate(female_names): new_puppy = Puppy(name=x, gender="female", dateOfBirth=CreateRandomAge(), picture=random.choice(puppy_images), shelter_id=randint(1, 5), weight=CreateRandomWeight()) session.add(new_puppy) session.commit()
#This method will make a random age for each puppy between 0-18 months(approx.) old from the day the algorithm was run. def CreateRandomAge(): today = datetime.date.today() days_old = randint(0, 540) birthday = today - datetime.timedelta(days=days_old) return birthday #This method will create a random weight between 1.0-40.0 pounds (or whatever unit of measure you prefer) def CreateRandomWeight(): return random.uniform(1.0, 40.0) for i, x in enumerate(male_names): new_puppy = Puppy(name=x, gender="male", dateOfBirth=CreateRandomAge(), shelter_id=randint(1, 5), weight=CreateRandomWeight()) session.add(new_puppy) session.commit() for i, x in enumerate(female_names): new_puppy = Puppy(name=x, gender="female", dateOfBirth=CreateRandomAge(), shelter_id=randint(1, 5), weight=CreateRandomWeight()) session.add(new_puppy) session.commit()
"http://pixabay.com/get/7dcd78e779f8110ca876/1433170979/dog-710013_1280.jpg?direct", "http://pixabay.com/get/31d494632fa1c64a7225/1433171005/dog-668940_1280.jpg?direct" ] def CreateRandomAge(): """Create random age between 0-18 months""" today = datetime.date.today() days_old = randint(0, 540) birthday = today - datetime.timedelta(days=days_old) return birthday for i, x in enumerate(male_names): new_puppy = Puppy(name=x, sex="male", dateOfBirth=CreateRandomAge(), picture=random.choice(puppy_images), shelter_id=randint(1, 5)) session.add(new_puppy) session.commit() for i, x in enumerate(female_names): new_puppy = Puppy(name=x, sex="female", dateOfBirth=CreateRandomAge(), picture=random.choice(puppy_images), shelter_id=randint(1, 5)) session.add(new_puppy) session.commit()
def AddABunchOfFakePuppies(): engine = create_engine('sqlite:///puppyweb.db') Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() #Add Shelters shelter1 = Shelter(name = "San Juan Animal Services", city = "San Juan") session.add(shelter1) shelter2 = Shelter(name = "San Juan Adoption Center", city="San Juan") session.add(shelter2) shelter3 = Shelter(name = "Carolina Dog Rescue", city = "Carolina") session.add(shelter3) shelter4 = Shelter(name = "Humane Society of Luqillo",city = "Luquillo") session.add(shelter4) shelter5 = Shelter(name = "Bayamon Humane Society" ,city = "Bayamon") session.add(shelter5) #Add Puppies gender = ["male", "female"] male_names = ["Bailey", "Max", "Charlie", "Buddy","Rocky","Jake", "Jack", "Toby", "Cody", "Buster", "Duke", "Cooper", "Riley", "Harley", "Bear", "Tucker", "Murphy", "Lucky", "Oliver", "Sam", "Oscar", "Teddy", "Winston", "Sammy", "Rusty", "Shadow", "Gizmo", "Bentley", "Zeus", "Jackson", "Baxter", "Bandit", "Gus", "Samson", "Milo", "Rudy", "Louie", "Hunter", "Casey", "Rocco", "Sparky", "Joey", "Bruno", "Beau", "Dakota", "Maximus", "Romeo", "Boomer", "Luke", "Henry"] female_names = ['Bella', 'Lucy', 'Molly', 'Daisy', 'Maggie', 'Sophie', 'Sadie', 'Chloe', 'Bailey', 'Lola', 'Zoe', 'Abby', 'Ginger', 'Roxy', 'Gracie', 'Coco', 'Sasha', 'Lily', 'Angel', 'Princess', 'Emma', 'Annie', 'Rosie', 'Ruby', 'Lady', 'Missy', 'Lilly', 'Mia', 'Katie', 'Zoey', 'Madison', 'Stella', 'Penny', 'Belle', 'Casey', 'Samantha', 'Holly', 'Lexi', 'Lulu', 'Brandy', 'Jasmine', 'Shelby', 'Sandy', 'Roxie', 'Pepper', 'Heidi', 'Luna', 'Dixie', 'Honey', 'Dakota'] names = {"male": male_names, "female":female_names} puppy_images = ["images/Puppy1.PNG", "images/Puppy2.PNG", "images/Puppy3.PNG", "images/Puppy4.PNG", "images/Puppy5.PNG", "images/Puppy6.PNG", "images/Puppy7.PNG"] breeds = ["Mutt", "Corgi", "Terrier", "Cockerspaniel", "Pommeranian", "Weiner Dog", "Collie"] descriptions = ["Well-behaved, friendly", "Nice to people, aggressive towards other dogs", "Very energetic"] for pupImage in puppy_images: newPup = Puppy() newPup.gender = random.choice(gender) newPup.name = random.choice(names[newPup.gender]) newPup.dateOfBirth = CreateRandomAge() newPup.breed = random.choice(breeds) newPup.description = random.choice(descriptions) newPup.shelter_id=randint(1,5) newPup.weight= CreateRandomWeight() newPup.pictureURL = pupImage session.add(newPup) session.commit()
def CreateRandomWeight(): """Create a random weight for a puppy. This method will create a random weight between 1.0-40.0 pounds (or whatever unit of measure you prefer). Returns: float: Random weight of puppy. """ return random.uniform(1.0, 40.0) for i, male_name in enumerate(male_names): new_puppy = Puppy(name=male_name, gender="male", date_of_birth=CreateRandomAge(), shelter_id=randint(1, 5), weight=CreateRandomWeight()) new_profile = Profile(description=random.choice(puppy_descriptions), picture=random.choice(puppy_images), puppy_id=i + 1) shelter = session.query(Shelter).filter_by(id=new_puppy.shelter_id).one() if shelter.current_occupancy == shelter.max_capacity: # Find shelter with some room free_shelter = (session.query(Shelter).filter( Shelter.current_occupancy < Shelter.max_capacity).first()) new_puppy.shelter_id = free_shelter.id free_shelter.current_occupancy += 1 session.add(free_shelter)
return random.uniform(1.0, 40.0) #This will create a random profile for a Puppy def CreateRandomProfile(): picture = random.choice(puppy_images) description = random.choice(puppy_descriptions) return PuppyProfile(picture=picture, description=description) #This will check a puppy in to a random shelter def CheckInToRandomShelter(puppy): shelter_id = randint(1,5) session.query(Shelter).filter(Shelter.id == shelter_id).one().checkIn(puppy) # Create puppies and wither have adopted or check in to random shelter for i,x in enumerate(male_names): new_puppy = Puppy(name = x, gender = "male", dateOfBirth = CreateRandomAge(), weight= CreateRandomWeight(),profile=CreateRandomProfile()) session.add(new_puppy) if randint(0,1): new_puppy.adopt(random.choice(adopters), random.choice(adopters)) else: CheckInToRandomShelter(new_puppy) session.commit() for i,x in enumerate(female_names): new_puppy = Puppy(name = x, gender = "female", dateOfBirth = CreateRandomAge(), weight= CreateRandomWeight(),profile=CreateRandomProfile()) session.add(new_puppy) if randint(0,1): new_puppy.adopt(random.choice(adopters), random.choice(adopters)) else:
print "All shelters are full. A new one needs to be opened." return shelter_id = input("Enter another shelter ID number to try: ") shelter = db_session.query(Shelter).filter_by(id=shelter_id).one() if shelter.current_occupancy == shelter.max_capacity: print "That shelter is also full!" full_shelters.append(shelter_id) continue else: found_free_shelter = True puppy.shelter_id = shelter_id shelter.current_occupancy += 1 db_session.add(puppy) db_session.add(shelter) db_session.commit() if __name__ == "__main__": # Setup the connection to the database engine = create_engine("sqlite:///puppyshelter.db") Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # Create a new puppy and check it into a shelter given by the user. new_puppy = Puppy(name="Fred", gender="male") req_shelter_id = input("Enter a shelter ID to check the puppy into: ") check_in_puppy(session, new_puppy, req_shelter_id)
today = datetime.date.today() days_old = randint(0, 540) birthday = today - datetime.timedelta(days=days_old) return birthday def create_random_weight(): """ :return: create a random weight between 1.0-40.0 pounds (or whatever unit of measure you prefer) """ return round(random.uniform(1.0, 40.0), 2) for name in male_names: new_puppy = Puppy(name=name, gender="male", dob=create_random_age(), shelter_id=randint(1, 5), weight=create_random_weight()) session.add(new_puppy) session.commit() for name in female_names: new_puppy = Puppy(name=name, gender="female", dob=create_random_age(), shelter_id=randint(1, 5), weight=create_random_weight()) session.add(new_puppy) session.commit()
def CreateRandomWeight(): """Create a random weight for a puppy. This method will create a random weight between 1.0-40.0 pounds (or whatever unit of measure you prefer). Returns: float: Random weight of puppy. """ return random.uniform(1.0, 40.0) for i, male_name in enumerate(male_names): new_puppy = Puppy(name=male_name, gender="male", date_of_birth=CreateRandomAge(), shelter_id=randint(1, 5), weight=CreateRandomWeight()) new_profile = Profile(description=random.choice(puppy_descriptions), picture=random.choice(puppy_images), puppy_id=i + 1) shelter = session.query(Shelter).filter_by(id=new_puppy.shelter_id).one() if shelter.current_occupancy == shelter.max_capacity: # Find shelter with some room free_shelter = (session.query(Shelter). filter(Shelter.current_occupancy < Shelter.max_capacity).first()) new_puppy.shelter_id = free_shelter.id free_shelter.current_occupancy += 1 session.add(free_shelter)
Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() try: session.query(Shelter).delete() session.query(Puppy).delete() session.commit() except: session.rollback() shelter1 = Shelter(name="Rajat Patwa puppy shelter", address="Street no. 7, House no. 8 ANewGalaxy, Mars.", phone="a cool phone number", email="a cool email address", owner="Rajat Patwa") session.add(shelter1) session.commit() puppy1 = Puppy(name="Tommy", weight=5.35, age=2, breed='German Shepherd', gender='Male', shelter=shelter1) session.add(puppy1) session.commit()