1 00:00:00,680 --> 00:00:03,050 Creating our relationships is straight-forward. 2 00:00:03,050 --> 00:00:06,770 Since the relationship is a model we'll just need to make a new instance. 3 00:00:06,770 --> 00:00:09,150 Let's go back to Workspaces and set up a couple of views and 4 00:00:09,150 --> 00:00:10,840 the appropriate HTML links to handle this. 5 00:00:12,270 --> 00:00:15,660 Okay, so, let's see about building a couple of views. 6 00:00:15,660 --> 00:00:19,280 One for following a user and one for unfollowing a user. 7 00:00:20,280 --> 00:00:24,970 So, I'm gonna do these, let's scroll this up here, I'm gonna do these to where we, 8 00:00:24,970 --> 00:00:28,210 we do a follow, and then, the user name. 9 00:00:28,210 --> 00:00:33,250 We could this with like, follow, and then, a, an ID, if we wanted. 10 00:00:33,250 --> 00:00:36,440 I think the user name, though, makes a nicer looking URL, and 11 00:00:36,440 --> 00:00:39,360 if you were doing this with say, a public app instead of this one, 12 00:00:39,360 --> 00:00:44,440 where it's kind of inside of a company kind of an intranet app. 13 00:00:44,440 --> 00:00:49,340 Then exposing the IDs could potentially open you up to some security problems, 14 00:00:49,340 --> 00:00:53,420 but you'd weigh that with the rest of your team and with your own needs. 15 00:00:53,420 --> 00:00:56,370 So we're gonna have this at /follow/. 16 00:00:56,370 --> 00:01:00,370 And then we're gonna wrap this with login_required, and 17 00:01:00,370 --> 00:01:02,725 we're gonna say follow(username). 18 00:01:03,840 --> 00:01:06,053 And then so we need to do a try here. 19 00:01:06,053 --> 00:01:11,285 And the to_user is gonna be models.User.get 20 00:01:11,285 --> 00:01:16,910 models.User.username is like username the, 21 00:01:16,910 --> 00:01:20,660 the double stars here is a case insensitive search. 22 00:01:20,660 --> 00:01:24,480 So where the username is this username, but it could be uppercase, 23 00:01:24,480 --> 00:01:26,132 lowercase, whatever. 24 00:01:26,132 --> 00:01:32,600 Except models.DoesNotExist. 25 00:01:32,600 --> 00:01:35,420 We need to do something, but for now, let's just pass, and, and 26 00:01:35,420 --> 00:01:36,760 we'll just have it go on. 27 00:01:37,890 --> 00:01:41,040 Else, so the pass didn't happen. 28 00:01:41,040 --> 00:01:47,410 Then we're gonna try models.Relationship.create. 29 00:01:47,410 --> 00:01:51,240 And the from_user is going to be our g.user. 30 00:01:51,240 --> 00:01:52,945 And we have to get that current_object. 31 00:01:54,980 --> 00:01:56,650 Oops, forgot my comma. 32 00:01:57,898 --> 00:02:03,170 And the to_user is gonna be the user that we looked up, up on line 120. 33 00:02:03,170 --> 00:02:08,560 So, the exception here would be 34 00:02:08,560 --> 00:02:15,870 a models.IntegrityError since we have the uniqueness constraint on there. 35 00:02:15,870 --> 00:02:20,230 Then if we try to create another record for the same, 36 00:02:20,230 --> 00:02:24,650 like, I tried to follow you twice, then it would trigger this integrity error. 37 00:02:24,650 --> 00:02:29,010 If that happens, we're just gonna pass, because, really, things are okay. 38 00:02:29,010 --> 00:02:32,430 And if that pass doesn't happen, then we're gonna flash up a message, 39 00:02:32,430 --> 00:02:37,700 we're gonna say You're now following, and then we'll put in a name. 40 00:02:38,810 --> 00:02:42,580 And let's use to_user.username. 41 00:02:42,580 --> 00:02:46,310 We could use the username that comes in to the URL, but they might have typed it 42 00:02:46,310 --> 00:02:49,790 slightly different, so let's use the actual username. 43 00:02:49,790 --> 00:02:52,670 And we're gonna send that back with a success category. 44 00:02:52,670 --> 00:02:55,310 Let me get back over here. 45 00:02:55,310 --> 00:03:01,460 And we want to do return redirect url_for stream and 46 00:03:01,460 --> 00:03:05,950 the username is going to be the username, again, 47 00:03:05,950 --> 00:03:11,935 we should probably do to_user.username just to be safe. 48 00:03:11,935 --> 00:03:17,850 Alright, so, pull this down a little bit so we can see the whole function. 49 00:03:17,850 --> 00:03:21,580 So, we're doing this /follow/ login_required, and 50 00:03:21,580 --> 00:03:24,560 then, we're gonna try and find the user. 51 00:03:24,560 --> 00:03:27,800 If that user doesn't exist we'll, we'll do something with this later. 52 00:03:27,800 --> 00:03:29,240 If the user does exist, 53 00:03:29,240 --> 00:03:34,180 then we're gonna create this relationship and, then we're gonna flash up a message. 54 00:03:34,180 --> 00:03:40,140 So, let's actually take this whole thing and copy it and then paste it. 55 00:03:41,300 --> 00:03:46,320 And let's change this one to unfollow, unfollow. 56 00:03:47,480 --> 00:03:47,980 Okay. 57 00:03:47,980 --> 00:03:56,110 We still want to get the user, but instead of calling create we want to do get. 58 00:03:56,110 --> 00:03:59,570 And then we want to call the delete_instance. 59 00:03:59,570 --> 00:04:02,280 And so what we're, what we're doing here is, we're calling this get. 60 00:04:02,280 --> 00:04:05,530 We're getting the same relationship that we had before. 61 00:04:05,530 --> 00:04:08,360 But now we're gonna delete it, when it comes up. 62 00:04:08,360 --> 00:04:12,530 And let's say You've unfollowed. 63 00:04:12,530 --> 00:04:16,030 And then we'll put in the username and we still leave it a success, 64 00:04:16,030 --> 00:04:20,700 because they successfully did the thing that they wanted to do. 65 00:04:21,910 --> 00:04:24,090 Alright, so those are our two views. 66 00:04:24,090 --> 00:04:29,230 So let's go add a couple of things in to our templates. 67 00:04:29,230 --> 00:04:34,360 And first of all let's go to the user_stream template and 68 00:04:34,360 --> 00:04:36,220 here we have these followers and following. 69 00:04:36,220 --> 00:04:39,760 Instead of the zeroes lets put in a couple of counts. 70 00:04:39,760 --> 00:04:44,820 So we can say user.followers().count, 71 00:04:44,820 --> 00:04:50,290 and here, we can say user.following().count. 72 00:04:50,290 --> 00:04:55,730 And I forgot my closing brackets right there, or my closing braces, rather. 73 00:04:56,840 --> 00:05:00,717 And then, here, we need to put in a follow/unfollow button. 74 00:05:01,780 --> 00:05:08,420 And so, let's do, if current_user.is_authenticated, 75 00:05:08,420 --> 00:05:12,130 cuz we only want them to be able to do this if they're logged in, all right. 76 00:05:12,130 --> 00:05:20,180 And then inside there, if user does not = current_user. 77 00:05:20,180 --> 00:05:21,960 End that if. 78 00:05:21,960 --> 00:05:26,565 Then let's say, if not user in 79 00:05:26,565 --> 00:05:32,170 current_user.following, and end that if. 80 00:05:32,170 --> 00:05:35,790 All right, so let's look at what we've got so far. 81 00:05:35,790 --> 00:05:36,920 So make sure they're logged in. 82 00:05:38,510 --> 00:05:41,690 We make sure that we're not looking at the user that's logged in. 83 00:05:41,690 --> 00:05:48,095 And then we make sure that the user is not already being followed by this user. 84 00:05:48,095 --> 00:05:53,102 Okay, so if all that's true [LAUGH] then we need to print out an href 85 00:05:53,102 --> 00:05:54,847 url_for follow. 86 00:05:56,220 --> 00:06:00,180 And the username would be user.username. 87 00:06:00,180 --> 00:06:05,380 And we'll do a class on this of small, and we'll say, follow. 88 00:06:05,380 --> 00:06:08,140 And really now we need an else. 89 00:06:10,020 --> 00:06:12,120 And we want almost this exact same thing. 90 00:06:12,120 --> 00:06:13,200 So I'm just gonna copy it again. 91 00:06:14,440 --> 00:06:17,980 And instead of follow, we want unfollow. 92 00:06:17,980 --> 00:06:20,710 And we want unfollow there as well. 93 00:06:22,120 --> 00:06:22,810 Okay. And then, 94 00:06:22,810 --> 00:06:25,970 we're still gonna call our super, and we're still gonna do our end block. 95 00:06:25,970 --> 00:06:27,650 So, that's all normal. 96 00:06:28,740 --> 00:06:31,670 And, let's go try this out. 97 00:06:31,670 --> 00:06:33,050 So, I'm gonna actually log out. 98 00:06:33,050 --> 00:06:35,610 And then, I'm gonna sign up. 99 00:06:35,610 --> 00:06:36,300 I'm gonna do a new one. 100 00:06:36,300 --> 00:06:42,810 I'm gonna say test user, test@example.com. 101 00:06:42,810 --> 00:06:43,990 And a password. 102 00:06:45,280 --> 00:06:46,820 I'm gonna register. 103 00:06:46,820 --> 00:06:47,730 Sweet, okay. 104 00:06:47,730 --> 00:06:50,080 So now I'm gonna go look at Kenneth Love. 105 00:06:52,390 --> 00:06:53,220 Oh. Oh, that's right. 106 00:06:53,220 --> 00:06:54,110 I forgot to log in. 107 00:06:55,110 --> 00:06:55,908 So let's log in. 108 00:06:55,908 --> 00:07:00,794 And test@example.com. 109 00:07:00,794 --> 00:07:02,649 Password. 110 00:07:03,750 --> 00:07:04,690 Okay. So now I'm logged in. 111 00:07:04,690 --> 00:07:06,950 So that says Hello testuser! 112 00:07:06,950 --> 00:07:08,410 So, here's Kenneth Love. 113 00:07:08,410 --> 00:07:12,690 He has no followers and nobody is following, he's not following anybody. 114 00:07:12,690 --> 00:07:13,540 So let's go ahead and hit Follow. 115 00:07:13,540 --> 00:07:16,840 Look, our follower count went up. 116 00:07:16,840 --> 00:07:20,340 And the unfollow, the following is still empty. 117 00:07:20,340 --> 00:07:24,380 So now I'm gonna log out and I'm gonna log back in as me. 118 00:07:26,910 --> 00:07:29,070 And put in my password. 119 00:07:30,550 --> 00:07:35,400 Sweet, alright, and if I go look at my account then I see I have a follower. 120 00:07:35,400 --> 00:07:40,450 And let's go over here to test user, and 121 00:07:40,450 --> 00:07:44,550 I'm gonna go ahead, he's following one, or she, they. 122 00:07:44,550 --> 00:07:48,150 I'm gonna go and follow them, there's that. 123 00:07:48,150 --> 00:07:53,980 And now, let's go back to my account and check that out. 124 00:07:53,980 --> 00:07:56,710 So, I have one follower and I am following one person. 125 00:07:58,770 --> 00:08:01,400 Those buttons will make our app a lot easier to use. 126 00:08:01,400 --> 00:08:04,250 And thanks to our followers and following methods it was straight 127 00:08:04,250 --> 00:08:06,370 forward to add some information to our user pages too.