Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Creating the admin backend for Flask
Lecture: The admin index view method
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The HTML set of our websites a little bit lonely. We just have this one little old '/' view method.
0:07
This index. That's what we get when we land on the homepage. But let's go in and build our admin backend,
0:13
and we can just borrow from what we got here because it's going to be so similar. So we're going to copy and paste and just call this admin.
0:19
Remember each major section I like to have organized under its own things that we have
0:25
an admin section. We've got our API order section up here at home and so on. Now, in our admin up here, it's not going to be '/'.
0:33
Of course, it's going to be /admin. That's going to be the index view here, and it's going to render the admin/index,
0:41
which PyCharm happily tells us it's not there. Let's go do that next, again,
0:50
The best way to take one of these HTML templates that derives from other templates and so on is to just do a quick copy and paste.
1:02
And there we have it. So let's get rid of most of this stuff here We'll have a div as a content, style and we're gonna have a table in
1:12
here with some stuff. And it's up to us to figure out what goes into the table. Remember, that was order,
1:17
username, whether or not it's fulfilled the fulfillment button and all that kind of stuff
1:21
So back over here, we need to decide what data it is that we're going to have to pass along. Let's go look over here at our database.
1:31
If we look at our order, it has things like the idea that we're going to need. But it also has this relationship over to the user.
1:40
What we're gonna do is we're actually going to just pull back the orders and make sure that this user field is populated.
1:45
If we didn't populated, depending on how we created and interacted with the session
1:50
it would either lazy load or crash the way we're going to work with it because we close it explicitly.
1:56
It probably would crash, but that's what we want. So we're going to do a joint to make sure that all the orders come back
2:01
loaded up with their user information, and that means we need to write a new service.
2:08
We have a user service. Let's write an order service and it's going to have a simple method here, All cake orders and what it's gonna do.
2:22
It's going to return a list of order now if we look back over here, remember, we have this cool session thing going on like that.
2:33
So we're just going to use our session, context here to make this nice and easy and make sure that's import it. What we need to do is we need to write
2:43
a query, all those orders, and we go to this context. We go to the session and we say there's a query of order, we want to
2:52
do an order_by, so it created date descending. Let's show the newest ones at the top. We can wrap that around and we can say we want all of them.
3:03
That's cool. And then we just say, Return orders. Now that looks like it's going to work.
3:09
This little weird error from Pycharm about descending that's not actually a problem, so there's no real errors here on the page.
3:16
It looks like it's okay, but if we try to access the order.user after this closes. So after that, we're going to be in trouble.
3:26
So what we need to do is go over here and say .options subqueryload and import that name, and we just go and say order.user as
3:40
part of this load, we want to make sure we do a join or subquery to get the user for every order that comes back.
3:47
And that way we can interact with both the order and its associated user. Okay, well, that should do it. Let's go over here and just do a quick test.
3:57
orders = order_service, all cake order. That should be orders, right? Let's do a refactory name and make that plural all cake orders.
4:11
There we go. And then let's just, you are let's put a breakpoint right here as, just to make sure that this is coming
4:16
back with something meaningful. Click on the admin section didn't load.
4:23
What do we forget? One quick thing because we partition this out with blueprints. We've got to make sure that we put the blueprints back in.
4:32
So down here, where we register the routes got one more. We've got admin and change, the name can have that conflicting.
4:48
All right, now it should work if we click on that little link right there and it does. Perfect. It didn't crash,
4:54
It just kind of came back with nothing but what we need to do. We set a breakpoint, didn't run into debugger like the debugger like this,
5:01
and see what we get. Look at that. We have a list that contains one item at 0x10cff. Such and such. Let's go and expand it out.
5:12
Look at that. That looks like legit data. We got a rainbow, which has maple frosting. It's not fulfilled. It has id1 and so on.
5:21
Super cool. Right? Well, there's only one order in the database, so we're not gonna have too many will go and create some more and see them
5:26
show up as we go through this,
5:28
But, yeah, we're pulling all the items back from the database and showing them here. Perfect we have just got pass them on to the view.