Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Listing the redirects

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next up on our CMS is we want this link to work. No, no, no, not 404. We want to see all the redirects
0:08 So we're going to implement this /admin/redirects, Let's go over here and duplicate that real quick.
0:14 So this will be admin, Remember, already have these here. So let's go see what we call them. Just redirects. Gonna be /admin/redirects like this,
0:24 and we'll call that redirects as well Go and work on the template first, I guess. So Copy/Paste Call that redirects. Here will say redirects,
0:34 and for each one of these, we're going to list all of them. Let's first actually put the ability to have a new one.
0:40 So we'll have a little linked to admin add redirect, I guess we'll call it. I'm not gonna write that first, but might as well put it in the template.
0:50 And then over here, we need to have, all of the redirects. So have ah list element a nd inside of our element, we're gonna have a couple of things
0:58 We're going to have a button to test it We're gonna have a button to edit it when have the short URL and then the name
1:06 So let's go here and put a button. This will be admin/edit/redirect We're gonna need to put something in here. We'll have edit.
1:16 Let's do test first, do edit second. Put something in there. How do we know where the URL is, where we're gonna test it to go?
1:24 But for the test, let's have it open a new page. The target equals blank, that will open a new tab for us.
1:32 Over here, we're gonna edit it, and then we're gonna have a span, couple of spans. Let's put the names. Put the url and then the name.
1:42 So how do we know what pieces go in here? What we're gonna do is we're going to make a copy of this for each redirect in the database,
1:50 and we're gonna pass it over and use this colon repeat, You want to say 'r for r in redirects:'
1:58 I'm gonna pass all the redirects over here, and we're gonna clone this and use the data from here, so it's gonna be to get the value out of it.
2:05 so in chameleon, you say that. You say r.short_url Oh, no! That's going to be just url. Let me copy this one. We'll need this in a sec. It'll be r.url
2:21 I'm gonna start with short_url but it really needs an ID. We'll give it that in a little bit.
2:27 I guess we could just... Yeah a short URL is gonna be what we're gonna give it for now. That is the short url, and then this will be its name.
2:34 Let's go look at the data real quick. When this service, we're going to this fake data.
2:40 But it has a name, a short URL, and a URL and this is going to be what counts as the ID. All right, Well, actually, that looks pretty good.
2:48 We go back over to our admin thing. We need a redirect list view model which doesn't exist yet, so we're gonna go and create one.
2:57 Let's put that away for a sec. Now it's time to add one of these admin sections over here. And again, the easy thing to do is copy and paste.
3:07 This will be a Redirect list view model. And now here's where we got to put the stuff that we want to have access to in that page.
3:18 Will say self.redirects And we said there this thing called redirects. It's a list or a collection of redirect objects.
3:26 and the way we access our redirects is through the CMS service. We'll say all redirects.
3:34 Now, obviously that looks like an error doesn't exist yet, so we can have PyCharm write that.
3:40 And let's go in and tell it that this returns a list of dictionary. So this is actually pretty easy. We have our fake data redirects that we're using.
3:51 We can't return that because that's like a little bit too high level. It has the keys and then the dictionaries.
3:57 What we want is just a list of these so we can return that. Like dot items or, sorry, values.
4:04 There we go. That's what we want and that should return a list of these. Let's give it a shot maybe it's gonna run.
4:12 Over here. We go to our admin section. I might have not put myself back as a admin. One sec.
4:19 Oh, no! That's not at all what it is. Hold on, hold on, hold on. This was worth running into this error in pyramid. It happens a lot.
4:27 I'm not used to running into it because in my websites, I've extended some of the pyramid functionality to catch these errors.
4:33 What went wrong here? Well, we never got to that, but that doesn't really matter.
4:37 What happened is, notice when I made a copy of this, I changed all this stuff. I changed this stuff.
4:45 However, What I did not change his index, and it replaced that function Which means the base just /admin is gone.
4:54 Yikes, I hate that pyramid doesn't check for that. So we're gonna have to rename that and try one more time.
5:00 Yay! Admin is back! Now, the big question is, is our other section redirects back? Yes! Look at that. There it is. Now, this is super super cool.
5:10 It's really close to what I was hoping for We got our test button. Let's test this. It should open a new tab to TalkPython Training.
5:16 Notice the bottom. The URL looks like it's probably gonna work. Yes, perfect. And this one that goes to Python Bytes well it should go to Python Bytes.
5:26 Yeah. It looks like our little test is working our edit won't work yet because we haven't written that section.
5:31 We're not allowed. We have implemented editing. We'll get there shortly, but it looks like it works.
5:36 I'd like to change the way this feels a little bit here. Let's go over to this and we're gonna just give this a few CSS classes.
5:43 So we'll say classes of button, 'button-'... Uh for the test what kind of button do we want? We'll give it a green button to test it
5:51 And edit it we'll give it a button, buttoned-danger. Maybe we'll make these small as well.
6:01 Little bootstrap there, Cool. Now we got our buttons Still works. Of course, nothing changes about them.
6:08 This also is not super clear that this is a forward slash So let's make that obvious. What that means like, forward slash here. Cool.
6:16 And then also we'll say this is class equals link. Something like that.
6:21 And in our CSS for our admin we'll go down here and say '.link {font-weight: bold;}' like that. There we go. Now it stands out a little bit.
6:31 I'm pretty happy with this. This isn't super cool looking, but again, it doesn't really matter.
6:36 Alright, this is just for us to be able to manage our website. So this is great. we can go here to our admin section,
6:42 go over and see all the active redirects on our pages. We could do cool stuff
6:46 like if there was tracking and like this one's been clicked 1000 times and this one 500, whatever.
6:51 But we're not doing that here, right? Things we could extend it as, but we're not. And then we can test it. That already works.
6:58 And a little bit of what's left is to just edit this right? The ability to, like, pull up those details, change the name into the URL. Stuff like that.
7:07 Cool Cool. All right. Well, it looks like our redirect admin section is coming along well.

Talk Python's Mastodon Michael Kennedy's Mastodon