#100DaysOfWeb in Python Transcripts
Chapter: Days 37-40: Introduction to Pyramid framework
Lecture: Paying a bill: Submitting forms
0:00 The next thing we should do
0:02 one of the final things we're going to do
0:03 is to submit that form
0:06 let the users pay part of their or all of their bill
0:08 and then show them the updated details.
0:10 And to do this, we're going to use our
0:12 details_get and our details_post.
0:14 We already have this working, this just shows the state
0:17 of a bill as it is. This one we have not written yet
0:22 but we're going to follow what is called
0:23 the Get Post Redirect pattern.
0:26 So, we're going to show the data, show the form that's the get
0:29 and then they're going to let them post the form
0:32 that's what this method does
0:34 and then if everything is good, we're going to redirect
0:36 them to a new view of the data.
0:38 Either back to this page or back to the summary information
0:41 probably back to that page. So, how do we do that?
0:46 Well, let's start by taking the same data we're doing here
0:49 because we're going to need a lot of the same stuff
0:51 like the bill_id and what not.
0:55 So we're getting the bill_id, this is part of the URL
1:00 that doesn't exist, we're going to bail, we get the user
1:01 and we also going to need to get the amount.
1:04 How do we do that? Well, we again go back to the request
1:07 and we're not going to use the match dict
1:08 that's for the routing.
1:09 We're going to use the post dictionary.
1:12 Then we get the amount and I'm going to pass it
1:15 and maybe -1 if it doesn't exist.
1:18 And again, error handling here would be a good idea.
1:20 We're going to skip it. So we'll say it's something like this
1:22 if amount < 0 or amount > bill.total-bill.paid:
1:29 So they're trying to pay too much
1:31 or they're trying to pay a negative amount
1:35 Set some kind of error, error equals nothing.
1:38 We'll say: You're amount must be more than zero
1:44 and less than what you owe.
1:47 We can do something better than that
1:49 but if we have that we want to
1:51 return this error here, like so.
1:54 Now if there's not an error
1:59 actually let's do like this, I guess.
2:03 If there is not an error, we would actually like
2:06 not to just show them this. Just send them back
2:08 somewhere else. So we're going to return our response.
2:11 Let's do it this way, raise HTTPFound exception.
2:18 I'll say the location is slash, details, sorry no
2:24 that's bill and here we format with the bill_id.
2:28 So, let's just try that, we are not actually doing
2:30 the payment yet, there is still left to do
2:32 let's just see there is something happening.
2:33 So, if I go over here and I say we are going to make a
2:35 payment of $100 or something like that.
2:39 We'll come back and say; oh, we caught this error amount.
2:44 Oh, I see we are making a mistake here, we are going to
2:48 have to pass one more thing. Let's go to details.
2:51 Now notice, if there is an error we want to pass along
2:54 this amount, so we are going to need to return what they
2:58 said they'd pass. That's because we want to keep the form
3:00 the way they had entered it.
3:02 So, over here, want to make sure we pass this amount.
3:07 Let's try our validation again, we'll go back here
3:10 look at it, pay $100.
3:14 Notice there is an error, it says; Your amount must be
3:21 more than zero less than what you owe.
3:22 Fix that, spelling your amount, right there.
3:27 So how do we get that to happen?
3:28 Let's look over here at the details.
3:30 There is one more cool thing we haven't done yet.
3:32 Check this out. So here is the error and it is red
3:35 because it has a class error. We said TAL condition.
3:37 So this entire block of code, HTML is only showed if error
3:42 is truthy in Python. So, a non-empty string. So, that only
3:46 shows up if there is an error. Obviously, the value of that
3:50 input we want to carry that amount around so that we
3:52 can keep that there and only give them the error.
3:54 So, let's say we want to pay $100, which should be valid.
3:58 Go back here, everything is good. If we want to pay
4:01 $10,000, it's going to be an error again, the amount you
4:03 owe must be positive.
4:05 It's kind of annoying this is below the fold, you like them
4:08 to maybe see that but that's the error handling we've gotten.
4:12 Final, final thing, super quick, let's just do a payment.
4:15 And the repository has an add payment for, you go to the
4:19 amount and they bill, the bill_id.
4:23 So, let's see if we can pay off some of these bills here.
4:26 Alright, I suppose we got some money, we going to pay off
4:28 this gardening bill. We are going to come over here and we
4:31 are going to start by trying to pay $100.
4:34 $199, that's not going to work. Oh yeah right
4:36 I would like to pay $100, so this amount should just be
4:40 seventeen, hey look at that, seventeen, got some more money
4:44 I'm going to pay off fifteen, now we are down to 2.
4:47 Now, if we do this it goes to zero and also we put the same
4:51 TAL condition on the same form so say if it's paid off
4:59 don't show that form at all so they can't try to pay
5:02 on stuff they don't owe or whatever, so it's gone.
5:06 We go back here, our gardening is no longer up here
5:08 notice the gardening is gone, now the gardening is right
5:11 here as paid off. Awesome, right? How do we do that?
5:15 Get, Post, Redirect. We have the get the details
5:20 here is our get request method, just show the details.
5:24 Down here we are saying is the post one that handles
5:28 the submission of the form or getting the data this
5:31 one from the form, if everything is validated
5:35 we record the action in the database and we do the
5:38 redirect by raising HTTPFound to the URL we would
5:41 like them to go to.
5:43 That's it, that's how we accept your input and let
5:45 them interact with this data, nice right?