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