#100DaysOfWeb in Python Transcripts
Chapter: Days 81-84: Unit testing web apps
Lecture: View models applied to details page

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's simplify our details page with ViewModels. We saw in our IndexViewModel well there wasn't really that much happening
0:09 so maybe it's justified maybe it's not. But certainly over here you'll see that there's a lot going on and this will be really helpful.
0:17 So again I'm going to create a class here. It's going to derive from ViewModelBase. Like so. It's going to have an init
0:24 PyCharm is being quite helpful here to bring all this stuff in. Well let's see what information we're working with here.
0:30 So in the request we're getting we're just working directly with the match dictionary. So it turns out that that part right there will be
0:39 quite helpful so we can just grab this and say you know what we're doing this. We need to import this again like so.
0:47 So let's go ahead and store all these on our item like so. You want to come in we're going to get the request use the request to get the bill_id
0:54 use the bill_id to get the bill. I'm going to do something similar for the user. Let's pass in the user_id.
1:09 Go and hold on to it in case we need it for somewhere. Now we've got our bill_id and we've got our user_id
1:14 now let's go back and see what else is going on. We'll pass that over we need to return this we're setting the error to None at least at the moment
1:20 we're validating this okay. So we'll go over here and we'll say this is the BillDetailsViewModel and pass in the request.
1:27 And then we need to pass in the user_id and right now we're just hacking it to be one but it could be anything.
1:32 So now we can say vm.bill and all this stuff down here is getting passed just by vm2 dictionary. So notice this was doing a lot more but now
1:42 it's barely more complicated then just the home page. That's good. Let's see did that work. If we click on one of these is it going to still work?
1:49 Tada, there we go of course we still have our details for kids and all that. Super. Now the other aspect is we want to do the same thing down here.
1:59 So let's go ahead and take it as far as we can with this idea. We've got all of this and now we can still work with our vm.bill like so
2:09 and we can also return vm.to_dict but notice there's this amount that we were passing around here.
2:16 We're going to need to work with that amount in just a second. But this is getting closer I guess we need to probably say something to that effect.
2:24 So we're passing around. So notice it's gotten simpler but now were doing this extra thing we're working with the form and this amount and so on.
2:31 So we're going to add one more layer to our ViewModel to not just basically look at the request but also to take the form get the data out of the form
2:39 and then do a little validation. That'll be the last thing we do with the ViewModels before we actually start writing tests.


Talk Python's Mastodon Michael Kennedy's Mastodon