#100DaysOfWeb in Python Transcripts
Chapter: Days 81-84: Unit testing web apps
Lecture: View models and forms

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Our view models have been simplifying things. They've been isolating this data exchange between the templates and our view methods here
0:08 but we've run into this part where we need to get some information from the form. Remember, request.post, that's the submitted form.
0:15 This is the method that runs when they submit the form to add a payment. Let's just look real quick there.
0:20 So, we come down to one of these and we click on it. When we go to make a payment, let's make this 700 come down and say, well, it didn't accept it
0:28 because it was the wrong amount, but that's okay. It was too high, right, but this is the thing being executed when we submit this form to bill 986.
0:38 All right, so the last step is to make this part work here so we'll go over here and we'll say from form and we need to pass it this.
0:47 You know, I guess we could probably even we already have a reference to it so we'll just leave it like that. Let's go in here and add this function.
0:54 Let PyCharm write it. It doesn't need anything so we can say the amount, self.amount and generally you only want to define fields like so
1:05 so this would be a self.request 'cause it's the and that back. There's a validation with a few too many dots. We're going to set self.error.
1:14 Super, so this lets us grab the amount convert it to an integer. Maybe we would even like to do you'd have an error here, something like this
1:22 like if they pass in ABC for the amount we want to tell them amount must be an integer or a number, and let's go ahead and put that here. There we go
1:32 so either we're going to get the amount to a proper value or we're going to tell them they couldn't you know, they, it's got to be a number.
1:37 Great, let's try this one more time and make sure we're calling it. Here, we can get rid of this so in the case that there's an error
1:46 which was that, we'll just say vm.error otherwise we're going to add the payment. All right, this is looking good, so we're calling it from form
1:54 checking whether or not it succeeded and if it did we'll submit the payment. Now we can see it work, so let's go pay this: toys for $72.
2:02 Now, let's start by making a valid payment: $2. This should go down to 70. Beautiful, beautiful, it did.
2:09 Let's say we want to pay $7,000 on this $70 bill. It's going to come down and say, no, no, there's an error. The amount must be more than zero
2:18 and less than what you owe, right? We could also check the negative case real quick and then, if we go ahead and pay it exactly off
2:25 boom, paid off, amount owed is zero. Now let's go back and look at our post method here. Notice how much simpler it is, right?
2:33 We can add more and more validation and it's just going to pile up in this section over here which is dedicated to validation, and this data exchange
2:42 no matter how much data we have to exchange it's mostly going to happen right here so that means there's kind of this upper bound
2:48 on the complexity of this. Also, we can just create one of these and test it without trying to deal with all the other stuff happening in this method
2:56 to test the data exchange validation. All right, so we don't have too much of a complicated application here so communicating across how much value
3:04 these view models bring it's a little bit challenging but I think if you try this in your web applications this idea of these view models
3:12 for data exchange and validation you'll find really quickly that they keep things simple and make it easy to work with this data exchange.
3:18 Now let's go do some testing.

Talk Python's Mastodon Michael Kennedy's Mastodon