Python for Entrepreneurs Transcripts
Chapter: Build web apps with Pyramid: Applied web development
Lecture: Concept: View models
0:03 Now that you've seen view models in action, let's review them in a picture.
0:07 So we've seen that we can have these view models, classes really,
0:11 in this case we have a RegistrationViewModel that we've created
0:14 and what we want to do is use this type to help alleviate some of the challenges
0:18 of sharing and passing data back to the views
0:22 as well as validation and getting it from forms.
0:25 So we are going to have two methods here, two actions,
0:28 one is a register, which is handling the GET request
0:31 and one, which is going to handle the POST or the form submission.
0:34 Here what we need to do in this register_get is
0:36 we need to return the data that will be displayed in the Chameleon template,
0:41 so we probably have to have an email and a password and things like that.
0:45 Our RegistrationViewModel is a class that has those properties or fields
0:50 and we write a to_dict method that will turn that to a dictionary, basically
0:54 a model as far as Pyramid is concerned, to be passed back.
0:58 So, this is really nice and simple and this might even be doing data access
1:02 in the constructor for the registration view model, right,
1:05 it could be doing lots of interesting stuff to get that data, that model prepared.
1:09 Next, we want to handle the form POST,
1:12 now we have the register_post and how do we do this?
1:14 Again we are going ot create our view model, and now we have a from_dict,
1:18 so given a dictionary which in this case is coming from the form POST,
1:22 let's parse that data back into our variables,
1:25 in our registration view model and then we'll be able to work with all those values
1:29 as if they were just present in the view model.
1:32 We also kind of add validation, so here we can call a validate method
1:35 once we've restored it from the dictionary, we can check
1:39 and even use this error field that we've added,
1:42 so if it happens to be there is something wrong,
1:44 we don't want to accept their form POST,
1:47 in this case we don't want to let them register for the site,
1:49 we can just return the view model to the same template and this will round trip
1:53 all of their data they have entered as well, so this is really nice and simple.
1:58 If we do validate, then we'll work with the various fields on that view model,
2:03 so view model email address, view model password and so on,
2:06 once we actually create the account and do the login and so on,
2:09 then we might raise an HTTP found exception
2:12 to redirect the user to wherever they need to go.
2:15 Remember, the GET/POST/redirect pattern.
2:19 Here it is, with view models.
2:21 Let's look into this to/from dictionary concept a little more deeply.
2:25 So what we've done is we've realized
2:28 that basically any view model needs to be able to convert itself to a dictionary
2:31 so it can be passed to the templates,
2:34 so we've created a ViewModelBase so that it has
2:36 the simplest possible implementation of "to dictionary".
2:38 it just returns all the fields, if for some reason your particular view model can't work this way,
2:44 just overwrite this method and create a dictionary and return it.
2:47 Now we have our various page-specific view models like a registration view model
2:51 and it derives from this ViewModelBase and it has a __init__
2:54 where it creates this fields, so above when we say "to dictionary", we get
2:58 an email address, a password, and an error passed to the view.
3:01 Optionally, if it's supposed to support a processing of form post,
3:06 we can add it from_dict as well, not all of view models need this,
3:10 but this particular one did.
3:13 And so here you can see we are passing in a dictionary
3:16 we don't really care where it came from,
3:18 and the case we just saw it from the form POST but it could come from the variety of locations
3:21 and we are going to use the GET sort of the safe dictionary key access
3:25 or dictionary value access to get the email address and password and store those.