def newPuppy(): if request.method == 'POST': newProfile = Profile(name=request.form['pupname'], weight=request.form['weight'], gender=request.form['gender'], picture=random.choice(puppy_images), dateOfBirth=datetime.strptime( request.form['birthday'], "%Y-%m-%d"), description=request.form['description'], specialNeeds=request.form['needs']) session.add(newProfile) new_puppy = Puppy(shelter_id=randint(1, 5), profile=newProfile) session.add(new_puppy) session.commit() flash("New puppy created!") return redirect(url_for('viewPuppy', key=newPuppy.id)) else: return render_template('new.html', viewType="puppy", traits=Puppy.defaultTraits())
def makeANewPuppy(name, description): DBSession = sessionmaker(bind=engine) session = DBSession() puppy = Puppy(name=name, description=description) session.add(puppy) session.commit() return jsonify(Puppy=puppy.serialize)
def newPuppy(): if request.method == 'POST': newProfile = Profile( name = request.form['pupname'], weight = request.form['weight'], gender = request.form['gender'], picture = random.choice(puppy_images), dateOfBirth = datetime.strptime(request.form['birthday'], "%Y-%m-%d"), description = request.form['description'], specialNeeds = request.form['needs'] ) session.add(newProfile) new_puppy = Puppy( shelter_id = randint(1,5), profile = newProfile) session.add(new_puppy) session.commit() flash("New puppy created!") return redirect(url_for('viewPuppy', key = newPuppy.id)) else: return render_template( 'new.html', viewType = "puppy", traits = Puppy.defaultTraits() )
def add_puppy(name, gender, dob, shelter_id): puppy = Puppy(name=name, gender=gender, dateOfBirth=dob, shelter_id=shelter_id) session.add(puppy) session.commit()
def makeANewPuppy(name, description): session = newSession() try: puppy = Puppy(name=name, description=description) session.add(puppy) session.commit() return jsonify(Puppy=puppy.serialize) finally: session.close()
def puppyAdd(): if request.method == 'POST': formDate = datetime.strptime(request.form['dateOfBirth'],'%Y-%m-%d') newItem = Puppy(name = request.form['name'], dateOfBirth = formDate, breed = request.form['breed'], gender = request.form['gender'], weight = request.form['weight'], picture = request.form['picture']) session.add(newItem) session.commit() flash("New puppy has been added.") return redirect(url_for('puppyList')) else: return render_template('puppyadd.html')
def create_puppy(): # validate attributes name = request.form.get('name') if not name: return 'name required', 400 image_url = request.form.get('image_url') if not image_url: return 'image_url required', 400 slug = slugify(name) # create in database puppy = Puppy(slug=slug, name=name, image_url=image_url) db.session.add(puppy) db.session.commit() # return HTTP response resp = jsonify({'message': 'created'}) resp.status_code = 201 location = url_for('get_puppy', slug=slug) resp.headers['Location'] = location return resp
def create_puppy(): # validate attributes name = request.form.get("name") if not name: return "name required", 400 image_url = request.form.get("image_url") if not image_url: return "image_url required", 400 slug = slugify(name) # create in database puppy = Puppy(slug=slug, name=name, image_url=image_url) db.session.add(puppy) db.session.commit() # return HTTP response location = url_for("get_puppy", slug=slug) resp = jsonify({"message": "created"}) resp.status_code = 201 resp.headers["Location"] = location return resp
def populate(self): #Add Shelters shelter1 = Shelter(name="Oakland Animal Services", address="1101 29th Ave", city="Oakland", state="California", zipCode="94601", website="oaklandanimalservices.org", maximum_capacity=randint(25, 35)) 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", maximum_capacity=randint(25, 35)) session.add(shelter2) shelter3 = Shelter(name="Wonder Dog Rescue", address="2926 16th Street", city="San Francisco", state="California", zipCode="94103", website="http://wonderdogrescue.org", maximum_capacity=randint(25, 35)) session.add(shelter3) shelter4 = Shelter(name="Humane Society of Alameda", address="PO Box 1571", city="Alameda", state="California", zipCode="94501", website="hsalameda.org", maximum_capacity=randint(25, 35)) session.add(shelter4) shelter5 = Shelter(name="Palo Alto Humane Society", address="1149 Chestnut St.", city="Menlo Park", state="California", zipCode="94025", website="paloaltohumane.org", maximum_capacity=randint(25, 35)) session.add(shelter5) session.commit() self.shelters.extend( [shelter1, shelter2, shelter3, shelter4, shelter5]) # Add Adopters a_name = random.choice(adopter_names) adopter1 = Adopter(name=random.choice(adopter_names)) removeAdopterName(a_name) session.add(adopter1) a_name = random.choice(adopter_names) adopter2 = Adopter(name=random.choice(adopter_names)) removeAdopterName(a_name) session.add(adopter2) a_name = random.choice(adopter_names) adopter3 = Adopter(name=random.choice(adopter_names)) removeAdopterName(a_name) session.add(adopter3) a_name = random.choice(adopter_names) adopter4 = Adopter(name=random.choice(adopter_names)) removeAdopterName(a_name) session.add(adopter4) a_name = random.choice(adopter_names) adopter5 = Adopter(name=random.choice(adopter_names)) removeAdopterName(a_name) session.add(adopter5) session.commit() self.adopters.extend( [adopter1, adopter2, adopter3, adopter4, adopter5]) #Add Puppies print "Before Males: %s" % self.numShelters() for i, x in enumerate(male_names): new_profile = CreateProfile(x, "male") session.add(new_profile) shelter_id = self.CheckinPuppy() new_puppy = Puppy(shelter_id=shelter_id, profile=new_profile) new_puppy.adopters.append(self.ChooseAdopter()) session.add(new_puppy) session.commit() print "Before Females: %s" % self.numShelters() for i, x in enumerate(female_names): new_profile = CreateProfile(x, "female") session.add(new_profile) shelter_id = self.CheckinPuppy() new_puppy = Puppy(shelter_id=shelter_id, profile=new_profile) new_puppy.adopters.append(self.ChooseAdopter()) session.add(new_puppy) session.commit()
from models import db, Puppy, Toy, Owner rufus = Puppy('Rufus') fido = Puppy('Fido') db.session.add_all([rufus, fido]) db.session.commit() print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) jose = Owner('Jose', rufus.id) toy1 = Toy('Chew toy', rufus.id) toy2 = Toy('Ball', rufus.id) db.session.add_all([jose, toy1, toy2]) db.session.commit() rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) rufus.report_toys()
from models import db, Puppy, Owner, Toy brigite = Puppy('Brigite') olie = Puppy('Ollie') # ADD db.session.add_all([brigite, olie]) db.session.commit() print(Puppy.query.all()) brigite = Puppy.query.filter_by(name='Brigite').first() mima = Owner('Mima', brigite.id) # Give brigite some toys biter = Toy('Biter', brigite.id) ball = Toy('Ball', brigite.id) db.session.add_all([mima, biter, ball]) db.session.commit() brigite = Puppy.query.filter_by(name='Brigite').first() print(brigite) print(brigite.report_toys())
from models import db, Puppy, Owner, Toy db.create_all() rufus = Puppy('rufus') spot = Puppy('spot') db.session.add_all([rufus, spot]) db.session.commit() print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='rufus').first() jose = Owner('José', rufus.id) ball_toy = Toy('Ball', rufus.id) train_toy = Toy('Train', rufus.id) db.session.add_all([jose, ball_toy, train_toy]) db.session.commit() rufus = Puppy.query.get(rufus.id) print(rufus) rufus.report()
# This script will create some puppies, owners, and toys! # Note, if you run this more than once, you'll be creating dogs with the same # name and duplicate owners. The script will still work, but you'll see some # warnings. Watch the video for the full explanation. from models import db, Puppy, Owner, Toy # Create 2 puppies rufus = Puppy("Rufus") fido = Puppy("Fido") # Add puppies to database db.session.add_all([rufus, fido]) db.session.commit() # Check with a query, this prints out all the puppies! print(Puppy.query.all()) # Grab Rufus from database # Grab all puppies with the name "Rufus", returns a list, so index [0] # Alternative is to use .first() instead of .all()[0] rufus = Puppy.query.filter_by(name='Rufus').all()[0] # Create an owner to Rufus jose = Owner("Jose", rufus.id) # Give some Toys to Rufus toy1 = Toy('Chew Toy', rufus.id) toy2 = Toy("Ball", rufus.id) # Commit these changes to the database db.session.add_all([jose, toy1, toy2])
def puppyPopulate(): #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", zipCode="94025", website="paloaltohumane.org") session.add(shelter5) #Add Puppies 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' ] puppy_images = [ "http://pixabay.com/get/da0c8c7e4aa09ba3a353/1433170694/dog-785193_1280.jpg?direct", "http://pixabay.com/get/6540c0052781e8d21783/1433170742/dog-280332_1280.jpg?direct", "http://pixabay.com/get/8f62ce526ed56cd16e57/1433170768/pug-690566_1280.jpg?direct", "http://pixabay.com/get/be6ebb661e44f929e04e/1433170798/pet-423398_1280.jpg?direct", "http://pixabay.com/static/uploads/photo/2010/12/13/10/20/beagle-puppy-2681_640.jpg", "http://pixabay.com/get/4b1799cb4e3f03684b69/1433170894/dog-589002_1280.jpg?direct", "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()
# create entries into the tables from models import db, Puppy, Owner, Toy tarzan = Puppy('Tarzan') bobik = Puppy('Bobik') db.session.add_all([tarzan, bobik]) db.session.commit() print(Puppy.query.all()) tarzan = Puppy.query.filter_by(name='Tarzan').first() print(tarzan) peter = Owner('Peter', tarzan.id) toy1 = Toy("chew toy", tarzan.id) toy2 = Toy('Ball', tarzan.id) db.session.add_all([peter, toy1, toy2]) db.session.commit() tarzan = Puppy.query.filter_by(name='Tarzan').first() print(tarzan) print(tarzan.report_toys())
"""This script will create some puppies, owners, and toys! Note, if you run this more than once, you'll be creating dogs with the same name and diplucate owners. the script will still work, but you'll see some warnings.""" from models import Puppy from training_day1 import db # Creating two puppies rufus = Puppy('Rufus', 3) fido = Puppy('Fido',4) print(rufus.id) print(fido.id) # ADD PUPPIES TO DB db.session.add_all([rufus, fido]) db.session.commit() print(rufus.id) print(fido.id) # check with a query, this prints out all the puppies! #print(Puppy.query.all()) # Grab rufus from database # Grab all puppies with the name 'Rufus', returns a list, so index[0] alternative is to use # .first() instead of .all()[0] # rufus = Puppy.query.filter_by(name='Rufus').first() # print(rufus) # Create Owner to rufus #jose = Owner('Jose', rufus.id) # Give Rufus some toys
# BASIC.PY # CREATE ENTRIES INTO THE TABLES! from models import db,Puppy,Owner,Toy # Creating 2 puppies rufus = Puppy('Rufus') fido = Puppy('Fido') # ADD Puppies to db db.session.add_all([rufus,fido]) db.session.commit() # Check! print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='Rufus').all()[0] print(rufus) # Create owner object jose = Owner('Jose',rufus.id) # Give Rufus some toys toy1 = Toy('Chew Toy', rufus.id) toy2 = Toy('Ball', rufus.id) db.session.add_all([jose,toy1,toy2]) db.session.commit() # GRAB RUFUS AFTER THOSE ADDITIONS! rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus)
from models import db, Puppy, Owner, Toy # create puppy rufus = Puppy('Rufus') kit = Puppy('kity') db.session.add_all([rufus, kit]) db.session.commit() all_pup = Puppy.query.all() print(all_pup) rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) # create owner mejomba = Owner('mojtaba', rufus.id) # create toy ball = Toy('ball', kit.id) pangpang = Toy('pank', kit.id) db.session.add_all([mejomba, ball, pangpang]) db.session.commit() rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) all_pup = Puppy.query.all()
# Create entries into the table. # First let's import models from models.py # Write the script so it's meant to be run once. from models import db, Puppy, Owner, Toy # Create some puppy objects. rufus = Puppy('Rufus') sammy = Puppy('Sammy') # Add puppies to the database. # Can also use add_all([rufus, sammy]) db.session.add_all([rufus, sammy]) db.session.commit() # Check if the puppies are added to the database. ############# PRINT FUNCTION ################ print(Puppy.query.all()) # grad Rufus from the database. # we filer by name, and by using "first()"; we are grabbing the first. # If we wish to grab all of the items in the list use ".all()"; and then if you want the first of it use indexing ".all()[0]" rufus = Puppy.query.filter_by(name='Rufus').first() # Create owner object jose = Owner('Jose', rufus.id) # Give toys to Rufus.
from models import db, Puppy, Owner, Toy # Create 2 Puppies rufus = Puppy('Rufus') fido = Puppy('Fido') # Add puppies to DB db.session.add_all([rufus, fido]) db.session.commit() #Check the output print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='Rufus').first( ) # all() will give the list of all puppies udner the name Rufus # Create owner for Rufus jose = Owner('Jose', rufus.id) # Give Rufus some toys toy1 = Toy('Chew Toy', rufus.id) toy2 = Toy('Ball', rufus.id) # Add the owner and toys to DB db.session.add_all([jose, toy1, toy2]) db.session.commit() print(Puppy.query.filter_by(name='Rufus').first())
from app import db from models import Puppy for i in range(1, 11): p = Puppy(f"Puppy {i}") db.session.add(p) db.session.commit() list = Puppy.query.all() for puppy in list: print(f"ID: {puppy.id} \t| Name: {puppy.name}") # for puppy in list: # db.session.delete(puppy) # db.session.commit()
from models import Puppy, Owner, Toy, db rufus = Puppy('Rufus') fido = Puppy('Fido') db.session.add_all([rufus, fido]) db.session.commit() print(Puppy.query.all()) print(Puppy.query.filter_by(name='Rufus').first()) jose = Owner('Jose', rufus.id) toy1 = Toy('Chew-Toy', rufus.id) toy2 = Toy('Ball', rufus.id) db.session.add_all([jose, toy1, toy2]) rufus = Puppy.query.filter_by(name='Rufus').first() print(rufus) rufus.my_toys()
# create entries in the db from models import db, Puppy, Owner, Toy # creting two puppies lluvia = Puppy('Lluvia') travieso = Puppy('Travieso') # add puppies to db db.session.add_all([lluvia, travieso]) db.session.commit() # check print(Puppy.query.all()) lluvia = Puppy.query.filter_by(name='Lluvia').first() travieso = Puppy.query.filter_by(name='Travieso').first() #Create owner object alex = Owner('Alex', lluvia.id) ale = Owner('Ale', travieso.id) # give lluvia some toys toy1 = Toy('Chew Toy', lluvia.id) toy2 = Toy('Ball', lluvia.id) # give travieso some toys toy3 = Toy('Chew Toy', travieso.id) toy4 = Toy('Ball', travieso.id)
resp.status_code = 404 return resp @app.errorhandler(401) def unauthorized(error): resp = jsonify({"error": "unauthorized"}) resp.status_code = 401 return resp if __name__ == "__main__": if "createdb" in sys.argv: with app.app_context(): db.create_all() print("Database created!") elif "seeddb" in sys.argv: with app.app_context(): p1 = Puppy(slug="rover", name="Rover", image_url="http://example.com/rover.jpg") db.session.add(p1) p2 = Puppy(slug="spot", name="Spot", image_url="http://example.com/spot.jpg") db.session.add(p2) db.session.commit() print("Database seeded!") else: app.run(debug=True)
def makeANewPuppy(name,description): puppy = Puppy(name = name, description = description) session.add(puppy) session.commit() return jsonify(puppy=puppy.serialize())
def makeANewPuppy(name, description): puppy = Puppy(name=name, description=description) session.add(puppy) session.commit() return jsonify(Puppy=puppy.serialize)
#create entries into the tables from models import db, Puppy, Owner, Toy # creating two pups rufus = Puppy('Rufus') leo = Puppy('Leo') # Add puppies to db db.session.add_all([rufus, leo]) db.session.commit() #Check print(Puppy.query.all()) rufus = Puppy.query.filter_by(name='Rufus').first( ) # gives the first entry encountered whose name is rufus print(rufus) #rufus = Puppy.query.filter-by(name='Rufus').all()[0] === Similar to the above line of code #create owner object amruta = Owner('Amruta', rufus.id) # giving rufus toys