#100DaysOfWeb in Python Transcripts
Chapter: Days 37-40: Introduction to Pyramid framework
Lecture: Home page (database data)

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It was nice to render this non-trivial bit of data here but that's not actually what we want to show. We want to show our users, we want to
0:08 show their bills, things like that. Let's go over here to our data section and have a look. So, if we look at our user, you can see we have an id
0:19 and a name and an e-mail and so on. We also have a relationship over to their bills. Okay, we have some properties that will show us
0:27 their paid bills, the ones that are still pending they still need to pay, how much money they owe.
0:31 So, what we would like to do is go and get a user from the database, get them back and then use these relationships and these properties to get rich
0:40 information and show it on that page. Turns out this is going to be ridiculously easy. So, I'll say user.id == 1.
0:49 Now, this you'd probably get this from a login cookie or something, but we're just hard code it cause, even though
0:55 this interesting and non-trivial web app we're building we don't want to go too crazy and just overwhelm you with all the details.
1:01 So, we're going to go get a cookie that we set that once they login that has some form of a protected cookie that has their user id.
1:09 Then, we're going to go get the user from our repository. Put that up here, and we'll say, let's import it like that.
1:18 So, we're going to go and get our repository and the repository already has, let's go look at that
1:23 that came like, paste this over, it has three queries. Three or four? Three. They're really simple, get me a user by id, do a join
1:32 against their bills or don't, we're just going to let it do that because we want their bills. So, it'll come in to a query, a join on their bills
1:41 find the right one and either give us nothing back if they don't exist or give us one back if we, the one and only one that has that primary key.
1:48 So, our goal over here will just be get_user_by_id, or pass in the user ID, like that, and then we want to pass this user along, like this.
1:59 Now, do we actually need to pass these things, no. We're not going to use them, you know maybe in some apps
2:04 we might have to get the bills and do some other query to get them here, something else would go right here.
2:09 We can just go to the user.bills and navigate that. Right, we've already joined against it with our repository. So, we don't actually care about it.
2:17 How about that, that's the entire implementation of our homepage. Now, granted our real one may be a few more pieces of
2:22 data would have to come along, but that's pretty much it. Also notice we're not using the request.
2:28 We would use that, here, if we were getting a cookie. We'd go to request.cookies and so on. If you want, come over and say a
2:34 request. and you get no help, right? None, and whatever you get nothing, but if we go over here
2:41 and say this is their request, from pyramid, like this one. Then, we have things like, a cookie, a cookies dictionary
2:48 all sorts of stuff that's in here we can work with. But, that said, as interesting as that is, we are not
2:55 using it so we can use the convention in Python to say put an underscore there, which means, we mean to not
3:01 use it but it has to be there for the signature to work. So, we got our user now, if we run it, it's not going to be so wonderful, let's find out why.
3:10 Boom, this project, remember we said, here's your project and we put the dollar project name, well that doesn't exist anymore, we deleted it.
3:18 Alright, so you'll see these values that appear in the template are required. So, let's go and rewrite our template a little bit.
3:24 This thing we don't really need, I'll do an H1. We're going to make this pretty, in a little bit but
3:30 we'll just go, a little tracker pro demo, okay, and the items is also going to give us an error so we're not going to show that.
3:37 Let's just say, something like this, div welcome ${user.name}, and what do we want, we want to say their name. How awesome is that, that we have
3:46 auto complete there, that is super cool. I want to say something like you currently owe, total owed.
3:53 Now, that's a floating point number, we probably want to format that a little bit nicer. So, we come over here and say, call function
4:00 you put standard Python in here. So, we're going to have something like {"${:,.0f}".format(user.total_owed)} something like that.
4:07 Now this dollar is an instruction to the template, but won't appear so if we want a dollar to appear, we've got to put it like that or maybe
4:14 it's more obvious if we put it like this. Let's rerun this and see what we get. Awesome, look, well welcome Danny Isum you currently owe 2,836 dollars.
4:25 Well, let's print out their bills, so we've got a few more things we want to print. Let's do a div here of an H2, this will be opens, pending
4:34 unpaid, that's called unpaid bills. So, we want to come down here and have just, list them out. So, we'll just do a quick list and then we'll bring
4:43 over some HTML, when we do the design to make this prettier. So, let's just have another div and we'll have TAL repeat
4:49 remember that, b and user.bills and it'll just say, b.description. We'll use the same little format trick and let's
5:01 just do a, we got total minus b.paid. So, this is our unpaid ones and then lets do another for our paid bills, those are the ones we like.
5:15 We don't want to do bills here, we want to do unpaid bills, open bills and paid bills. So, open bills and paid bills and this one will just show
5:27 how much this amount pending, it will show that, how much is pending, this one just, this one is paid. Paid off at this.
5:38 Alright let's try now one more time, see what we got. Boom, well the design looks like, well not terrible but it
5:43 definitely is inappropriate of what we're looking for but down here, look at this, we have our unpaid bills
5:49 all that money there, and then here are our paid bills we paid the games off at $23.00 whatever we did on the home categories, $622.00 and so on.
5:58 Isn't that cool and you can bet if you add up those numbers and those numbers they should be the same unless they have a bug in my code somewhere.
6:06 So, this is how we go and talk to the database we put it in the page and we use all the interactions that Pyramid provides us and really, what's left?
6:16 Well, there's two major things left one this looks like crap right? You wouldn't, I mean it's a fine little starter project
6:22 template but it definitely doesn't look like what we want our website to look like, that's one. Two, how do we work with this website, right now it's
6:30 read only, I would like to say, maybe pay off my $568 of beauty and just pay that baby off, how do I tell this website that I've done it?
6:38 Alright, it's not pulling this from a service it's kind of static. So, we need to add the ability to see the details of
6:44 these and enter the amount we want to pay, and pay towards that amount, they're handling all that sort of stuff, so those are
6:49 the two major things left, you'll see they're both pretty straight forward.


Talk Python's Mastodon Michael Kennedy's Mastodon