Skip to content

Commit

Permalink
add andreas's second commit
Browse files Browse the repository at this point in the history
  • Loading branch information
K02D committed Jan 29, 2024
1 parent 43a55fb commit d6454f2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
32 changes: 23 additions & 9 deletions friends/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
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()))
4 changes: 2 additions & 2 deletions friends/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from friends.views import send_friend_request, accept_friend_request, reject_friend_request
urlpatterns = [
path('send_friend_request/<int:userId>/', send_friend_request, name="send friend request"),
path('accept_friend_request/<int:userId>/', accept_friend_request, name="accept friend request"),
path('reject_friend_request/<int:userId>/', reject_friend_request, name="reject friend request"),
path('accept_friend_request/<int:friendRequestId>/', accept_friend_request, name="accept friend request"),
path('reject_friend_request/<int:friendRequestId>/', reject_friend_request, name="reject friend request"),
]
41 changes: 20 additions & 21 deletions friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d6454f2

Please sign in to comment.