Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: View models demo (reading data section)

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Ok so let's move this over into the view model as well. This part here we will point in in this data and I am going to call it like this,
0:08 I'm going to add a function called from_dict and we need to give it a dictionary, so we go like this, let me just cut that out of here
0:21 we'll add that method, let PyCharm do it and POST is not a good idea, let's just call it data_dict or something like that,
0:27 and here we are going to do basically the same code but instead of self.request, we are just going to say "get that from the data_dict", like so
0:34 this lets us get it from the query string, from other places as you'll see later we'll unify this into one piece, and this will be self.email
0:44 and our error, we don't get that from the form, that's generated in the handler, so that might come out later.
0:51 Now let's go over here and we'll say vm.from_dict and then see all this stuff, let's just write this in terms of the model,
0:58 the data the model the view models gathered and then we'll fix this
1:02 so we'll say vm.password, if those aren't equal, we'll move this somewhere else in a moment. vm.confirm_password. If that doesn't match
1:10 we want to turn all this data back but we also want to set an error so we'll say vm.error, it's going to be whatever the error was.
1:19 And then all of this goes away we just say "return vm.to_dict()", remember, it can't give it itself.
1:25 So look how this what we need to get from the form is gone, we are going to remove this in a moment,
1:32 the concept of doing the testing here is well and let's try it one more time.
1:37 Go over here and try to register, this will be "jeff", hit Enter, oh oops, my little print statement is killing us here,
1:47 I forgot to move that over, and let's just delete this, alright, that was just telling us that was being called, right,
1:52 this is not actually part of the form or anything, this is just me trying to print this out and from the previous one. Let's do it again.
1:59 We repost it, ta-da, error, the password and confirmation don't match because I entered two values, let me just put the word "the" into both of them
2:07 and now it should run through that comparison and it should be all good. Boom, your account is valid.
2:14 Last step, let's move this validation into the view model, so I am just going to take this and we'll say vm.validate, this is a thing I am making up,
2:23 we can call it whatever we want. So down here it's going to just run whatever test we had, well how about this,
2:29 of course vm doesn't make sense here but "self" does, and let's start by saying this,
2:36 the beginning of this validate we are going to just in case null out the errors, and then we are going to do this, if there is some kind of failure,
2:47 we are going to set the error, maybe this error should be a list of errors, who knows,
2:50 but for this sample case, we are just going to make it a single string, not a set of errors that could possibly occur.
2:56 OK, and then up here we are going to say "validate" and then we are just going to say "if there is an error,
3:02 we don't really care or know what it is, we just don't want to display the page", OK,
3:07 so we are going to create the view model, we are stored from the POST data, we'll do a validation and we'll just see if it failed the validation
3:16 we'll go and just show the form again, we don't have to worry in this area what happens,
3:20 but what we do care about is up here, we'll start working with vm.email and things like that to create the account assuming everything was setup OK.
3:29 Alright, last bit, let's try, one more time. So over here, we'll put in an email address,
3:35 the password is going to be, I'll make them mismatch for a minute, we'll try this, error, it doesn't match,
3:42 alright, this should be identical behavior as we had before. Now they do match, boom, look at that, how nice is that,
3:48 we could also add additional validation here, we could say things like "if self"... "if not self.password",
3:56 like if the password is empty, you could say something like this, like maybe we want to bail out of here
4:02 as soon as we can so we could change this to return right, we are not collecting a set of errors, and we'll say "if not self.email"
4:13 OK, great, now we are really validating for all the things that we probably care about,
4:16 we could also check for valid email using regular expression or whatever,
4:19 but you can get the idea, right, so if I just hit "register", you must specify a password, the password doesn't match, I better remember what I typed,
4:27 make the match, you must specify an email address, see how it carries the data back around, beautiful, let's pick that one,
4:35 let's say go, and boom, we are registered. So you can see we've created this thing I called the view model,
4:41 based on our ViewModelBase, which really is just to give us this to_dict as a common feature
4:47 and it stores various fields that the template is going to need, initially it's empty it could allocate these from a database or somewhere
4:56 but this one, there is no data to start with, so it starts like this, and we have a from_dict, which will read the form POST basically,
5:03 and then a validation, and look how much simpler this becomes. So let's go over here, I'll show you cool trick on the accounts control,
5:11 I can go to local history, show history, I haven't checked this into GitHub but let's have a look anyway, so we can compare this against yesterday,
5:21 oops, that's not good enough, there we go, so we can compare this against this, so you can see that the GET call is much simpler,
5:28 this register here is just the to_dict and no longer does the controller really care about the data that's being exchanged,
5:34 it only has to work with the data that it cares about, so we've pushed that off to the view model and look how much simpler this has gotten,
5:41 all this stuff about getting this thing or that, you know, getting the password, "what was it called in the form again?", forget it,
5:48 that's the view model's problem, it goes to the from_dict, this I took away because I just didn't want to edit it, and this validation,
5:54 here we only had one but you saw we really needed three or four different validations at least,
5:59 and so those were all moved and expanded down into this "validate", and we just look at the error.
6:07 So this view model idea is a bit of a new concept that layers on top of what we are doing here, but once you start working with it,
6:13 you really want to keep working on it because it makes the separation so much simpler and these controller actions become ridiculously simple
6:22 so I encourage you to adopt it in your own app as well.


Talk Python's Mastodon Michael Kennedy's Mastodon