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

Login or purchase this course to watch this video and the rest of the course contents.
0:00 here we are in our project. Now what we want to do is actually Crean Admin section and over in our controllers
0:07 area we have our CMS thing that handles the pages in the read Rex. We have stuff about packages and home and so on.
0:13 But what we need is a whole new section. So I'm gonna create one of these in the easiest way is to just copy and
0:19 paste, in my opinion, so does it say admin controller. And this is going to be admin home, something like that, and we'll give it a template.
0:28 So we're gonna organize our template over here. We're getting need a new directory called Admin, and this is going to be index speak consistent.
0:38 Either call them both home or both. Index will say admin index and admin there. Okay, so this is going to map over to this index function.
0:46 We're going to call it admin Index. You can imagine it. Something like slash admin in the euro will get to that
0:52 in a second. And we're gonna have ah template over there that's gonna show it now. We don't need much of this.
0:59 First, it will be able to just return an empty directory. Our entry dictionary that's going to be haven't no data,
1:05 but we're actually going to need a little bit of information about like who the user is and stuff like that. So we'll expand on that.
1:11 It's super important that we don't let anyone, just anyone, access this. So we're going to do something like I have a
1:19 functional say Validate is admin and we want to pass the usual. How do we get the user? Well, the easiest way, it turns out,
1:28 is to create one of these view models, this view model base and give it the request. Why is that? Because if we go into RV model base,
1:39 you can see that it has all this stuff for managing the user, whether or not the user exists going and getting it from the logging and cookie and
1:48 things like that. So that's great. We're going to just be able to go over here and pass in the user. VM validate is the user Now.
1:59 That's right. That really quick, you'll type annotation. That's gonna be a user now. What's the easiest way to do that one.
2:09 We could return a value and then check it here and then send them somewhere else
2:14 if they're not allowed. Or we could just raise an exception down here if it's not right. So let's say if not user, if we took out the user they have.
2:23 This is admin and property, which weaken set in the database. So if they're not, first of all,
2:28 if there's not a user logged an or if there's your user log jam, but they're not an admin, this is not OK, so we want to raise. They're like that.
2:38 Say you don't have access this section and that's it. We just need to make sure that we call this everywhere.
2:45 Now. Some other parts of the website also need access to this user and other
2:50 information. So we're going to return the dictionary of that now here we can put and it turns out we can,
2:59 you know, pull back information about some of the redirects or some of the pages how many pages there are,
3:04 and so on. But we're not really going toe put that we're just gonna have to links on this page that will take you in to,
3:10 say, edit the CMS pages or edit the read Rex. We're pretty close. There's two things missing We could write runnin it and you'll see
3:17 the first one right away is that admin Index is not set up over in our routing. So let's go do that here.
3:27 I called already had one in here called Admin home. So we just want to go to slash admin.
3:31 I'll say index. Okay. These I guess we're left over from when I was fiddling with the site earlier. But these were the Urals that we're gonna need,
3:38 and I'll just leave them here because But this is what we're gonna need. We need first half slash admin like that would run this and it's going to come
3:47 across. Okay, let's log in here and logged in and notice it. Says admin here. The reason that is is when I logged in before.
4:01 If we go check out the users down at the bottom, here I am. Here's my account and I went had set that this user is an admin. If I take that away,
4:15 I pushed those changes back to the database. Watch if I hit refresh this admin section appear goes away.
4:20 But let's just see what happens if I try to go to slash admin for three Forbidden. Okay, that's because I'm not right now and admin.
4:29 Let's go put me back as an admin. Refresh it. It comes back. And now, while it tried to get us there,
4:37 right, this is the other thing we gotta fix is we need to write the h t mail that you saw in that last page.
4:42 But, well, you're validating access to our admin section, so that's pretty cool. The last thing is just really quickly to throw together a
4:49 bit of ah, template. That'll sort of set stage for going down that section So what do we got here?
4:56 Let's just take this super simple about and throw it in here and call it Index This is gonna be admin home.
5:08 We have a couple of actions that you can do. It will be able just have maybe two dibs with some hyperlinks. B slash admin slash redirects. That's one.
5:22 And the other one will be pages again. We're not doing that just yet. We can't do that in a bit. We saved that. Come over here and refresh.
5:29 Now we have control or site. Maybe don't want the self centered, but not a big deal. So here we go to our redirects.
5:37 Here we go to our page is these are not created yet, obviously for four. Sorry, we can't find that page,
5:43 But these were the two main things you're gonna be able to do from our site So we're gonna set it up so that you can come here.
5:48 Let's just one quick final thing Throw in an admin CSS. You already have an admin CSS with some things set for us so that we can
6:00 have, like, a cool little block that stuck in the center of the page and so on. So we come down here and say, Put these two together. See,
6:13 this is an admin, Matlock. Let's put these and do some linked lists like that. These will be the things we can dio.
6:24 The other thing we have to do is over here. We have to include that CSS file into our site. So we want the static. CSS has to be admin like that.
6:35 They were doing. We go. I guess we want one more little change to her. Yes, as here text align is left great and maybe also four of this piece
6:46 as well. That was supposed to be in there. Perfect. That's our little admin section that we got for our site. The two things you can do.
6:57 Are you gonna go visit the redirects and the pages? They don't exist yet, but our progress through the rest of this chapter is going
7:03 to be built out ability to add, edit and control these redirects just from this page sitting messing with code or the database

Talk Python's Mastodon Michael Kennedy's Mastodon