def newPuppy(shelter_id): if request.method == 'POST': newPuppy = Puppy(name= request.form['name'], gender='Male', shelter_id=shelter_id) session.add(newPuppy) session.commit() flash('New puppy added') return redirect(url_for('puppyMenu', shelter_id = shelter_id)) else: return render_template('new_puppy.html', shelter_id=shelter_id)
def checkinPuppy(shelter_id): if request.method == 'POST': newItem = Puppy(name = request.form['name1'], gender = request.form['name2'], picture = request.form['name4'] ,shelter_id = shelter_id) session.add(newItem) session.commit() return redirect(url_for('showPuppies' , shelter_id = shelter_id)) else: return render_template('checkinPuppy.html', shelter_id= shelter_id)
def PuppyCheckin(_name, _gender, _dateOfBirth, _picture, _shelterID, _wieght): shelter_capacity = session.query(Shelter.maximum_capacity).filter(Shelter.id == _shelterID).one() shelter_occupancy = session.query(Shelter.current_occupancy).filter(Shelter.id == _shelterID).one() shelter_state = session.query(Shelter.state).filter(Shelter.id == _shelterID).one()[0] shelter_city = session.query(Shelter.city).filter(Shelter.id == _shelterID).one()[0] print (shelter_state == "California") print shelter_city print (shelter_occupancy < shelter_capacity) if (shelter_occupancy < shelter_capacity): new_puppy = Puppy(name = _name, gender = _gender, date_of_birth = _dateOfBirth, picture = _picture, shelter_id = _shelterID, weight = _wieght) session.add(new_puppy) session.commit() else: close_shelter = session.query(Shelter.id).filter(Shelter.state == shelter_state).first()[0] print close_shelter if close_shelter: new_puppy = Puppy(name = _name, gender = _gender, date_of_birth = _dateOfBirth, picture = _picture, shelter_id = close_shelter, weight = _wieght) session.add(new_puppy) session.commit() print "Transfered to close shelter" else: print "Shelter at capacity and no close shelters"
def add_new_puppy(name, gender, dateOfBirth, picture, shelter_id, weight): """ Args: name: gender: dateOfBirth: picture: shelter_id: weight: Returns: Puppy: A puppy object. """ new_puppy = Puppy(name, gender, dateOfBirth, picture, shelter_id, weight) session.add(new_puppy) session.commit() occupancy = get_shelter_occupancy() update_occupancy(occupancy) return new_puppy
def createPuppy(): """ Creates a new puppy entry and adds it to the database. """ form = CreatePuppyForm(request.form) if request.method == 'POST' and form.validate(): new_puppy = Puppy(name=form.name.data, gender=form.gender.data, dateOfBirth=form.birthday.data, shelter_id=form.shelter_id.data, weight=form.weight.data) try: session.add(new_puppy) session.commit() print "Successfully created a new Puppy" flash("Successfully added %s to the our website!" % new_puppy.name) return redirect(url_for('puppyList')) except: print "The entry was no added to the database, an unexpected error occurred." flash( "The puppy was not added to the database, an error occurred!") return redirect(url_for('puppyList')) else: return render_template('new_puppy.html', form=form)
def AdoptPuppy(adopters): # remove puppy from its shelter, # decrement number of puppies in shelter``````` # add puppy to each adopter.puppies # add each adopter to puppies.adopters # 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): shelter_id = randint(1, 5) s = session.query(Shelter).filter_by(id=shelter_id).one() if not ShelterHasSpace(s): shelter_id = AssignPuppyToShelter(session) if shelter_id is not None: s = session.query(Shelter).filter_by(id=shelter_id).one() else: print 'NO MORE SPACE.\n' break new_puppy = Puppy(name = x, gender = "female", dateOfBirth = CreateRandomAge(),picture=random.choice(puppy_images),shelter_id=shelter_id, weight= CreateRandomWeight()) s.currentOccupancy = s.currentOccupancy + 1 # print s.currentOccupancy session.add(new_puppy) session.add(s) session.commit() print 'Final occupancy:' for shelter in session.query(Shelter).all(): print shelter.name, shelter.maxCapacity, shelter.currentOccupancy
"http://pixabay.com/get/3157a0395f9959b7a000/1433170921/puppy-384647_1280.jpg?direct", "http://pixabay.com/get/2a11ff73f38324166ac6/1433170950/puppy-742620_1280.jpg?direct", "http://pixabay.com/get/7dcd78e779f8110ca876/1433170979/dog-710013_1280.jpg?direct", "http://pixabay.com/get/31d494632fa1c64a7225/1433171005/dog-668940_1280.jpg?direct"] # 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(), 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()
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) session.add(puppy4) 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) #picture=random.choice(puppy_images), 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()
"Create another shelter".format(city)) return False # raise Exception("All shelters in {} are at full capacity. " # "Create another shelter".format(city)) else: print("You can try the following shelter(s):") print("\t|{0:_^6}|{1:_^50}|".format("Id", "Name")) for av_sh in avbl_shelters: print("\t|{0:^6}|{1:^50}|".format(av_sh.id, av_sh.name)) return False lp = Loripsum(pre_load=True) # Instance of Loripsum with pre-loaded vocabulary for i, x in enumerate(male_names): new_puppy = Puppy(name=x, gender="male", dateOfBirth=create_random_age(), weight=create_random_weight()) session.add(new_puppy) session.flush() # sync to DB but not persist session.refresh(new_puppy) new_puppy_profile = PuppyProfile(picture=random.choice(puppy_images), description=lp.local_random_paragraph(), specialNeeds=lp.local_random_paragraph(), puppy=new_puppy) session.add(new_puppy_profile) session.commit() check_puppy_into_shelter(new_puppy.id, randint(1, 5)) for _, x in enumerate(female_names): new_puppy = Puppy(name=x,
def create_puppy(shelter, session, name, puppy_gender): new_puppy = Puppy(name=name, gender=GENDER_MAP[puppy_gender], dateOfBirth=CreateRandomAge(), picture=random.choice(puppy_images), shelter_id=shelter.id, weight=CreateRandomWeight()) session.add(new_puppy) session.commit()