#100DaysOfWeb in Python Transcripts
Chapter: Days 33-36: Database access with SQLAlchemy
Lecture: Finishing the app

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's implement the rest of these functions here
0:02 and we'll be done with this program.
0:04 First thing we have to do is rent a scooter.
0:06 Now, if we go over here where this ToDo was
0:09 we'll have our data_service, and it has book_scooter.
0:13 All we have to do is provide it
0:15 the scooter, the user, and when.
0:17 So, let's say we want to book this scooter
0:20 we're going to list out all the scooters here
0:22 that's what this does.
0:24 And then, we'll let them randomly pick just a number
0:27 and they'll say, I want scooter four.
0:29 So we're going to book whichever one they pick here
0:33 and that's going to be
0:34 this global default user that we set up.
0:36 Last thing is just the time.
0:37 Let's say we want to book it right now.
0:41 Now, PyCharm is not being very cool
0:43 about what type it is.
0:44 We can go over here and say
0:45 this is a user, which we have to import.
0:50 PyCharm won't get over itself.
0:51 Okay, so we booked the scooter, right?
0:53 That's pretty good, but we won't really know
0:55 whether that worked until we can say
0:57 show us the available scooters
0:59 and see that it's no longer in that list.
1:01 So remember, available ones are parked at a location
1:05 and then, it's to just go over here to the data_service
1:08 say, give us the parked scooters.
1:12 I already wrote that. The other thing to do is say
1:14 for s in parked_scooters we want to pronounce something
1:19 and we want to give it a number so that people can pick.
1:21 So we'll use this enumerate operation here
1:23 say enumerate, that will say also that start equals one
1:28 so it goes one, two, three; not zero, one, two.
1:31 And for that, we're going to print out
1:33 I'll just drop a little thing here for you.
1:35 I'm going to print the number
1:36 and the location, street, and city
1:39 the id of the scooter, the model
1:41 its vin number, and its battery level.
1:44 So that will let us print those out.
1:46 Let's go ahead and run this.
1:47 We should be able to get some sort of output now.
1:49 If I should say, show me my available scooters
1:53 boom, look at that, it's working. How cool is that?
1:57 This one's over on 700 Terwilliger Boulevard in Portland.
2:00 It's all charged up 'cause it's parked
2:02 here's its vin number, things like that.
2:04 It's a Hover-1, Third Edition
2:06 not the sport, which is nicer.
2:07 But, okay, so that worked pretty well.
2:10 We should be able to rent one.
2:12 Let's go here and run this one more time
2:14 and say, show me my history. We have no rental history
2:17 because we haven't rented anything. Let's go try to rent one.
2:22 I want to rent number two. Now, how about number three
2:24 so that would be this vin number right there
2:27 I'll go ahead and copy that.
2:29 Okay, that was weird, let's see our history.
2:31 We haven't implemented our history
2:33 so that's not going to show a whole lot, is it?
2:36 So the next thing to do, let's go and implement our rental history.
2:39 This one is really interesting it uses our relationship.
2:42 So we have our user, right
2:44 this is our global logged-in user, if you will.
2:46 We'll say, for r in rentals, I guess
2:52 r in user., remember, we have that relationship.
2:59 Oh, comment it out, that's why it's not working.
3:01 So we are also going to have a rental relationship over there.
3:06 Any more commented out, yes, I'll get back to this.
3:10 So we can come over and say, now that we're finished
3:13 user.rentals, that's going to be the rental we got.
3:17 And then, on the rental
3:18 we can actually go to the scooter and print that out.
3:21 And then, here, we're going to go
3:22 and just show the start time for that rental
3:25 and then the scooter model that we have rented.
3:27 I believe that import stuff
3:29 actually already added some history for us.
3:32 So here, if we say, show out history, yeah, perfect
3:36 we rented, there were four that we rented before
3:40 that's part of the import data
3:41 and we added a fifth one.
3:43 So this is the one that we added today when we rented.
3:46 Let's rent one more. I want to get number nine.
3:51 Now, let's view our history. I made such a silly mistake.
3:56 So down here, when I was trying to see the user
4:00 let's say, change this so that we don't get complaints
4:04 notice we were using that user
4:06 to just traverse their relationship.
4:07 Well, see SQLAlchemy tries to be smart and say
4:11 you probably don't want to hit the database
4:12 over and over and over.
4:13 So once you've done that, like load that list
4:15 and just keep it in memory.
4:17 So this rental wasn't refreshing.
4:19 So what we need to do is go back to the database
4:22 do a query to reset that user each time.
4:27 So, everything's working fine
4:29 except for that little bit of reporting.
4:30 So now, we come over and see our history.
4:32 We have one, two, three, four, five
4:35 and we could rent another one.
4:37 Let's rent this Touring, First Edition here
4:41 it's going to be 11.
4:43 Now, if we look at our history, you can see
4:44 we have one, two, three, four, five, six
4:47 and the Touring, First Edition right at the top.
4:50 So we have our history working
4:52 we have our rent working
4:55 we have our available scooters.
4:57 These are all the ones that are available for rental
5:00 under Being Parked, and let's say, locate ours.
5:03 I don't think we've written this yet.
5:04 No, let's go write that
5:06 and it'll be the last thing to write.
5:08 So we say, data_service.rented_scooters
5:13 data_service.parked_scooters
5:16 and then, we just want to show
5:18 a kind of a loop like we did before.
5:20 So we'll say, how many are rented
5:22 how many are out with our client
5:24 and let's do a little print
5:25 like id and the model and the battery level.
5:27 Okay, these should all be below 100, probably.
5:30 Then, similarly, for our parked scooters
5:33 we want to go out and say
5:34 the parked ones are parked in these locations
5:36 navigating that relationship
5:38 and then, similar information.
5:40 Let's run this, and it's going to be a locator scooter.
5:44 This would be a function that somebody ran
5:45 who maybe owned or operated the hover share business.
5:49 So look at this, we have 11 remember, we have 21 total.
5:53 11 are out with clients
5:55 these have been where they are rented from
5:57 and their battery levels and so on.
5:59 And these are the parked ones
6:00 parked at their various locations.
6:02 Perfect, right?
6:03 So now, we have our entire app written.
6:06 We've inserted data, we defined our schema
6:08 we've set up the relationships
6:09 we imported some starter data for you
6:12 and everything is up and running
6:14 and I think it's working really, really well.
6:16 I hope that was fun to build.
6:18 I think it's a pretty realistic example.
6:20 It's a little bit complicated
6:21 but hopefully, not so complicated
6:23 that you lose sight of what we're really trying to do
6:26 and what the lessons are.
6:28 I'm going to give you something fairly realistic.
6:30 I think this encapsulates that pretty well.