Replies: 3 comments
-
Can you explain what this would look like? In theory, we have the |
Beta Was this translation helpful? Give feedback.
-
def has_request(self, from_user, to_user):
""" Has a user sent a friend request to the other? """
sent_requests = cache.get(cache_key("sent_requests", from_user.pk))
received_requests = cache.get(cache_key("requests", to_user.pk))
if sent_requests and any(r.to_user == to_user for r in sent_requests):
return True
elif received_requests and any(r.from_user == from_user for r in received_requests):
return True
else:
return FriendshipRequest.objects.filter(from_user=from_user, to_user=to_user).exists() Usage: def add_friend(self, from_user, to_user, message=None):
""" Create a friendship request """
if from_user == to_user:
raise ValidationError("Users cannot be friends with themselves")
if self.are_friends(from_user, to_user):
raise AlreadyFriendsError("Users are already friends")
- if (FriendshipRequest.objects.filter(from_user=from_user, to_user=to_user).exists()):
+ if self.has_request(from_user, to_user):
raise AlreadyExistsError("You already requested friendship from this user.")
- if (FriendshipRequest.objects.filter(from_user=to_user, to_user=from_user).exists()):
+ if self.has_request(to_user, from_user):
raise AlreadyExistsError("This user already requested friendship from you.") |
Beta Was this translation helpful? Give feedback.
-
Just like @acjh says, it should be identical to the are_friends in that you can pass in a sender and recipient and check if that friend request exists. This way, I can modify someone's profile page to show the status of a friend request. The implementation should be a helper function like described by Aaron Chong. I can try to work on it myself. |
Beta Was this translation helpful? Give feedback.
-
It would be nice to have a function to check if a friend request exists between two users. It would be like the are_friends method for checking if two users are friends.
Beta Was this translation helpful? Give feedback.
All reactions