WEBVTT 00:00.480 --> 00:06.300 Now, the next step for us on our site is add the ability for someone to be able to complete a to do 00:06.300 --> 00:06.510 so. 00:06.510 --> 00:11.310 For example, in my current to do list that one of the things that I want to do is complete it to do. 00:11.330 --> 00:13.680 How do I say that I have accomplished this? 00:14.250 --> 00:16.320 Well, it starts first by creating a U. 00:16.380 --> 00:18.600 RL, just like we've done all these other times. 00:19.050 --> 00:21.270 Now it's going to be very similar to what we've done. 00:21.360 --> 00:26.880 If someone wants to update or delete an existing to do, we're just going to say to do slash get the 00:26.880 --> 00:27.780 primary key. 00:28.020 --> 00:32.280 But then we're going to say the user should pass in complete. 00:33.010 --> 00:33.230 OK. 00:33.810 --> 00:40.920 So if someone says I want to complete to do number five, they're gonna do to slash five, slash complete, 00:41.040 --> 00:44.100 which then we need to go create an appropriate view for this. 00:44.130 --> 00:46.860 Let's go ahead and move over to our views. 00:46.890 --> 00:47.570 Dot Pi. 00:48.510 --> 00:51.240 And this is just going to be an update for you. 00:51.450 --> 00:57.900 So let's go ahead and copy what we had up above here for the to do retrieve, update, destroy and really 00:57.900 --> 00:59.250 simplify this down. 00:59.940 --> 01:04.400 So we're going to change the name, the name of this to be to do complete. 01:05.100 --> 01:09.750 Then we're gonna change this to just be the update API view. 01:10.920 --> 01:17.400 Now, with this in place, it's very tempting to use the current serializer that we have. 01:17.400 --> 01:24.360 But unfortunately, that's not going to work for us, because when someone is going to update to say, 01:24.360 --> 01:30.180 hey, I want to complete this particular to do, there's nothing, in fact, on the model that we want 01:30.180 --> 01:31.380 them to be able to mess with. 01:31.410 --> 01:35.060 If we go to the to do models, we don't want them to change the title. 01:35.080 --> 01:40.110 The memo that created the date completed important or the user in hear me clear on this. 01:40.170 --> 01:43.230 We do not want them to be able to set the date completed. 01:43.260 --> 01:46.980 That's something that should be handled by our system. 01:47.340 --> 01:53.280 So what we're going to do is we're gonna come back to this to do completed and rather than using the 01:53.280 --> 01:56.160 classic to do serializer that we've been using everywhere. 01:56.430 --> 01:57.900 Let's make a new serializer. 01:57.960 --> 01:59.280 It's gonna be very similar. 01:59.640 --> 02:05.190 So we can, in fact, copy a lot of what we have here and paste it down below. 02:05.910 --> 02:10.950 And I'm going to call this the to do complete serializer. 02:11.760 --> 02:12.070 OK. 02:12.540 --> 02:16.800 And what we're gonna change here is that we want all the fields to be read only. 02:16.920 --> 02:22.740 So what I'm going to do is I'm going to delete these two fields that we said we're gonna be read only 02:22.800 --> 02:25.200 instead of just saying, you know, these are all the fields. 02:25.260 --> 02:30.110 These are gonna be the read, underscore only field. 02:30.150 --> 02:33.650 So all of these are going to be real read only. 02:33.990 --> 02:37.020 Now, there is one field that we still want to hold on to. 02:37.020 --> 02:40.730 So are going to say Fields is equal to the I.D.. 02:41.400 --> 02:43.730 And go ahead and delete that from the list down below. 02:43.740 --> 02:45.330 So that's the one that we're gonna keep around. 02:45.330 --> 02:48.360 But everything else is going to be read only fields. 02:48.420 --> 02:51.300 So we could have just manually typed all those out there. 02:51.330 --> 02:56.220 But I think this one looks a whole lot cleaner and now we have this to do complete serializer. 02:56.490 --> 03:02.040 So with this, I'm going to go ahead and save the serializer, stop pi, come back to my views, import 03:02.040 --> 03:03.570 this up at the top. 03:03.570 --> 03:09.420 So I'm going to do comma to do complete serializer and now pass this through to say, OK, I want to 03:09.420 --> 03:11.220 use this to complete serializer. 03:11.670 --> 03:14.730 So now that we have a name for this class, let's go ahead and copy this. 03:15.180 --> 03:18.990 Come back to our your LS and update that there. 03:18.990 --> 03:22.950 Save that file and let's come back and finish the view here. 03:23.100 --> 03:26.610 So again, the user shouldn't be passing us any information. 03:26.610 --> 03:31.020 They've already passed the I.D. of the particular to do that they want to finish. 03:31.050 --> 03:36.320 So what we've got to do is add a custom function here when someone is going to do the updates or and 03:36.320 --> 03:40.140 say def perform, underscore, not create. 03:40.200 --> 03:46.530 We want update and inside of here we're going to pass self and also the serializer. 03:47.960 --> 03:53.690 We go through our colon and then inside of this function, basically, we want to set the date completed 03:53.690 --> 03:56.390 to the current time and then save that serializer. 03:56.450 --> 04:02.930 So what we're gonna do is we're going to get our serializer dott instance. 04:03.050 --> 04:05.570 So this gets the particular object that we're working with. 04:05.570 --> 04:07.880 Remember, it already exists in our database. 04:08.340 --> 04:14.000 And then we're going to take it's date completed and we're gonna set this equal to and we want to use 04:14.420 --> 04:17.180 a time zone which we're gonna have to import this code. 04:17.180 --> 04:20.840 So let's go ahead and scroll up to the top of our file here. 04:21.290 --> 04:24.460 And in order to get the timezones here, this comes from Django. 04:24.470 --> 04:34.490 We're going to say that we want to say from Django spelled Django correctly, from Django Dot Utils. 04:35.520 --> 04:39.610 OK, we want to import time zone. 04:40.630 --> 04:41.050 Excellent. 04:41.300 --> 04:41.480 OK. 04:41.980 --> 04:46.510 Now that we've got that in place, we can say time zone dot now, which just takes the current time 04:46.510 --> 04:47.380 and sets that there. 04:47.890 --> 04:51.400 And once that's in place, we basically just want to save that object. 04:51.400 --> 04:58.330 With this new completed dates, we're going to say our serializer dot save with open and closed parentheses 04:58.330 --> 04:58.600 there. 04:59.050 --> 05:04.660 And I realize here that I have misspelled perform, so I need per form update. 05:04.750 --> 05:06.520 Okay, let's go ahead and save that. 05:06.980 --> 05:13.540 Come back to our project and let's go ahead and, you know, say, hey, I want to complete. 05:13.570 --> 05:14.380 Number five here. 05:14.380 --> 05:20.250 So I'm going to go say to dos slash number five, slash complete. 05:20.850 --> 05:21.170 OK. 05:22.300 --> 05:27.040 Now here it looks like it's not happy with our serializer. 05:27.940 --> 05:30.370 And let's go ahead and look inside of our project. 05:30.580 --> 05:32.730 Inside of the serializer is that pie. 05:33.520 --> 05:34.450 Now, look at this. 05:35.020 --> 05:36.370 I have horrific spelling. 05:36.400 --> 05:40.540 So let's do fields sort of flails or whatever I had spelt there. 05:40.600 --> 05:43.150 OK, so let's save this comeback. 05:43.570 --> 05:44.530 Reload this page. 05:44.560 --> 05:44.800 OK. 05:44.860 --> 05:52.050 So it's telling me I can do a put or a patch here for this, you know, to do number five here. 05:52.060 --> 05:53.920 So it's saying hey, you can't do a get. 05:53.930 --> 05:59.290 But great thing about the jingle rest framework is it sort of helps people to understand what your API 05:59.290 --> 06:00.160 is able to do. 06:00.520 --> 06:06.190 So in this case, if I simply just do the put or a patch, I do a put here, it's going to market as 06:06.190 --> 06:06.540 complete. 06:06.550 --> 06:07.390 So I'm gonna do that. 06:07.870 --> 06:08.870 It gives me the 200. 06:08.980 --> 06:09.460 OK. 06:09.940 --> 06:17.680 So now if I go back to my listed to DOS here, you'll notice that I no longer have that number five 06:17.680 --> 06:17.890 there. 06:17.890 --> 06:22.150 I just have the one that says add an API to my site, which we're not done with that yet. 06:22.180 --> 06:28.720 But if I go to my API and I say I want to see my completed look at that completed to do, we just did 06:28.720 --> 06:30.340 that via the API. 06:30.370 --> 06:36.340 If you want to confirm, we can go to our completed here via the Web portion here and see that beautiful 06:36.340 --> 06:36.730 to do. 06:36.820 --> 06:40.660 Oh, it's so awesome that we've got all this functionality in place. 06:40.750 --> 06:46.420 So at this point we are basically done with the to do app. 06:46.450 --> 06:48.750 It can do everything that we want it to do. 06:48.760 --> 06:53.170 We can, you know, see the current ones, the completed ones that we can create. 06:53.800 --> 06:58.510 If we have an ape to do, we can, you know, update it, we can complete it. 06:58.540 --> 06:59.410 We can delete it. 07:00.190 --> 07:05.860 All these different pieces here, really the only thing that we're missing is how does someone even 07:05.950 --> 07:11.720 access the API with, you know, their current logged and credentials like the current user, Nick. 07:11.770 --> 07:17.140 How does Nick use this if he's not going to be using the Web browser here, like if he was on a mobile 07:17.140 --> 07:18.910 app, like if we were going to use Kerl? 07:19.090 --> 07:20.140 How would that happen? 07:20.440 --> 07:24.560 And, you know, how do we allow someone to sign up via our API? 07:24.970 --> 07:30.130 These are all questions that we're going to answer but know that the to do functionality is all complete 07:30.520 --> 07:34.330 up till this point from here on out, it's really just authentication.