Skip to content

Commit

Permalink
the send_friend_request method uses Q objects to ensure that there is…
Browse files Browse the repository at this point in the history
… no existing pending friend request between the two users. If a pending request exists and it was sent by the current user, it cancels the request by deleting it. If the request was sent by the other user, it informs the current user that there is already a pending request from the other user. This way, the system handles both sending and canceling friend requests efficiently.I still need to work on cancel_friend_request function
  • Loading branch information
abbastoof committed Jul 12, 2024
1 parent 2798edb commit f63a09c
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions Backend/user_service/user_service/user_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .models import User, FriendRequest
from .rabbitmq_utils import consume_message, publish_message
from .serializers import UserSerializer, FriendSerializer
from django.db.models import Q


class UserViewSet(viewsets.ViewSet):
Expand Down Expand Up @@ -270,21 +271,36 @@ def send_friend_request(self, request, user_pk=None, pk=None):
try:
if (user_pk == pk):
raise ValidationError(detail={"You can't send a friend request to yourself"}, code=status.HTTP_400_BAD_REQUEST)
current_user = get_object_or_404(User, id=user_pk) # current user is sending a request to the target
receiver = get_object_or_404(User, id=pk) # target user is the receiver of the request
# This FriendRequest.objects.get_or_create() function will get the object or create it
friend_request, created = FriendRequest.objects.get_or_create(
sender_user = current_user,
receiver_user = receiver,
status = 'pending'
current_user = get_object_or_404(User, id=user_pk)
receiver = get_object_or_404(User, id=pk)
existing_request = FriendRequest.objects.filter(
(Q(sender_user=current_user) & Q(receiver_user=receiver) & Q(status='pending')) |
(Q(sender_user=receiver) & Q(receiver_user=current_user) & Q(status='pending'))
).first()
if existing_request:
if existing_request.sender_user == current_user:
existing_request.delete()
return Response({"detail:" "Friend request canceled"}, status=status.HTTP_200_OK)
else:
return Response({"detail": "You have a pending friend from this user."}, status=status.HTTP_400_BAD_REQUEST)

# friend_request, created = FriendRequest.objects.get_or_create(
# sender_user = current_user,
# receiver_user = receiver,
# status = 'pending'
# )
friend_request = FriendRequest.objects.create(
sender_user=current_user,
receiver_user=receiver,
status='pending'
)
if created:
friend_request.save()
return Response({"detail": "Requested"}, status=status.HTTP_202_ACCEPTED)
return Response({"detail": f"Your request status is {friend_request.status}"}, status=status.HTTP_202_ACCEPTED)
# if created:
# friend_request.save()
return Response({"detail": "Friend request sent"}, status=status.HTTP_202_ACCEPTED)
except User.DoesNotExist:
return Response({"detail": "Invalid user_id"}, status=status.HTTP_400_BAD_REQUEST)

except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
def accept_friend_request(self, request, user_pk=None, pk=None):
try:
current_user = get_object_or_404(User, id=user_pk)
Expand Down

0 comments on commit f63a09c

Please sign in to comment.