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