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

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Now recall over here, we had to pass this structure data to our template
0:05 and for our template to work we really needed
0:07 email, password confirmation and error always provided.
0:10 Moreover, we had some validation that we were going to do,
0:14 right here, like that the confirmation matches,
0:18 and we had this bit of code we had to write
0:20 to pull this stuff out of the POST dictionary
0:23 to get the data loaded from the POST into our method.
0:27 All that stuff should really be done somewhere else
0:31 and unified into a single, consistent style.
0:34 So I want to introduce you to this concept of view models,
0:38 now view models means many things in different situations,
0:41 what I am talking about here is we have specific classes
0:46 that bundle the validation and the data,
0:48 and how to get and convert that data to and from strings in the forms and so on.
0:53 And they are typically, not a 100% of the time but very often
0:56 there is a single view model dedicated to a single Chameleon template, single view.
1:02 Because what's in that view is often similar but not the same as other things.
1:07 They can create data the form might need to show itself,
1:11 so for example maybe you have a form and you want to say
1:14 "select the state" and you have a drop down full of states,
1:17 well where do those stated come from? They would come from the view model.
1:20 As well as the validation, but most importantly
1:22 they provide consistent data exchange,
1:24 so I am going to create a folder over here,
1:28 called viewmodels, and just to save us a little trouble so it won't be a major deal...
1:35 but let's put a few things in a common base class,
1:39 so we can call this whatever we want,
1:41 so let's just call it ViewModelBase, make it really obvious,
1:43 and it's going to have basically one method to dictionary.
1:47 And what we are going to do is we need to remember,
1:50 over here we need to return literal dictionaries,
1:53 if you try to return a custom type you would see it's not going to like it so much,
1:56 it's has to be a dictionary.
1:57 So what we need to do is take whatever class we are working with
2:01 and turn its data into a dictionary.
2:03 For custom types this is pretty simple, we can say self.__dict__
2:07 and a lot of times we can get away with passing this, if this is insufficient,
2:10 you can overwrite this method and then return the data in whatever for you want,
2:14 sometimes that will be necessary.
2:16 Now, remember what we are trying to work on is we are trying to work on a register,
2:20 so let's create a register_viewmodel, like this,
2:25 and it's going to be a class called RegisterViewModel,
2:29 it's my naming convention, you can use whatever you like,
2:32 we are going to derive from ViewModelBase,
2:34 and we'll let PyCharm pull that in, here we go,
2:38 we could import that, great,
2:40 what are we going to do, it's going to need to have some data,
2:43 remember, what do we have up here, we have... let's just actually grab this here,
2:48 and say "well, we want to store this data and we want to make sure
2:52 we have this", so let's just put it into __init__
2:56 and down here obviously this is not what we want,
2:58 we want to say "self.", let me just clean this up.
3:06 OK, now we have this data here, and if we call to dict,
3:10 we are actually going to end up returning a dictionary with email as a key,
3:14 because remember, __dict__ is basically where the fields are stored,
3:18 the attributes are stored, and we are going to have the value of None,
3:21 let's actually start using this straight away,
3:25 so if I come up here, we can say "I'd like a view model to be one of these",
3:30 we could import that at the top, and down here we'll just return vm.to dict,
3:34 remember that comes from the base view model.
3:37 Now, let's go and run this and see that everything still works.
3:44 If I go up here and click on register, it should provide that information,
3:48 remember when it didn't it crashed, boom, there it is,
3:51 email, password, confirmation, and so on.
3:55 OK, so now we've got a little bit of this managed,
3:59 this little bit of what that view needs to display itself, managed right here,
4:03 we could even inline this, like allocate this and call to dict straight away.
4:07 Next up, let's go down here and work on this part.