Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Saving content to a database
Lecture: Inserting a redirect

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now we have our redirect sequel. Alchemy! Entity, Class created. I think we're ready to start using it. Now.
0:08 We use this cool pattern of having services that isolated, accessing and talking to whatever type of data or dealing with the data structures for the most part.
0:18 And so what that means is we have four functions over here. Get a redirect all redirect, create a redirect, and then get one by I. D. Owen also update.
0:27 So I guess that makes it five.
0:28 We have these functions, and all we have to do is rewrite these for the most part, and we want to touch any other part of the program.
0:36 The only caveat is we didn't go and create a class. We had dictionaries here, So there's gonna be a very, very small thing.
0:43 We have to change for that to work, you know, like the replaces always say dot get title, just say dot title when we switched to these classes.
0:51 But other than that, we won't have to change how our program works at all, which is awesome.
0:56 But let's say this returns a redirect year, and actually it returns an optional redirect reason.
1:04 Optional is we don't know if the euro you're asking for exists in the database, so you might get nothing or you might get the one redirect you ask for.
1:12 The other thing we're gonna do here is all of these data accessing parts are gonna have exactly the same thing when increase this session, which is going to be a one of these.
1:23 It's equal alchemy sessions and the way we do as we say, DB session that import that got create, then later we're gonna say session dot clothes or even when we're making changes commit, then we're gonna return something.
1:38 So it here going to say the redirect is equal to something. I'm going to return the redirect. That's pretty much it.
1:46 This is gonna be the pattern that we're going to go for all of these. Someone good in here.
1:53 Make that plural reader Rex redirects create one of these Return of the redirect Singular. Okay, Super. Now we also need our create session.
2:09 And hey, while we're on it, let's do it more here. Okay, So let's just right. The top one here were coming in getting the Ural on normalizing it.
2:22 That's great. Now we have to do this query.
2:25 So the way we talk to the database and sequel alchemy as we say, session dot query of the thing we want to get We want to get redirects.
2:32 Then we went to filter where the redirect dot What are we asking for by your URL so short your l double equals, uh, what would pass as euro?
2:43 This will give us potentially many of them back all of them. And have this Our model is we only have one.
2:49 And maybe maybe we should have gone over here. We should have actually said unique equals. True, That would be good, right?
2:58 So we should probably add that, But when I got to do some migrations and I'm not gonna mess with it but in yours but unique it go through because there should be only one.
3:05 So this will basically be one or zero away. To say that it's equal alchemy is to go to the end and say first.
3:13 Okay, If you knew there was gonna be one, you could say one. But if they ask for your old, it's not there, it's going to crash. So first and that's it.
3:21 We're now talking the database getting are redirected back here. Well, let's take this idea and just keep going. Okay?
3:28 So it'll be redirects, and it's gonna be a list of redirect. We say session query of read Rex.
3:36 And maybe we just wanna give them back newest and first or something like that. We could order by redirect Thought it'd date.
3:44 That will give him oldest to newest ascending. So we want deaths ending like this. Beautiful. All right, so that's written. I hope you see these air.
3:53 Not super hard to convert, are they? Well, don't hear this one. Takes a little bit more. We're going to create a redirect. We're gonna set his values.
4:02 Not set the i d. But redirect dot your URL equals your URL direct, not short. Your l equals you Got it.
4:10 We're just gonna keep going like this name like so.
4:15 And then we're going to go to the session and say, had the redirect to the database and then commit those changes and give it back, and that's it.
4:25 Perfect. Now we come in here, we could say on these we could say strip, tell them what they are. I can see that our strings.
4:38 This one is not strip, not lower one is strip.
4:42 But when a short locates the name, just make sure there's no whitespace, no spaces or tabs or anything stuck in there.
4:49 Now you might want to add a little test for these. They could crash.
4:52 Technically, the editor should check if these air coming in as none and not allow it. But that's not a runtime thing.
5:00 That's just a suggestion that we should probably put testing here. But for now is gonna leave it like that.
5:05 We do have the required stuff on the view model side where we make sure that those things are all set that they're required.
5:11 So it's pretty, pretty safe assumption. Now hear, this is quite similar coming in. We're gonna get the i d.
5:18 Now this is gonna end up to be an integer like so. And instead of going through and doing this loop, well, guessed it.
5:27 We're going to do something like this. Get it in. The redirect is gonna be session.
5:34 We re a redirect when a filter it I want to say the redirect dot I dy Biggs to the redirect idea that we passed in not first. That's it.
5:48 We're going to tell this one. It gives back an optional redirect, which, of course, it does. Well, these air close.
5:57 Let's work on this one here in a second the update, but it looks like the rest of them are in pretty good shape.

Talk Python's Mastodon Michael Kennedy's Mastodon