From d6454f2e5ab9b88684fa2f989ff1eb1afe6aba4d Mon Sep 17 00:00:00 2001 From: K02D Date: Sun, 28 Jan 2024 19:45:17 -0500 Subject: [PATCH] add andreas's second commit --- friends/tests.py | 32 +++++++++++++++++++++++--------- friends/urls.py | 4 ++-- friends/views.py | 41 ++++++++++++++++++++--------------------- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/friends/tests.py b/friends/tests.py index e705c952e..85fcc8982 100644 --- a/friends/tests.py +++ b/friends/tests.py @@ -20,18 +20,32 @@ def setUp(self): self.user2 = create_user(username="Bob", password="security2") self.student2 = create_student(user=self.user2) + def test_send_request(self): self.client.force_login(self.user1) - response = self.client.post(reverse("send friend request", args=[self.student1.id])) + response = self.client.post(reverse("send friend request", args=[self.user1.id])) self.assertEquals(response.status_code, 200) self.assertEqual(1, len(FriendRequest.objects.all())) print(response) + + + def test_accept_request(self): + + self.client.force_login(self.user1) + friend_request = FriendRequest.objects.create( + from_friend=self.student1, + to_friend=self.student2 + ) + send = self.client.post(reverse("accept friend request", args=[friend_request.id])) + self.assertEquals(send.status_code, 200) + self.assertEqual(0, len(FriendRequest.objects.all())) - - # def test_accept_request(self): - # send = self.client.post(f"accept_friend_request/{self.student1.id}/", send_friend_request(request=self.student1, UserId=self.student2.id), name="send friend request") - # response = self.client.get(f"accept_friend_request/{self.student1.id}/", accept_friend_request(request=self.student2, requestId=self.student1.id), name="accept friend request") - # self.assertEquals(response.status_code, 200) - - # # self.assertEquals(response, self.student1.id) - # self.assertEqual(0, len(FriendRequest.objects.all())) \ No newline at end of file + def test_reject_request(self): + self.client.force_login(self.user1) + friend_request = FriendRequest.objects.create( + from_friend=self.student1, + to_friend=self.student2 + ) + send = self.client.post(reverse("reject friend request", args=[friend_request.id])) + self.assertEquals(send.status_code, 200) + self.assertEqual(0, len(FriendRequest.objects.all())) \ No newline at end of file diff --git a/friends/urls.py b/friends/urls.py index 1d1b52a6e..1eea17a31 100644 --- a/friends/urls.py +++ b/friends/urls.py @@ -2,6 +2,6 @@ from friends.views import send_friend_request, accept_friend_request, reject_friend_request urlpatterns = [ path('send_friend_request//', send_friend_request, name="send friend request"), -path('accept_friend_request//', accept_friend_request, name="accept friend request"), -path('reject_friend_request//', reject_friend_request, name="reject friend request"), +path('accept_friend_request//', accept_friend_request, name="accept friend request"), +path('reject_friend_request//', reject_friend_request, name="reject friend request"), ] \ No newline at end of file diff --git a/friends/views.py b/friends/views.py index c28cb85c6..d04aa5443 100644 --- a/friends/views.py +++ b/friends/views.py @@ -5,29 +5,28 @@ # Create your views here. def send_friend_request(request, userId): from_user = request.user - from_student = Student.objects.get_or_create(user=from_user) - to_student = Student.objects.get(id=userId) + from_student = Student.objects.get(user=from_user) + to_student = Student.objects.get(user_id=userId) friend_request , created = FriendRequest.objects.get_or_create(from_friend=from_student, to_friend=to_student) - + if created: return HttpResponse('Friend request created', 200) else: return HttpResponse('Friend request failed', 400) - -def accept_friend_request(request, requestId): - friend_request = FriendRequest.objects.get(id=requestId) - if friend_request == request: - friend_request.to_user.friends.add(friend_request.from_user) - friend_request.from_user.friends.add(friend_request.to_user) - friend_request.delete() - return HttpResponse('Friend request accepted', 200) - else: - return HttpResponse('Friend request rejected', 400) - -def reject_friend_request(request, requestId): - friend_request = FriendRequest.objects.get(id=requestId) - if friend_request.user == request.user: - friend_request.delete() - return HttpResponse('Friend request rejected', 200) - else: - return HttpResponse('Friend request failed to reject', 400) + +def accept_friend_request(request, friendRequestId): + friend_request = FriendRequest.objects.get(id=friendRequestId) + + friend_request.to_friend.friends.add(friend_request.from_friend) + friend_request.from_friend.friends.add(friend_request.to_friend) + friend_request.delete() + return HttpResponse('Friend request accepted', 200) + #else: + # return HttpResponse('Friend request rejected', 400) + +def reject_friend_request(request, friendRequestId): + friend_request = FriendRequest.objects.get(id=friendRequestId) + friend_request.delete() + return HttpResponse('Friend request rejected', 200) + # else: + # return HttpResponse('Friend request failed to reject', 400) \ No newline at end of file