Adding a CMS to Your Flask Web App Transcripts
Chapter: The redirects (our bit.ly)
Lecture: Demo: creating the ViewModel

Login or purchase this course to watch this video and the rest of the course contents.
0:00 let's begin exploring this idea of a view model by improving this with RV model. Now I want to be really clear and upfront here.
0:09 This is not that important of a case to apply this pattern here because it's not that complicated. But when we get into the CMS side,
0:18 where we're creating things and editing them and there's get methods and post message post methods and there's all of data being exchanged in the form,
0:27 it gets super complicated with the validation and data exchange and conversions and what not So that's where the real value this will come from.
0:34 But let's take a super simple crack at it here first. Now, remember how we talked about the organization of the views in the templates.
0:42 So, for example, we have our views than account views and say, in here we would have like an index or a register big over our templates.
0:52 We haven't account Holder, and then we have a register and we haven't index and so on. A few models are gonna be organized in the same way when have
1:03 for example and account folder and an index view model in a register of your
1:09 model just give you a sense like this is a decent amount of validation happening over here. In order to add one for our CMS,
1:16 we want to create that same structure. Let's go create a directory called CMS, and then we could just create a blink.
1:23 Pylon file is not that complicated, but there's a few things going on about driving from a certain based type to share
1:30 things. That's one. So it's easier to just copy one and then change it to this. So this is gonna be the CMS request view model.
1:37 And I was calling request view. Monica, that's in the CMS folder. Okay, Great. Now what we want to do is we want to go back
1:48 over here and see what's going on. What's happening? We wouldn't simplify this a bunch. So we're gonna be getting the page or checking that.
1:56 So let's copy that over. Okay, This stuff, we want this and we need to import peace, and we're gonna have to get the fool you Earl from somewhere.
2:10 The way flats works is that's being passed in. So we're going to want to pass it and to review models. So let's get started. Like this.
2:19 Come over here and say this is a request view model. Now if I hit command space notice, nothing is happening. Pyjamas, as I have no idea.
2:27 We're talking about What if I had a second time, it says, Oh, actually, if I look more broadly, would you like me Toe Do all the code to
2:34 import that and write the import statement up here? Yes, I would. Thank you very much. Let's go over here and pass the full you,
2:41 Earl. And we can change the signature here so it automatically adds this if we want or it's just honestly is quick to just go type it.
2:53 But now if we jump back over here, you can see there's the girl like this. So it's store that you are all actually and then also store the page.
3:07 Well, something similar for the redirect. Get it from there like that with the euro.
3:16 And then remember, we also have this funky thing we were doing with the redirect
3:21 URL because maybe want to keep the query string so we could set that hearsay self that redirect. You're l equals for the moment.
3:30 We're gonna say it's nothing, but if there happens to be a redirect. Then we'll go work with, say itself, not redirect. And then I'm just gonna copy.
3:37 This could cause you know what? It's the same, like this import flask. OK, we'll come down here and say the destination is going to be that The
3:49 question is this the final destination? It's this and we'll see itself, not reader act. Euro equals test.
3:57 Or we could just in line that however you want to do excellent. So this is the code that we need to write to move to a simpler mode
4:06 Now, why is this better than just sticking over here? Well, when you get to the CMS side, that would be massively complicated with all the validation,
4:14 all the data exchange and whatnot. So it's going to make our life much, much better. But also imagine weaken,
4:20 Just test, create one of these classes and just test it right. It would be a lot easier for testing.
4:26 Enough did lean so heavily on the flask framework and so on. Okay, so let's put this into use instead of that.
4:32 We don't need that anymore. We'll just say if you have not page wherever we had Page. We gotta stick this bid. Then we have. If there's a redirect,
4:47 all of this is gone. Where is going to send you over to via not redirect your ill? That's it.
4:54 A little more cleanup. Look how nice and simple dysfunction is now. You don't even have to give it like comments or anything to say what's happening.
5:02 You get a request, there's a page, you show the page and this went the way. We're gonna do something better soon.
5:08 If there's a redirect, you take them there. Otherwise, for four pretty awesome. It would be awesome if it works. Well, let's give it a quick test.
5:15 If it doesn't work, it doesn't matter how clean or nice it is over here to make sure our site still working and go to the old places.
5:24 If we go somewhere, it doesn't exist. Oh, no, This one doesn't. This this is one of our pages, right? We go to one that doesn't exist.
5:32 We get a four or four. And if we go to somewhere like we had before, like bites with our query string and everything perfect,
5:40 it's taken us over there. Let's just one more time just make sure without a crease string, we end up on the other side correctly.
5:48 All right, that's this is really nice. One really quick comment here. You might look at this and think OK, well, this is an improvement,
5:57 but it's actually hitting the database. Quote database. Eventual. Have a real database. It's hitting the database for a page in a redirect every time,
6:05 even if there is a page. So it's a little bit of extra overhead. Whether or not you want to deal with this really is up to you.
6:12 It may be like one millisecond. It doesn't matter. Who cares? But if for some reason we wanted to re factor this,
6:18 we could come over here and create a property called Page like this in on Lee we went on to set us, but only go and do the database thing.
6:35 If somebody actually asks for that, we could do the same for redirect. Maybe call it something like that and we don't need this redirect.
6:45 You are ill. Yes, Will you? Let's go over here and just get this and we'll say self not you are ill
6:52 Return this and then the last thing of a property for the redirect Turell as well. I'm not necessarily advocating this. I'm just saying. You may,
7:06 you may care, Are you may not care about this. If there's a redirect, then we're going to go and compute this. Don't need a story like that.
7:17 Will just say return. URL or destination. Perfect. Okay, I think this will probably still work. And this way we're on Lee,
7:32 really computing the page of the redirect if we ask for it. So, for example, if there's a page or not gonna ask for the redirect
7:40 now, I don't think I would go and do this, but, you know, maybe things were slow. So let's go check. Donate.
7:45 The page comes back. Hope for a four and bites takes us there. All right. Works the same way. A little bit more work. Now,
7:55 of course, we may be calling this twice, so we want to put a little cash in here, right? There's there's layers of what we're getting ourselves into,
8:02 but I'll stop here. We just give you a sense that we could make this a little bit better if we actually cared, too. But I think honestly,
8:08 I would have written the first one and just been happy with that, because most the time someone's going to request a page it it's not that big of
8:17 an overhead. So anyway, we've got a much cleaner CMS request view method here
8:23 by using this view model and you see weaken rework how our internal view model is
8:28 working. Teoh optimized for different things clean code performance forever.
8:34 We also test it more easily in isolation outside of the full Web requests of flask
8:40 and saw. All right, there's gonna be a pattern that we're gonna use as we get into our CMS, especially the view edit data type of scenario.
8:49 We're going to see a lot in the CMS.


Talk Python's Mastodon Michael Kennedy's Mastodon