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