WEBVTT 00:03.190 --> 00:09.950 We now need to create this signals for our project then as we said signals are really awesome because 00:10.100 --> 00:16.310 they allow certain senders to notify you set of receivers that some action has taken place elsewhere 00:16.340 --> 00:20.350 in the framework and in the context of our profile CPA project. 00:20.360 --> 00:24.410 We want to use signals to create a new instance of a profile. 00:24.590 --> 00:31.370 As soon as a new is this of use that is created so that later on when we are going to set up a project 00:31.400 --> 00:37.370 in order to provide some authentication and registration and points we are going to have an underlying 00:37.430 --> 00:44.150 system in charge of making the connection between the two models because let's repeat it one more time. 00:44.150 --> 00:48.950 They use that model is in charge or voluntary authentication related tasks. 00:49.040 --> 00:55.780 And so the first thing that you want to do use Cygnus in a Django project is to create it signals that 00:55.790 --> 00:58.430 by file within the application. 00:58.430 --> 01:01.720 Where do you want these signal communication to take place. 01:02.000 --> 01:04.410 And of course we need to make some imports. 01:04.460 --> 01:07.790 First of all I'm going to import the use that model. 01:07.790 --> 01:23.960 So from Django dot country north of DOT models import user Dan from Django dot D.B. dot models dot signals 01:24.500 --> 01:31.270 import post safe Dan from Django dot dispatch. 01:32.240 --> 01:41.120 Import receiver and finally from profiles dot for this import profile. 01:41.540 --> 01:44.190 So let's discuss what we've just imported. 01:44.210 --> 01:52.760 Clearly we've imported the user and profile models that we've imported the past save signal and a decorator 01:53.000 --> 01:54.020 receiver. 01:54.050 --> 02:01.720 We're going to use the first save a sig now using the user model as the sender to send a signal. 02:02.000 --> 02:05.120 Every time a user instance is saved. 02:05.300 --> 02:13.250 So right after a user instance gets saved a signal is sent and this signal is going to be received from 02:13.250 --> 02:17.400 a function that will go into decode aid using the receiver decorator. 02:17.530 --> 02:23.250 And not only we are going to receive these signal when a user is Nancy's safe. 02:23.310 --> 02:31.130 We're also going to know if they user instance that has sent the signal as just being created or just 02:31.130 --> 02:37.190 modified and so from within the created function we are then going to create a new profile instance 02:37.280 --> 02:42.990 when needed connecting it to the user instance that has sent the signal pretty easy. 02:43.220 --> 02:46.100 So let's implement all of these. 02:46.130 --> 02:53.900 We're going to create the function like a normal function def create profile which is going to accept 02:53.900 --> 03:03.300 a couple of parameters sender is this created and keyword arguments. 03:03.440 --> 03:11.430 We need to decorate create a profile using the receiver signal which is going to accept signal posts 03:11.450 --> 03:16.640 save and which also needs to know the sender. 03:16.640 --> 03:19.650 In this case use it. 03:20.140 --> 03:25.580 Now we can also print just to make it more explicit. 03:25.800 --> 03:37.130 Created the to best created and so are even created again if the instance if they use any instance is 03:37.130 --> 03:46.310 just being created then we're going to create a new profiling instance or profile dot objects to create 03:46.430 --> 03:50.510 with user equals instance. 03:50.510 --> 03:57.470 Again if that specific user instance is just being created we can then create a new profiling star's 03:57.890 --> 04:04.820 profile dot objects dot create with user equals instance. 04:04.940 --> 04:07.850 So again let's explain one more time what's going on here. 04:07.850 --> 04:13.760 We've created this function that is in charge of creating a new profile instances and because of the 04:13.760 --> 04:22.100 receiver and liquidator our function knows that it has to expect a signal sent by user so that in the 04:22.100 --> 04:29.930 context of this specific signal that we are using past save right after a user instance is being saved 04:30.170 --> 04:36.260 the create profile function basically is going to be triggered and because of the created parameter 04:36.530 --> 04:42.620 the function will know if it s to create a new profile instance or not let us know move to the Dan there. 04:42.650 --> 04:52.760 E need file of the profile application because we need to add one line of code default up configuration 04:52.820 --> 05:02.990 config profiles dot ups dot profiles config and to finish their configuration we need to open up the 05:03.080 --> 05:13.680 apps dot by file profiles config and within the class we need to define ready which is going to accept 05:14.070 --> 05:14.690 self. 05:15.430 --> 05:26.340 And we're going to import the signals files so import profiles dot signals now that everything is setup. 05:26.750 --> 05:33.190 Let's restart our development server and let's just leave the terminal open. 05:33.190 --> 05:43.270 Let's go back to Chrome and from here a we can't go back to date site administration interface. 05:43.270 --> 05:49.310 Let's go back to home users and let's modify the ad mean instance. 05:49.370 --> 05:58.210 For example let's say I want to add a first name here Michela going to save and if we now go back to 05:58.260 --> 06:04.480 Venezuela to your code you see right here in the terminal we got created false. 06:04.510 --> 06:08.170 What happened is that when we've saved the admin use it. 06:08.200 --> 06:13.360 Instance right after we've saved it the post saved signal is being sent. 06:13.570 --> 06:20.190 It has been received from the create a profile function which as therefore printed dislike. 06:20.410 --> 06:28.490 But it has not created any new profile instance because clearly created was set to false. 06:28.510 --> 06:35.540 So let's go back one more time to the site administration interface and let us create a new user instead. 06:35.560 --> 06:42.740 So I'm just going to call it random to give a password like so save. 06:43.420 --> 06:45.100 Let's go back to business. 06:45.100 --> 06:47.730 Your code you see. 06:47.820 --> 06:50.240 Now we get created through. 06:50.490 --> 06:52.580 So let's go back once again. 06:53.690 --> 06:59.420 To add been home profiles and entities are new. 06:59.420 --> 07:03.710 Profiling is this that is being created automatically perfect. 07:03.770 --> 07:10.180 Let's now complete the setup for our project by creating this utilize their classes for our models. 07:10.220 --> 07:21.750 So yeah I can just close this one and settings seek had been and instead site it profiles up I'm never 07:21.750 --> 07:26.100 going to create an API folder and weave in this one. 07:26.830 --> 07:43.040 Realizes that by so first of all from rest framework import scene ISIS then from profiles that models 07:44.030 --> 07:47.830 import profile and profile status. 07:47.960 --> 07:50.540 Let's create the Syrian eyes as for our profile first. 07:50.570 --> 07:59.430 So classic profile Syrian eyes that ISIS dot model analyzer. 07:59.510 --> 08:05.230 And so yeah first of all user equals see that ISIS dot string related field. 08:05.240 --> 08:17.190 We've read only equals through then a third equals ISIS dot image field. 08:17.210 --> 08:26.780 We read only through and yet on passing read only equals through because we are going to create a specific 08:26.840 --> 08:27.970 serialized it. 08:28.070 --> 08:33.710 We are going to use only to update the avatar field in our profiles. 08:33.890 --> 08:39.800 And that's because it is so much better off to just create the specific end point to update the avatars 08:39.860 --> 08:40.980 in our profiles. 08:41.000 --> 08:47.690 This is also the same strategy adopted by most social networks that allow you to only update one media 08:47.690 --> 08:49.250 file at a time. 08:49.250 --> 09:03.100 So class Meta model equals profile and we can now set fields equals 0 considering that any way we've 09:03.100 --> 09:07.060 set the way we want to manage user and arbiter and right here. 09:07.060 --> 09:12.730 So let's now create these profile avatars utilize their profile. 09:13.170 --> 09:21.160 How about a nicer she's going to extend more than she realized that just as well. 09:21.160 --> 09:23.030 And here we can't copy. 09:23.350 --> 09:24.700 Plus matter. 09:25.120 --> 09:29.240 But this time we're in fields we're going to define only. 09:29.500 --> 09:30.360 How about that. 09:30.490 --> 09:31.410 Like so. 09:31.700 --> 09:38.520 And let's not define the last satellites that we're going to need plus profile starters. 09:38.890 --> 09:50.640 See I said screen to extend more than seat Eliza and Europe first of all user profile equals serialize 09:50.670 --> 09:54.860 that's not the string related field we've read only through. 09:55.650 --> 10:03.120 And then basically classmate is going to be very similar but of course the model is going to be profile 10:03.210 --> 10:04.490 status. 10:04.490 --> 10:04.740 OK. 10:04.770 --> 10:08.300 So that was it for the set up of our profile CPA project. 10:08.310 --> 10:13.860 See you in the next lecture where we're going to talk about a very important subject authentication 10:13.950 --> 10:14.940 in January's framework.