Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Creating the admin backend for Flask
Lecture: Fulfilling a cake order in the admin backend

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we've got our table all built and we've got that cool fulfill button.
0:03 But we need a back end service to handle that request,
0:06 right? Let's go and add one more thing here and we're going to do is
0:11 going to put /admin/fulfill and pass over the order id, here and
0:21 now, the warning and pycharm is that this does not have an order id
0:25 down here. It's going to be an integer.
0:29 So, we're gonna go over here and do you actually 'get' against this doing a 'get'
0:33 to make a modified change of our sites?
0:35 Not ideal. We could change things supposed because it's back behind the admin section.
0:41 I think this is going to be okay.
0:43 I want to change this to fulfill it doesn't conflict with that name,
0:48 and then we need a little protection.
0:51 We need a little test here.
0:52 So instead of having the orders,
0:53 I want to say order, say find_order_by_id.
0:58 have that function and we want to check,
1:02 if not order. So if they try to fulfill an order that doesn't exist,
1:06 guess what? Return flask.abort(404) nope.
1:12 No order here otherwise, we want to go and say,
1:16 order_service.fulfill pass over the order_id and then let's send them back you
1:30 /admin. I think this is going to be all we need to do for a
1:34 view. They submit the order_id.
1:36 We say, Let's make sure that you've got it.
1:38 Go to the database, get the order_id.
1:40 If you don't well, that means effectively.
1:42 The URL here is invalid.
1:44 So 404, But if it does exist,
1:47 let's go ahead and fulfill it.
1:49 Passing over the order_id again.
1:52 Send them back to that list that will refresh it, When it refreshes.
1:55 It should flip from having the fulfilled button to being closed.
2:00 All right, All that's left is to write these two database queries.
2:02 Let's do this one first. Let pycharm write the function for us.
2:10 Return an Optional[Order] optional because it's probably there,
2:17 but it might not be there,
2:18 So let's do that again. Everything is going to look like this.
2:21 So borrow that. I'll just say order=context.session.query,
2:33 and we'll just do a filter where
2:36 order.id== order_id and do it first.
2:42 and then return order or maybe none,
2:45 Or maybe in order. Either way,
2:47 it's going to fulfill that, and that'll be great.
2:51 What do we got to do next?
2:52 Well, we need to fulfill an order.
2:54 So with that pycharm, right that as well they want to use our context
3:05 again. And you might think we could just re use this function.
3:08 But because this order is tied to this session and this session is getting closed,
3:14 sadly, we've got to do this again.
3:17 We'll see, if not order:
3:18 So if there's no order, we can't close,
3:20 it can weigh, so will return false.
3:24 This returns the bool here. There's no order.
3:27 We can't close it. That's great.
3:30 If there is an order, it has already been fulfilled.
3:34 And the way we're going to do that,
3:35 it's going to set the fulfilled date from none to a daytime.
3:39 We're just going to return True,
3:40 we can't fulfill it twice. It's already been fulfilled.
3:44 Given these two things are working here,
3:46 we will say order.fulfilled_date=datetime.datetime.now(),
3:54 Now we've got to commit on success appears True perfect.
4:00 That will just push those changes back, that does indicate we don't really need it up
4:05 here. It won't hurt anything,
4:06 but there's nothing to do similarly up here,
4:11 it gets slightly more efficient. Okay,
4:14 But this one, we definitely want this one to commit on success.
4:17 All right, this is looking really,
4:19 really good. I think we might actually be able to fulfill it.
4:22 Better view Method written. I didn't get the order.
4:26 Try to fulfill it, and then we'll just go back and reload the page after
4:31 doing so. Let's try it.
4:34 Here we are. We go down to our admin section.
4:36 There's two orders 1 and 2
4:38 which are both open. Let's go and click this Notice the URL on the bottom
4:42 left, /admin/fulfill/1.
4:47 We'll click this bam Super fast fulfilled.
4:52 Fantastic. Let's copy this URL And just try our our testing here,
4:56 So copy, link location. Let's go to uh,
5:00 not 2, But 2000 not found 404.
5:04 44 Okay, perfect. We do have our other one left here,
5:08 button right, Let's go and fulfill this other 1,2 bam!
5:11 Super fast fulfilled. Done, so neat.
5:16 So we've got our full admin backend
5:18 entirely done. Let's go down here and add a comment to do rather send WhatsApp
5:27 message to notify customer. So,
5:32 yes, we still got to send our WhatsApp message.
5:34 But the whole data juggling side of things on our admin back end wasn't that hard
5:39 was it? And it's totally done,
5:41 but yes, let's go ahead.
5:42 And later on, we're gonna do some WhatsApp programming to directly send that notification.
5:47 Come to the shop. Your cake is ready.