Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Loading initial values in viewmodel

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's go over into this and write this bit of code again. Now, in the add case, there's no data initially provided.
0:01 But in the edit case, remember, the URL is passing over the ID of the redirect we want to edit.
0:01 And that means we can write this or maybe that rather and this will be self.request_dict. So this is going to give us the ID if we're editing one.
0:01 Otherwise it's going to give us none just like we had. And here we can say, if '' then we want to set some more values, right?
0:01 We want to get the id and 'self.redirect'. I guess we could go and just get this. Let's say CMSService.get_redirect_by_id.
0:01 Let's make sure we're checking that this, if it's empty, redirect by id. 'if not redirect_by_id: return None'.
0:01 Alright, if we pass and nothing, we expect nothing back. Just so we don't have to check it.
1:01 Here you will get the redirect and we'll just say if there is a redirect, we want to actually copy those values
1:01 over so they can start from seeing those values and then edit them. So this will be short URLs and the others are going to be similar.
1:01 All right, get those And the redirect url, redirect ids are already set. Perfect. Think this actually might already do it?
1:01 There's a few little things we're gonna have to touch up later to make it perfect, but this is really close. If I got it right.
1:01 Well, let's go over here. Go to our admin section. View the redirects. We've seen we can already add them. Let's go ahead and just add test...
1:01 Oh, I'll add my little talkPython one back. Right. Here it is. Let's suppose we can edit it. All right, Well ah, didn't totally work.
1:01 We have our edit redirect five here.
2:01 Uh, why is this not coming up? Good time to check out our debugger isn't it?
2:01 Make sure that's hitting the right one. Yeah, it's coming here and you see, here is the URL. Everything looks fine with that.
2:01 Redirect ID? It's five, okay. You know what I think? I think it's something super tricky. Watch this. It's going to be frustrating.
2:01 Okay, so we come in here. If we look at this, you can see there's ID but notice there's no quotes on the 2.
2:01 There's just the value 2. More relevantly, if we go further. You know, we just look at the redirects.
2:01 Expand it. The items all have numbers, not strings.
2:01 So what we're doing here is we're comparing a number to a string which is never okay, Never gonna match. So let's just do it like this.
2:01 Alright, I think we should have this working. Now, let's go and do it again.
3:01 Over admin, let's just edit one that exists now, so it's edit courses. Oh, yeah. Look at that. There's the courses.
3:01 This will be, I'll put talkPython courses, for example, that should when I hit, 'create'.
3:01 Notice, this is one of the little things we gotta work on. It will push that back to the edit should change this, let's see.
3:01 Oh our validation is kicking in. Yes. So let's go and lock that.
3:01 Down here we're saying if we're trying to create one, then we got to check that it already exists. We should say, 'if not self.redirect'.
3:01 And this maybe like that. so 'self.redirect' What could say that's the best way to make this clear?
3:01 We also need to say this if there is a redirect and it doesn't exist, redirect with URL was not found.
3:01 So if they're trying to create a new one, we need to check for one like it already existing.
4:01 If we're trying to edit an old one, it had better already exist. Okay, let's try this again. Resubmit our form. Maybe it'll work this time.
4:01 Yeah, it does. And check it out. It's edited right there. Let's edit it some more. Let's just put all those classes.
4:01 Oh yes, we need to dio slightly different tests over there. Let's just edit where it goes. Question mark equals true or something like that.
4:01 Alright. Now, if we test this or even look at the bottom, we've edited it.
4:01 Beautiful, beautiful, beautiful. Send us over there and the title is perfect. We're able to edit that.
4:01 The one thing that we saw was that we're not yet able to edit this. We're somehow not relying on that ID, which doesn't change ever. Enough!
4:01 And so let's do this will say 'get_redirected_by_ID(redirect_ID)'.
4:01 That's the one we want and what probably should say down here with ID whatever it is.
5:01 All right, Last chance. Let's see if we can edit that URL that short euro in our thing. Otherwise it's working.
5:01 OK, here's the redirects. So let's make this course is exclamation mark and, ironically, we'll call this "classes".
5:01 and we'll edit this as well and say question mark final equals Yes. All right, can we change it so that we can redirect through '/classes'?
5:01 Woo! It's looking pretty good. Here's the final euro, but let's the real test is typing '/classes' up here isn't it. '/classes' Ready? Course it works.
5:01 Beautiful, our little CMS backend, while it took a little bit to get it up and off the ground. Maybe took a few minutes.
5:01 This is the same thing we're gonna do for pages.
5:01 This is like the blueprint for everything we're going to do in the rest of the class in terms of our admin backend.
5:01 But we're going to extend that, of course, with rich editing and all sorts of cool stuff. When we get to pages, but there it is.
5:01 We were able to go over here. Go to our admin, and now we can edit our classes. We could even put it back to courses.

Talk Python's Mastodon Michael Kennedy's Mastodon