 Hi, this is Christian, so this is part two of the Flask application we've been developing. If you have not completed the part one yet, so make sure you watch that first. All right, so now back in the IDE, I'm going to go and activate my environment first. Script, activate. Okay, now I'm on. I want to go ahead and run again just to make sure it's still working. So Flask run and I'm at core 5000. So I'm going to go low my other browser and let's see should go back to this page. Not that one. Let's try again. Maybe it should be just 5000. Okay, so this is the one and our providers is all here and we're able to add data successfully last time and so forth. Okay, so now we're going to perform the edit and the delete operations. Okay, so let's look at the edit first. Now when you do the edit, right, so we're going to build or create a new URL for this edit slash with an ID and the form for the edit will look very similar to the add. So it will look the same as this. We're going to just pre populate the data from the list. Okay, based on that selected ID and then instead of saying add provider, we're just going to say update provider. Okay, and then once we update it, it should take us back to the providers page and we should see the update information right here on the screen. Okay, so let's go ahead and do that first. So I'm going to open the app and the routes file. So in here we have, let me close this terminal now. So we have the ad here. It will look very similar to this. So I'm going to copy maybe just, well, let me, first of all, let me do this. I don't know why it's not wrapping. I just wanted to set the wrapping. Let me go ahead and set that first. I really don't like that feature. So it's in the edit, just give me a moment here, setting and I want to change the wrap and just turn the wrap on. Okay. I think that's the one. Oh yeah, maybe that's it. So let's save this and then now we should be on a wrap. Okay. So I'm going to copy all of this and we'll put right below here. And we'll change it to say this is going to be edit or in this case it'll be update. Okay. So the URL will be edit all of my ID. So in Flask you reference the ID by using a pink or bracket like this and set the bracket. We'll just do the very simple one. There are other ways to do it, but inside here we're going to give a variable. So whatever this variable is, you're going to access it directly from the request. Okay. The function watch it grab the by default. So I'm going to just call it ID. Okay. So that is the ID. And then the function here will be called edit. And then inside here is the ID variable. So this ID variable must match this ID you put inside here. Okay. So that is the first argument. We're going to grab grab from URL and inside here then we're going to update this a little bit. One thing for sure is that once we do the edit, right, it comes back from the form. As you can see, this looks very similar to the ad. Right. It's going to come to the post as well. The only thing is that when we edit the form, let's say that we're not going, we're not going to go and edit the ID. We'll leave it out. Okay. So we'll leave the ID out. Only generate a random ID when we add a new provider. Otherwise we're not going to touch the ID. And then when we build the provider, the ID here will be the same ID as it was before, which is fine in this case. So as you can see, it looks exactly the same. Okay. So we get this type of, you know, repetitive code. The first thing that comes to mind should be a function. Okay. So we're going to put this into a function and we'll let a function build this object and then return it back to us. We're just going to use it. Right. So we can save you, you know, a couple of lines of code. Okay. If you can be using this again and again, it saves you a lot of code, lines of code. All right. So why don't we do that first? And I'm just going to do right here so we can see it and then we'll move that later. All right. So let's just say I do a function here. I would say get provider. What should I say? Get provider. What should we call it? Maybe not get. I'm actually, I'm actually building. So I'm not getting, I'm going to go build provider object. Okay. And what I, what I need from that function, I need the ID or the request object. Right. I need to pass in the request object. I need this object here. And I also need the ID. Okay. So put ID here. So we add a new object. You're going to pass that to the function. And then the second function is this whole thing. I'm going to put this whole thing here and move everything put inside this function. And then when I build the ID here, the object, I'm going to return it. So you can do that. Okay. That's okay. Or you can just simply return the whole thing in one go like that. Okay. We'll just make it short. So I'm going to return that object. And notice I'm not doing any validation here. So normally you would probably want to do that. But in this case, I think we're going to leave it as is. Okay. So I returned the provider as an object. It comes back down here. And then this is the add function. By the way, I'm going to go ahead and put provider is equal to build provider object. We pass to that function, the ID. Actually the request first. And then the ID. Okay. It's going to build that returns that object to a provider variable. And then we append that to the list. Great. So now we do the same thing down here for the edit. Right. So this part is going to be exactly same as above. And then the only thing is that notice we did not generate the ID. It's a new ID. We don't do that. We're just going to grab the current ID. So whatever the ID is, right, we'll just use it. Now, the only thing is that when you submit the form back. Okay. It depends how you set up your form. The ID may not exist anymore. So you either have to have it hidden form. You get that from the request that form. Or you can also include that in the URL. It comes back in the same URL pattern. And then grab that ID here. So since our setup is already like this, I'm going to update the form to include the ID in the URL. Okay. So when the ID comes back, if it's a post, yes it is. We're going to go and build the provider. Right. Because we don't know which field was altered. Generally, there are other ways to do it. But the way I'm doing here is more of a brute force approach where I'm basically rebuilding the entire provider object as a new object to include whatever data has been sent over from the form. Right. As opposed to if there's a change only in the first name, then that's the only thing that's changed. We just alter that. But that's another day, another time. If you want to build a class to a form class to actually sync to the form itself, then that's the way you do it. But for this one, we're going to build the entire object. And then instead of appending it, we're not going to append that. So what we're going to do is we're going to do something like this. And I find the index position of this object. And then we're going to assign that with the new provider. So now, how do we know which provider it is? Certainly it's not the ID. So we need to find out where this provider is matching this particular ID. So to do that, then we have to find out where to do a fetch. So we would do that through a function. Again, we use the function to find the ID position in the list. And then maybe return the index position of that object. And then we know where to go from there. So I'm going to go up here and add another function up here. This time we'll say maybe get the provider index. And I need the ID and also need the entire list. So I want to call the same name providers underscore list. So now there are, I'm sure, a couple ways to do this. But the way I want to do this, I'm going to search in that list of objects and then compare each of the list IDs to the current ID I'm getting here. If it matches, then I'm going to return the index position. So I'm going to do a range. So for I mean the index position and the range between zero, the first parameter, or first on the list, all the way to the end of the list. So we'll call the lend function. And we'll pass in the provider's list all the way to the end of the list. And if then we're going to say if the provider's list of I is equal to the ID, if that is the case, then we're going to return that I, which is the index. And if it's not found, then let's say we'll return maybe none. Now only thing is you have to be careful because when we access the ID in the list, if you look at our model, our list is actually a number as opposed to a string. So when you pass the ID from the URL down here, because I'm passing this ID from the URL to that function, I need to convert that ID to a number. You can do it here or you can do inside here. It's entirely up to you. So if we do outside, then we'll assume that that is in already a number. So just to be safe, maybe we can do it in here. It's entirely up to you. So I want to say this ID here has to be the integer. And all you have to do is just put that inside the end function like that. Because I know that my ID is in number. So whatever it is, being compared to must also be an integer. And if that matches, return the index position, and then we're good. So then down here, when we look for the provider, so you're going to go and maybe create a, you know, we'll just do one step at a time. So I'll do here, index is equal to get provider ID index. I pass in again, the ID, and then also the list, right? Number equal sign. So I'm going to get the index coming back, but it may, it may not be found, right? So if it's not found, you can kind of check to make sure that's true. And if it's not found, then I want to say if index is not, not none. And that's the case to go ahead and, you know, update the index at that position with the new provider. Otherwise, you know, nothing changes. Okay, and then we're going to return or redirect the users back to the provider's page to see the updated information. Okay. Yeah, we'll see if that is the case. Okay, so this is only for when we submit the form. Now, when we load the edit form page, we also need to populate the form. So that means that we're going to get the ID just like this, and then, you know, grab the ID from the user, the entire list, and then we pull that ID and then pass it to the form. So I need to do, hmm, interesting. That means okay, so I might have to put this outside. Okay, so let's put this outside because I'm going to do the whole thing again. So let's put out here. You don't have to do it twice. I'm going to get the index. Okay. And then down here, I do the same thing. If index is not, no, not none, then the provider is going to be equal to the provider list of that index. Okay. And then we'll pass this provider to the form to pre-populate the form. Right. Otherwise, it's going to be a no value, and that could lead to a problem, but we'll see. So here, it would say form the edit form. We don't have that form, but we're going to build one and you have to say update provider. Okay. Let's see. Yeah, maybe it looks good to me. So go ahead and save this now. Now, we need to create a template for the edit. So the providers here, again, will look very similar to the ad form. So all I'm going to do is I'm going to copy this. Okay. So I'm going to just do a copy and paste, writing here, and control C, control V, shortcut. And then right-click on this to rename it to edit provider dash edit dash form. Make sure it matches your thing here. So now when you open this form, it will look very similar. I'm going to change this just to say update. So that makes sense. Here will be update provider. The, let's see. Okay. So down here, we just say update, provider for the bottom as well. No. So these fields will be the same as they are. The only thing we need to do is we need to pre-fill these fields. Right. And remember that when we add, when we load this form, we're going to pass the object provider to the form so we can access all their fields. So let me turn this one off. So when we hear them, we're going to start with the name first, the id here, the require. So let's put here the value. It's going to be bound to the provider of the first name. It will look like that. Let me copy this and I'm going to put it right here. Add this field and the company field down here as well. Okay. I'm just going to copy and paste these in so they're, they should be correct. All right. That looks good. And then finally up here, we want to change this URL to say not add but edit. And also we want to include the id. Right. When we submit this form, we want to include the id which is in this case is provider of id. Yeah. You can, right. Absolutely. You can do this, which is okay because it's that provider. Or I mean, other way you can do it too because you know that you found the id already. You can also include here like id is equal to id. Either way, like that's fine too. If I do it this way, then in my form, I would just say, you know, right. And that's okay. Maybe I'll just do that way. We'll see. So say this file and then, yeah, it looks okay. We'll see. And let's go to the browser and give it a spin. So let's refresh the page. I'm going to go and do the edit. Right. Some errors. Okay. So it's local variable provider is not used before assignment. Line number 77. So let's take a look there. I'll probably use it before I've been created. So 77. So right here provider. Either way, I do this because, you know, this may not exist inside the variable here. So that means the index here comes back. And if it's a get, then it's going to load this part. Let me separate this out. It's a little confusing. Provider should be equal to that. So before I do this, maybe up here somewhere, let's do this. Put provider is equal to, you know, none. Okay. So it's pretty clear. So that doesn't cause an issue down here. Let's save this and try again. And it's going to refresh it. Okay. So something is loading the form, but our data is not populated. So that means we did not load the correct data. So let's try again. Go here. I'm going to go and edit. Okay. It loads. It does load the form. It just doesn't load the data. So let's take a look and see how to fix that. So when we load the data, it goes in here, we look at the index. So that means our index was not doing correctly. So if I go over here to the index function, and we look at the range from zero to the list, and we check to see if the, oh, yeah, I can just do that. Right? It's not the ID. I mean, the provider's object of that list, but then what am I comparing? Right? I missed that. Comparing the ID field. So I do another one here, like that. Okay. So I did not do that correctly. So let's save that. And let's go back and do it again. Let's go and then edit. All right. Here we go. So we got the data pre-populated. Now I want to put just a dvd here. And let's put some information here. Make sure they are updated. Just some fake information and update. So it does seem to work. It takes us back to this page, but then I have a problem here because it says this URL is not found. So edit provider slash the ID. And I thought we had that before. Let's take a look. So it goes back here. Oh, call it just edit. Right? Just edit. But in our form, call it edit provider. So let's change that to just edit. There we go. It's a really good experience to go through this. So you can see I'm not perfect. You debug as you go. Let's go back again. And I'm going to go and just provide updates to what happens. Okay. I probably didn't update the URL one more time. So just put everything here. Update. Perfect. So you can see that now. The ID or the name, the last name have been updated. We did not touch the ID field because we don't want to, you know, change that. All right. So that is pretty good. And I can go back again and update. And again, now it fails because whatever reason have a problem, it doesn't load the data. So let's take a look and see why. So what is the problem? Right? So I guess we can check. Let's go back and try again. If I load a different one, and that seems to work when I load the one I updated. It doesn't work. So I did load the credit ID. And so I think what's causing this is that when I, let's see, when I send the data, when I send that ID over to pull the index, now remember when I pass, when I load the ID, it comes in as a string, even though it's a number, it's a string. So when the ID is passed to the function up here, get the ID, compare that. I did convert it to an integer already, which is great. That's correct. And then the problem was when I did the edit, the very first edit, I go in here, build the object here, I pass in the ID. Remember ID is also still a string. So this function here, when I build the object up here, notice I did not convert that to an integer. So once I edit it, this ID is actually a string. Okay. So again, you have to take care of it either here, the function level, or outside here. So in this case, I want to assume that, you know, the function is perfect. This ID should be all integer. So I did here in the other one where I actually converted here. So this time, I'm just going to show you that in a different way, is I want to convert this in here to an integer. Okay. So when you do the build ID here, do something like this. I want to convert it before I send it in. Again, this is just another way you can choose either way you want to write. Maybe you want to convert it first before you do that. There's another way to out here, you can make this into an integer and submit that in. So it'll be automatically converted to an integer. I mean, lots of ways. So let's see if this one here solves the problem. Okay. So save that. Go back to the form. And let's go ahead and, you know, let's refresh our page again. Okay. Let's edit this one. And I do it again. TTT, just those two. Update. So it's updated. I should be able to edit again. There you go. Okay. So that was the problem. Perfect. All right. So now we are able to edit our data. The next one is we want to delete each of these records. So if I click on it, as you can see, it looks very similar to the edit. I pull the ID. And then from that ID, I'm going to search in the list to find the index position. And I'm going to project from the list. That is the logic here, right? So let's go back to our code and see what we can do. So now, so I'm going to go and create the other API. I will go ahead and just copy this part. We'll put it down here. And we'll call it delete. And we're just going to do delete one. Just to do one with the ID. So it would say delete with the ID. And really, in this case, we don't want to care about the post again. Okay. So I can leave it there. It's fine. I can remove it because we're not doing anything from a post. So this would be delete. And then getting that from the ID field. Okay. So now the process is kind of similar to the edit. We first must find the index of this particular object. So I'm going to go here again, make sure, well, you know, this will be converted inside a function. That's fine. So when I get the index, if the index is not, it's not none. That means it's found. Then I can call the delete function, TEL function. And you just basically delete that from the provider's list of that index. That's all, right? I will purge it. And then once that's done, then you can, you know, redirect to a different page. If it doesn't really matter, then you can just go ahead and render the provider's template. I'm going to go and provider's template would be... Yeah, I'm going to go back up here. I'm going to copy this provider's... Yeah, that's just to redirect. So I don't have to worry about passing all this data. So I'm going to do here, regardless, you know, whether it's successful or not, I'm going to go and redirect to the provider's list. Okay. And if that's successfully purged, you should see a reduction on the list. So let's go and take a look. Then there we go. The first one, right? You did see that it's now gone, right? You probably want to do another little computation to show the number of records here, but it didn't disappear. Okay. So again, if we do that one here, tell them, and it's now gone. Okay. Perfect. So that's how you delete. Very simple, very easy. And I will go back and just do one more reflector and then we will be done for this one here. Okay. So now you see that these functions are quite useful because, you know, you will use them again and again. So I don't like to put my functions all in the same place here. So if you have any of these type of functions, these are considered as utility functions or sometimes you can call it helper functions. You can move them into another directory, right? Just like these. So I'm going to go into, maybe in the app or even outside of it doesn't matter. We'll put outside of the, in the root directory, put here functions. And set a function just like before, init file. Okay. And then I'm going to create my functions file. You'll call it um, u, u2 underscore functions.py. Okay. And then I'm going to copy and move, I'm actually going to move these two functions into that util function.py, right? Save that. And then I'm going to import that in here. So from the functions directory, that util functions file import the get index and get build provider object. Okay. So let's do those two functions. Now, and then once you get that, and then up here in the top, we're not going to, we have to import those in. So like this one here, import from the functions, import the get provided indexed and build credit object. Here we go. And then our code should still work just like before. Okay. So you can see how, you know, this works. Of course, you could also add another function instead of, you know, how I did it here and see when we retrieve the provider, like right here, right? We, like right here, we get the provider from this index, which is okay in this case. Once you find the index, then you can just use that to pull the provider out of that. You can use it here. We use it also here as well, right? When you want to do a fetch by one, you would do exactly just like this. Another way you can do is that instead of, you know, accessing directly this way, you can write a function to do that. So instead of saying providers in the school list index, I'm just going to say get provider, right? And we can easily do that over here. For example, I'm going to go down here and function get provider. So the index, get provider, the, I will still need the ID and the list here. So let's put that in here. And then so in here now, because I already have the index, I still need that. So what I'll do is you will then get the index just like before. I'm going to say index is equal to get provider index ID and the provider's list. And I'll do here, if the index is not none, then return the provider's list of that index, right? Otherwise return, maybe return none. Okay. So if you want to get the index, you use this function. If you want you to get the provider, you use this function. Okay. So let me say that. And let's go import that in here as well. The get provider function, good. And then up here, make sure we include that in the import up here else also your get provider. Perfect. So now down here where I have the, you know, get the index here. Like once I got the index. So instead of, you know, I see that's fine. Like right here, right? So I'm doing this way. I could then do it this way. Provider equal to get provider. And I pass in the index. I mean, ID and the provider list. Just make your code a bit more, you know, usable and portable for other usage. So then I'll do the same thing down here for the, well, I actually know this one here. I need the index, right? I need to know the index so I can purge it. But this one, I need the index and I need to get there from the list. So instead of doing this one, you just do this and then, you know, use the index function to do other things. So I think that should still work. So let's see. We'll try one more time before we call this off. So we go, I'm going to do an edit her again. Okay. So you can see here it works fine. I delete that and perfect. All right. So that is it. Now we have time. We can go ahead and, you know, do a log in the contact register. The logic is the same. Also, maybe you can implement the delete all at another button. Maybe up here or down here somewhere to say delete all, you know, that will go to the delete link. But then once you get there, you know, don't provide the ID, right? So you will delete everything. And okay. So I hope that was helpful. Have any questions, please let me know.