Adding a CMS to Your Flask Web App Transcripts
Chapter: Saving content to the DB
Lecture: Saving a Redirect

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we have our redirects modeled, let's go and use them. We follow this really cool pattern as governor of use
0:08 to our admin section here. We're adding to redirect. We're using our view model, get the data and a validated, and then we're just calling CMS service.
0:16 Create redirect and passing the information. So what you'll see is we won't actually have to do anything to this code to
0:23 make it work or just going to go over here and change what this services that's
0:27 ideally services. They kind of hideaway how you talk to the database or talk to like an A p I or whatever,
0:34 and you don't usually have to change too much beyond just that that's here. So let's go and look at the various redirects.
0:41 We're not gonna worry about pages for the minute, and we've got I think five functions. Get a single redirect. Get all redirects,
0:49 get one my i d. Great one and update one. Yeah, that's it. So let's go work on Those will save update for a little bit later, but let's work on,
1:02 for example, this one. Now all of these are going to follow the same basic pattern as gonna be quite simple.
1:08 What we do is we create a session by going to D B session. I'm gonna import that and we say create session.
1:16 Remember that that returns one of these session objects. Then the easiest way to do this is we want to do a query,
1:26 return a value and make sure that we close out the session. He's this way to do that. Say, try. And then what are we looking for?
1:33 Gonna redirect here? It's in doing this. You see, It's almost just is easy to talk to. The rial database will say Sasha and not Queary locates.
1:42 Query of a redirect had to import that. And this will give us all of them.
1:49 We don't want all of them went of filter where the where the redirect thought short euro is equal to We called it the base.
1:57 You're all here and then we're gonna return that and then we'll get say, finally session dot clothes. Okay,
2:07 now this technically will give us a collection of redirects we know because of the uniqueness constraint, there's one or zero.
2:14 But in terms of query syntax, we've got to express that so we could come over here and safe first,
2:19 and it will either give us the first one it finds or will give us none If they ask for you, Earl, that has no associated redirect.
2:26 We can even make this a little simpler and just in line it like that. So that's it. That's all we gotta do to talk to the database.
2:33 Now let's go over here and say, This doesn't return an optional dictionary.
2:37 But it now returns a redirect because we were working with dictionaries before, and now we're working with classes.
2:43 We have to make very minor changes like there were a few places where we might say, Redirect on, get Earl and we have to say redirect dot euro Right
2:53 so it is not hard, but there's a few minor things we got to dio. All right, so let's just because this is so short,
2:58 let's just go knock it out for the other ones. This one is even simpler, and we're going to create this And what are we
3:04 going to return? Well, first of all, the type we're specifying as a list of Reader X and here we don't need to
3:12 do a filter, but we're gonna want toe show The newest ones might as well put them at the tops. Will say order by reader act.
3:21 What are we gonna order by created date? We put it like this, but like this is going to give us the oldest
3:30 ones first ascending. We're gonna go in and say we want the descending version and then make it a less It will say off and that's it.
3:40 Okay, let's just keep rolling this one. Because the way we're storing their little fake data,
3:46 this was a real mess and actually gets a whole lot better over here. And we're going to actually let me copy the prior one this one,
3:55 because it's basically the same. Get redirect my i d. We're gonna go down here, and we're just going to say the I d is
4:03 the redirect. I d not Be careful. This is to equal science that just my fault makes it look like one.
4:08 But it's too. Do in a community or say give us the redirect where the I d matches first done. And I guess we'll do create as well.
4:17 It's going to do that when a little bit different. So for our data, what we're gonna do is we're actually gonna create the redirect
4:26 as a class like this and we just set the property. So we say redirect, We don't set the i d as automatically done.
4:32 But we will set the euro to the Orel in the short euro to the short euro in the name do the name. And then what we gotta do down here?
4:47 As we just say, session dot Add redirect session dot Commit. I don't save it back to the database.
4:56 This we don't need. Go ahead and keep our check here. That's cool. It also go ahead and put a strip on these to get out of the white space and a lower.
5:11 So they went to worry about that. We're really doing this other places, but you know, it's good to check it at this layer as well.
5:18 And that's it we're now creating. Redirects were getting them back by D. We're giving them a list of all of them and so on.
5:27 Do this again needs to be a redirect like that, and let's see what we're doing. Is this return anything You know,
5:33 we might as well return the redirect right? How we created. If someone wants to work with it, they can get a hold of the idea and so on.
5:44 And it looks pretty good again. We'll come back and fix that one in a little bit. Let's start by seeing if we can just add a new one to our site right
5:55 now. 100% sure this will work, But let's give it a try. It looks like I've got a log in again.
6:01 Oh, here. Now we shouldn't have any redirects anymore because there's none in the database and we're not talking to are fictive.
6:07 So let's try to add the Python bites one back again. This was just bites, and this is I don't buy it Set off them,
6:23 All right. Remember, what it's gonna do is gonna go to that method. That view method is gonna go to the service and say,
6:29 Create one of these and then redirect this and get the list. It's might work. Let's get her try. No, no, no, no, no, no. Look at that.
6:39 Well, we said in our data model that you must specify who the creating user is, and we didn't but we should have.
6:47 Let's go like that. Really quick change here. I forgot to do that because we weren't doing it before. So here, let's pass in v m dot user,
6:57 not email. The reason I know that it has this this user exists is because you have to be an admin to be an admin. You have to at least happy logged in.
7:08 So let's go over here and say this takes a an email. Use your email or some like that redirect dot creating user.
7:26 Is this all right? Well, checks to make sure that it's enforcing State of Model doesn't let's try to just submit
7:32 that form again, this time passing over the user. Yes, okay, that it worked. Let's add the other one. We had courses. I think it was. Uh,
7:46 hold on. We've made a mistake. We're not gonna be able to fix it because I haven't written edit. That's OK. We'll just test this one.
7:52 I didn't need to put that slash their We should probably write some code that will just take it away if you put it there.
7:57 But on no problem. We could almost go there, but not quite. Remember, I told You have to replace some things where we're
8:05 doing don't get with just like dot, get of title or just want title and that's what's going on. But at least in terms of having the redirects here,
8:14 this is working and these aren't in the database. If we go over here and look in the database, will be there. Notice. Here's the ones we created.
8:23 I could go and fix that now, but of course that's not the best way to do it, but it will fix it. All right,
8:31 so here's our redirecting, even see the Created A Cup. But in the end of the creator user, all the stuff that we need perfect.

Talk Python's Mastodon Michael Kennedy's Mastodon