Building Data-Driven Web Apps with Pyramid and SQLAlchemy Transcripts
Chapter: Client and server-side validation
Lecture: Viewmodel data exchange
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
One of the two primary jobs of these view models is to exchange data with the view. And the way it does that is with dictionaries.
0:10
We saw that dictionaries are passed into us and they're sprinkled throughout the request and Get Post Headers, etc.
0:17
And they're also passed back as the model in the form of straight, singular dictionaries that is encapsulated up in the ViewModelBase.
0:27
We have this __init__. We pass a request. And we store the request for the other derived classes.
0:33
And then there's certain common data that is always present and we don't want the individual view models to care or worry about setting it.
0:41
So, here for example, error, user_id, things like that. Right, this might be the outer view that needs it or it's just something so common
0:49
you're going to put it here. Well, then how do we take this information and turn it into a dictionary?
0:54
There's tons of ways. The most straightforward way and the way that automatically lets the derived classes take advantage of just setting fields
1:02
and then magically becoming these dictionaries that flow through to the views you just return the __dict__. And we say, here's a to_dict() function
1:12
and because it's on the base class it's on all the view models. Now the job of the concrete view model that is the ones that derive from this
1:19
like RegisterViewModel and so on their job is to set fields in their __init__ necessary for the view to get. And those will flow through here
1:28
and also to do validation. But the validation side doesn't appear on the ViewModelBase. If we look at something derived from the ViewModelBase
1:37
like, here, a registration view model it also has one of these __init__. It also takes a request. And the first thing it has to do
1:45
is make sure it passes that to its base class. So, it's going to call super and pass a request along. And that pre-populates all the shared data.
1:53
And then, we're just going to set a bunch of fields: password, first name, last name, email, and so on. Here you can see we're doing
2:00
some data cleaning along with it. So, we're going to our request and we're stripping out the first name if there's any white space
2:09
something that drives me crazy about websites. Like if you have a space at the beginning of your email address because you accidentally put a space
2:16
they're like, "Whoa, that email address is not valid." Not really, you could just strip it out, right? So, here we're doing that for all of our people.
2:23
We're normalizing that emails are always stored in the lowercase, things like that. And we're even computing full name.
2:31
Notice that we're using the default value of "", rather than None. So even if first name or last name weren't there this is not going to crash.
2:38
It's just going to strip an "" which is an "".