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

Talk Python's Mastodon Michael Kennedy's Mastodon