Full Web Apps with FastAPI Transcripts
Chapter: View models and services
Lecture: Benefits of using view models everywhere
0:00 Let's go look at our views one more time.
0:01 We're just about there. We've got our account stuff going on here.
0:04 Account views with their various view models,
0:07 and we've got the home view with it set up. And the package view going on
0:11 here. These are all good,
0:12 but if we look here, there's this one we left this TODO.
0:15 Remember that? Let's see one more thing to do with these view models that is
0:19 really, really valuable. So one of the things that you often want to do
0:23 is provide some feature, some functionality, to every single part of your application.
0:29 Every template. It's just gonna be common across there.
0:33 So let's look at a really straightforward example that's over here in shared.
0:38 I've got this login and register.
0:42 Well, guess what? What if I'm logged in?
0:44 Do I still want to see
0:45 login and register? No,
0:46 I want to see a link that says your account and a button that says
0:50 logout. Wouldn't that be nice?
0:52 So how do we do that?
0:54 Well, it's really super simple.
0:57 Over here in Chameleon, you just say tal:condition, and then what?
1:02 Like, is there a user_id?
1:03 Let's say that that's gonna be something we can get really cheaply because it's gonna
1:07 come from the cookies, which is just a dictionary,
1:09 not from the database, which maybe would be more work.
1:12 So instead of testing for a user,
1:14 we're just gonna test for the user_id.
1:16 Maybe a better way would be,
1:17 say is_logged_in, are logged in or has_account or something like that,
1:24 right? Make it a little more abstract.
1:25 That would be nice to have.
1:27 We'll say that we don't want. It's gonna, It's not the case.
1:31 And if it is the case,
1:33 then we want to say, change these around a little bit.
1:36 So if you are logged in,
1:37 we wanted it to go to just /account,
1:40 and this is gonna say "Account" and this is gonna be/account/logout
1:49 and down here it'll say Logout.
1:52 That's great, right? Let's go and run it.
1:56 Mmm, not as great as I was hoping.
1:58 Probably not as great as you were hoping either.
2:00 What is the problem? without looking. It's
2:03 NameError: is_logged_in not found.
2:07 Right there. It doesn't know what the heck
2:11 that is. So what we do is, we go to our base view model and
2:19 it always pulls this and sets this. I'll say self.is_logged_in = False.
2:25 And then maybe a comment, right.
2:29 We'll set that from a cookie.
2:30 And it'll be really straightforward. So what's gonna happen is, the view model,
2:35 anything that is either this or derives from this is going to have that information.
2:40 So every single view now should have that info there.
2:44 And what is our problem? Oh,
2:46 I said case, didn't mean case.
2:48 I meant condition. Somehow I got that,
2:51 uh, condition. Sorry about that.
2:52 I'm sure you noticed that and you were like: what is going on here?
2:55 Condition, I said condition, but autocomplete gave me case.
3:00 There we go. Again, perfect.
3:03 Now, right now, we said
3:04 there's nobody logged in and notice if I go to any page on the site.
3:08 Always working, go over here to our ViewModelBase and just say True.
3:13 And obviously you wouldn't just hack that and say,
3:16 of course, you're always logged in.
3:18 What you would do is get that from user.
3:20 But check it out. Now we have Account and Logout.
3:23 Have it over on the home page,
3:24 we have it on this page and so on.
3:26 But if we don't use the view models everywhere,
3:31 remember our about page? Crash! Again, all of a sudden,
3:34 what is going on with our site?
3:37 Well, here's that NameError that I was telling you about. In this particular view,
3:41 that information our shared layout needed, didn't get what it was expecting.
3:45 So it crashed, right? So these view models provide a really nice way to
3:49 send that information to everything as long as everywhere you use it.
3:54 And if you don't need any details,
3:55 we can just say return the to_dict() or just the base,
4:01 right? We didn't have to go create,
4:02 like, an AboutViewModel here,
4:04 but by using it everywhere. This is working great.
4:07 There's other ways to do this,
4:08 the view models provide a really nice mechanism.
4:10 And if you're using them in most places,
4:12 just use them everywhere and you'll be good.
4:14 So back here that works, this one works,
4:16 everything works. Great, just one nice chance to show how this ViewModelBase provides
4:23 common data and common functionality for the entire template engine.
4:29 Well that's it for view models.
4:30 I hope you found this way of
4:32 partitioning our logic and data exchange into view models and services really nice way to keep
4:38 things like our actual view methods really clean. Create some
4:43 testable little classes that we can test in isolation and provide common functionality to the entire template engine.