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