Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Editing an existing redirect

Login or purchase this course to watch this video and the rest of the course contents.
0:00 we have our add redirect working, but it's time to edit as well. Because what if we make mistake? What if we want to change our minds?
0:08 So for over here in our admin section in the redirects, we click on Edit. Well, ah, 404.
0:15 And what we're trying to go to is '/admin/edit_redirect' And the ID of that object. Okay, well, that's pretty easy. We should build to do this.
0:24 We've already done it for adding we should be able to edit it.
0:26 There's actually not that much difference. Now noticed over here. We have an edit re direct route already.
0:32 And it's going to be that one that we saw pass along is gonna come through is the redirect I d that we've already kind of been working with.
0:39 So let's go and work in that here. Now I'm going to do something terrible and make it better.
0:44 Terrible thing is, I'm going to make an exact copy of this and just repeat it and rename a few things. We're going to name that to 'edit'.
0:53 And why is this terrible? Well, all the code, the order hiding down here is gonna be an exact copy in both places.
0:59 That's not great. But there's so much similarity that let's just start this way and I'm gonna improve it. So here we go. Edit, and edit. Edit.
1:09 Take that away from both of them. Actually, you don't need that would process. bit anymore.
1:12 Over here, instead of 'create', we're going to call 'update' the redirect, and we need to give it one more thing at the beginning.
1:21 The redirect ID. Because we need to find it by the ID and then make a change to it. Okay, So what's going to write this function first?
1:29 Gotta write, Write them somewhere. So, yeah, redirect ID names. short url You're all these are all good.
1:34 So how are we gonna find the one by ID? Let's go look at our data real quick.
1:39 The each have an idea that gets set here, so it's not gonna be very efficient when we get to the database.
1:45 It's gonna be a lot better, but we just do this And we'll just do a quick little sort here. Going through all the items. So we'll say,
1:53 'for k, v in big_data_redirects.items()'
2:00 So this will give us the key, which will be the short url, and the value, let's change that to 'r' for the redirect.
2:06 Now, on that item, we're gonna have the ID. So here will say, 'if r.get(ID) == redirect_id'
2:17 And redirect is going to be just that thing. That's perfect. And we can break out of our loop.
2:24 Super and then down here instead returning 'None' we'll just return 'redirect'.
2:29 Alright Well, that looks pretty good. You could even give it a little test down here in a Python console. Actually Let's do this in two parts here.
2:38 let's have the get_redirect_by_id.
2:47 That's this one here will say get_redirect_by_id. 'if not redirect: return'. just out of here. There's nothing to do.
3:01 Otherwise we're gonna say redirect of name equals name and just do it for all of them.
3:19 There you go. That should do it. We're gonna get it back, make those changes to it and just stick it back in the database, the "database".
3:25 Our little in memory thing that we're working with until we get to storing this in the database better.
3:30 All right, Now, let's go over here and restart this. We'll get it by ID. We have a one. Look that. Perfect! We have a two. There it is our bytes.
3:43 Do we have a three? No, we don't. Give us nothing. Excellent. Okay, so it looks like our little get that is changing. So perfect.
3:51 We're still calling up here. Update by ID. This should work pretty good. Now, surprisingly, that might be all we got to do for this part.

Talk Python's Mastodon Michael Kennedy's Mastodon