Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: Viewmodel base class

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The key thing to wrap your mind around as your work with these view models is that the data exchange is deeply tied to the HTML template, and what this
0:11 view method has to pass to it. It's going to be solely the job of the view model to know how that data is exchanged, how it is being passed back
0:20 and what kind of validation we need. So it's all about this data exchange and validation. What you'll see is that every view model
0:26 is different and it's in particular tied to this template and this method. However, there's certain common functionality
0:34 that we have to use, like every single template has to have this for the shared layout. We'll also see that most templates
0:42 and anything that has a form really has to have some kind of error message like this as well. So, these kind of things will be great
0:50 to put into a base class along with some of the behaviors that every view model's going to need. So let's go and do that real quick here.
0:58 Create a folder called viewmodels. Because very much like our templates here we have some organization, like this account has these three views, right?
1:09 CMS style also has an index but that's not the same as an accounts index, right? Very, very different. This organization equally applies
1:17 almost exactly over here because remember the view models are tied to those templates with that very same organization.
1:24 So let's go over here and create shared one and over here and create an account one. So the shared is where we're going to put our base class.
1:36 Called ViewModelBase. ViewModelBase, like so with no, nothing derive from it. And it's going to use some stuff from Flask as we'll see.
1:48 So what we're going to do is have an initializer, a little constructor here and in here we're going to store the request.
1:57 So we're going to get the request because we need to do certain things with it and we could also define this as a request
2:05 from Flask, so that when we say self.request. we get all sorts of good stuff there, all right. The other thing we might want to have
2:12 is we might want to have a request dict or something like that. Remember we had for this request dict library that we created
2:22 that will unify where all the data is coming from so we want to take that and say create and we're going to give it a default value.
2:32 I guess could be like this. And we don't have any route args here not yet anyway. So that's going to go and create our dictionary here.
2:40 And then we also have some common data we had our error which is an optional from typing, Optional string that's equal to None.
2:53 We have our user_id which is an optional. And this one we can go to our cookie_auth. Here we're going to say get_user_id_via_auth_cookie
3:08 what's it going to take, it's going to take self a request. Perfect, so that's going to give us some back, either the user, or not.
3:16 And then we'll see that classes can derive from this ViewModelBase and they can add additional fields. This is great but, what was happening over here?
3:24 We were creating a dictionary and sending it back. Well, we're going to want to be able to convert from this class from an object, into a dictionary.
3:34 So let's add a little function here as well. To dict and it's simply going to return the simplest possible thing for now.
3:42 It's just going to return the dictionary that backs the storage for all the fields in the class not for the view model base
3:49 but for the instance of whatever it is. So a extra view model, or a package view model whatever the things are that we're going to create.
3:57 So here's going to be our base class and this is going to provide a lot of functionality for working with this data exchange
4:05 and the validation and so on. However, to really make it useful we have to create individual, specific ones tied to these views
4:13 by creating classes derived from our ViewModelBase.


Talk Python's Mastodon Michael Kennedy's Mastodon