Adding a CMS to Your Flask Web App Transcripts
Chapter: The redirects (our bit.ly)
Lecture: Demo: Admin section

Login or purchase this course to watch this video and the rest of the course contents.
0:00 it's time to start building the actual CIA mass, the management system or the admin backend for us to work and manage the content in
0:11 our CMS, Remember, we're gonna focus first on redirects, but we need to kind of build the overall structure,
0:18 and then we'll get into the redirect side of things so we could stick that into the sea mess views. But I feel like I kind of want the admin side
0:29 in the consumption side to be two different concepts. And so because of that, I'm gonna copy this and create an admin views.
0:39 Call this admin views change That name is it's kind of a unique Now here such is gonna be slash admin. And let's just call this index with no input.
0:57 All right, so this is the page that you're gonna request when you go to the admin section and what I think it's going to do this just going to show
1:04 a really simple and basic page that says, There's two things you can do in the admin side A.
1:10 You can view the redirects and B you can go view and edit the pages. So we weren't just set up a quick little template for that.
1:18 We look over here, you can see that we've got this decorator that we're using
1:22 throughout the rest of the site to talk about what Jinja template were using. So when you go around here and this is going to be admin Index,
1:32 okay and we do need to import that at the top. So let's go over here and make an admin boulder there.
1:42 And let's say something super simple like this about Paige and put it here as the index. All right, so let's put just some,
1:51 like Admit home. We'll comment controlling update the website from here. We're going to have some additional CSS that we can include over here.
2:08 Let's see we confined wind. We have something going on here at the bottom. Now, we're not using any extra there, but over in this one here,
2:17 you can see that we're using an additional CSS file. This is injected back into the overall template.
2:23 So let's just go. But that here and notice that we have an at men dot CSS over in our CSS file.
2:32 I've already done the CSS. We don't really need to do the Web design for it. It's not super inspiring.
2:37 It's just some real basic stuff in this admin block. I also realized we need to have a text a line of left here to get
2:44 this to work. Okay, so we're gonna include our admin, CSS, and we're gonna have our content here. Let's say that this has the class admin block.
2:55 Put that into an inter Div. One of our two things. That's an A nordle ist over here. We're going tohave Ah, hyperlink,
3:04 which is going to slash redirects This will be Do you read a rex? Guess what? The other ones?
3:14 The same, but for pages And I'm guessing that should be admin slash redirects and admin slash pages. All right,
3:26 Super Well, it looks like this is kind of in place or inherited from the layout. This is the title we months to be.
3:35 So you must add man or something like that. A little header appear are am unblock our CSS. Okay, I think the HTML side of things is done.
3:45 So we've already linked that up here. We could almost make it work. We could give it a try. If we go to slash admin,
3:52 you're gonna see It's not as amazing as you might hope. Let's try. Wait a minute for a four. What's going on? Remember, in order to use these views,
4:02 these blueprints, we have to register them in the sight. So let's go down here and we'll just do the same thing as we had before
4:09 from views were gonna import admin views and down here or that will register the blueprint for that one. All right,
4:22 let's see if that's gonna do the trick. Perfect. It's sort of working. Except there says you gave us a string and we expected a dictionary.
4:31 That's from our decorator right here. It says, Hey, we're gonna pass a dictionary of data over to that.
4:39 So let's return an empty dictionary and see if it like that rerun. Yes, it does. And it does do this, sort of. But there's something going on here.
4:51 If I'm over here and I log in, I log in and I go to the admin section. Wait a minute. What happened in my account? Where I go, I thought it was logged.
5:02 In a way, I'm logged him in the admin section. I'm not locked in weird What's going on?
5:07 Well, it turns out that the view model based remember all these other pages that we had, like over here, we're turning some kind of view model,
5:17 and that view model drives from view model base. Here. The view model base is the thing that initialize is whether or not there's
5:24 a user and passes it along to the underlying view template. So we can just do a real simple fix. Here we say there's a view model,
5:32 which is this we don't actually care about any specific one. Just have the base one, and we can say to dictionary here.
5:41 If we do that, it will carry across the user information like you expected. And now we got our account info appear. All right, so that's pretty good.
5:49 We got our admin home shown. If there's a small problem and it kind of was hinted at a little bit of
5:55 their If we go over here even in a non authenticated unlock, then view. Ah, how do you feel about the slash admin having no restrictions
6:07 Anybody on the Internet that decided to go to slash admin can just view. It's here. Maybe the subsequent operations have restrictions.
6:15 Who knows We haven't created those yet, but I'm not a fan of people just being able to see this if they're not
6:20 logged in. So let's go over here and add one more technique. So what? We could do it. I could come down here and say some like this.
6:31 If not VM don't user or not? VM not user dot is admin so in the user object. If you go look over here in the database,
6:43 there's an admin value set. Right now, it happens to only be set for, well, me. Let's Gol and throw this into PyCharm and open up the
6:55 database. Here we go. Have a quick look at users good under the very bottom. That's where I am. Notice that I have this set.
7:08 So what I want to do is I want to check that if this is set I can get in. If it's not set, can't get it. Obviously, if there's no one there as well,
7:16 we want to make sure that we're testing for that. We could do this like so we could say Return last abort or three,
7:25 something like that. So let's try that again. So over here we started. If I go my private window over here and see
7:38 it's doesn't allow me in. But if I go with my logged in window, it does let me lock. It does allow me. And so for three vs. Hey,
7:46 you are an admin. You're logged in. That's good. That's what we wanted. I should check that if I have a user,
7:53 but they don't have the admin property won't let us in. But that's gonna work now. This is a little tricky. It's like stuck in the middle of this page.
8:01 It's a little weird. So what I've done is I've created something much better.
8:05 We have another decorator here call under this permission section can have more, but right now it's haven't admin.
8:12 So what we can do is we can just wrap original function with this, and it's going to check. Get a view model,
8:19 all right. The good we just wrote are turned. It's either going to return flask aboard,
8:24 or if it does pass, it's going to call the function that we're decorating the view function. Okay, so this is very easy to use.
8:31 You haven't built decorators. Don't worry about it. It's just how it works. Come over here. And we just have to say at her mission,
8:42 Import that. And then we can just say admin like so And that allows us to take that whole thing away right there. Let's rerun it and try again.
8:55 Make sure it still works. Can I log in? Is me? Yes. What happens if I hit it without an account or not logged in with the right one?
9:04 Oh, whoops. It's not supposed to let us in. I think the blueprint has to be on the outside for this to work. Let's give it one more try.
9:13 Here we go. Not allowed. And this one still less It's in perfect. But just like before it's blocking that. Maybe we could put it as far inside is this.
9:27 It works the works, right? Just got, remember? But the blueprint first. OK, but I feel like this a little bit cleaner.
9:35 You can look up here and see the permissions on this. So we're gonna use this permissions view decorator,
9:43 and that's pretty much it. We have our base of our CSS admin section over here. We can be on the home, and if we're logged in as an admin.
9:54 Let me just show you by changing that really quick go down here and I uncheck that look away and there's a button that's hiding this.
10:09 Push the changes to the database. Refresh it. The admin section is gone. If I try to get a admin, even is, this doesn't work.
10:19 But that Mac push those changes now we get back in. So we have our admin section and Onley logged in users. Or also add mends in the database.
10:31 Get access to the section. I think that's a great start.


Talk Python's Mastodon Michael Kennedy's Mastodon