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


Talk Python's Mastodon Michael Kennedy's Mastodon