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