Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Remove the code duplication for Add vs. Edit

Login or purchase this course to watch this video and the rest of the course contents.
0:00 one final cleanup before we move on to the next chapter. Remember when we wrote this add_redirect piece here?
0:07 We then turned around where he said, We're gonna just copy the exact code and make minor changes
0:13 like change the name 'edit' here and here and then other change went from 'create' to 'update_redirect'.
0:19 Otherwise it was the same, and that wasn't great. So let's go through and clean this up really quick. So let's look at this one.
0:26 Now there's this part which is identical to this part. Remember, it's the view model that handles a lot of the setting things up and loading the data.
0:34 If there is a redirect that was passed to be edited and so on, there's nothing going on here.
0:39 But I'm not going to do anything with these two because they're already only two lines of code.
0:43 It's just going to complicate things to try toe centralized those. But let's go and write a function that does this.
0:49 I'm gonna copy this part here, and I put it down the bottom. We're going to create a function and it's gonna be called add or edit redirect,
0:58 and it's gonna take a request which is a request, and we're gonna drop that here.
1:05 This is gonna work fine for this one appear, cause I literally just copied it. But down here, we want to do same thing.
1:13 Let me just, we're gonna call update. All right, so we're gonna do the same thing here.
1:17 We're gonna return whatever this generates either an error or some kind of redirect or something. So how do we address this fact that we have?
1:26 Sometimes there's a thing being edited, and sometimes there's a new one. So what we're gonna do here is we're going to say 'if view_model.redirect_id'
1:37 So If there is a redirect ID than that means we're in this 'cms_service.update(redirect_id)' We could just check for this. It might be a little safer.
1:50 ID and then all these when you spell it right. That, else we're gonna do this and that's it.
1:59 So this way we can have the same basic logic where we redirect them to how we handle all this stuff, and we don't have to have that code duplication.
2:08 is it, Major no. Is it better? Probably a little cleaner Now it's only even potentially better if it actually works.
2:17 Let's go running and just make sure this works. Over here, we're gonna view our redirect and we want to be able to add one and then edit one.
2:25 So let's add our Google one 'G'. This will be https. Google And that's cool. We can test that it works. Let's see that. We can. Now can we edit one?
2:42 This is the other side of the story, put an exclamation mark here. And let's just change that to not HTTP
2:51 Google has this exclamation mark And if we click on test, well, we can't really tell. But if we go and edit it again now the S is gone. Beautiful.
2:59 So it looks like that works One other really, really quick thing when we test it, we're actually going directly to the URL here, as you can see.
3:04 What I would like it to do is actually really, really test typing in '/courses' or '/bytes'. So let's do that also.
3:14 All right, here this is super Super fast. So here's the test button. And right now the url is the full url, but we want is the short url.
3:23 We need to put a forward slash So let's go back and refresh. Notice this is '/g', ' /bytes', '/courses' But if I click, it works.
3:32 All right, well, those redirects and this little redirect cms backend, I think its in a great place.
3:37 And it's gonna be a really solid foundation when we move on to work with editing and creating pages.


Talk Python's Mastodon Michael Kennedy's Mastodon