diff --git a/friends/models.py b/friends/models.py index 3f0404b..15c93ae 100644 --- a/friends/models.py +++ b/friends/models.py @@ -5,12 +5,28 @@ class Friend(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="friends") + friend = models.ForeignKey(User, on_delete=models.CASCADE, related_name="friend_of") + + class Meta: + unique_together = ("user", "friend") + + +class FriendRequest(models.Model): STATE_CHOICES = [ - ("request", "Request"), - ("approve", "Approve"), - ("deny", "Deny"), + ("pending", "Pending"), + ("accepted", "Accepted"), + ("declined", "Declined"), ] - user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="friends") - friend = models.ForeignKey(User, on_delete=models.CASCADE, related_name="friend_of") + from_user = models.ForeignKey( + User, on_delete=models.CASCADE, related_name="sent_friend_requests" + ) + to_user = models.ForeignKey( + User, on_delete=models.CASCADE, related_name="received_friend_requests" + ) + created_at = models.DateTimeField(auto_now_add=True) state = models.CharField(max_length=20, choices=STATE_CHOICES, default="request") + + class Meta: + unique_together = ("from_user", "to_user") diff --git a/friends/serializers.py b/friends/serializers.py index f7c9ead..13aee59 100644 --- a/friends/serializers.py +++ b/friends/serializers.py @@ -1,5 +1,5 @@ from rest_framework import serializers -from .models import Friend +from .models import Friend, FriendRequest from restaurants.models import Restaurant, UserRestaurantsList from accounts.models import User @@ -11,16 +11,25 @@ class Meta: class FriendRequestSerializer(serializers.ModelSerializer): - user = UserSerializer(read_only=True) + id = serializers.IntegerField(source="from_user.id") + name = serializers.CharField(source="from_user.name") + profile_img = serializers.URLField(source="from_user.profile_img.url") + reliability = serializers.IntegerField(source="from_user.reliability") common_restaurant_count = serializers.SerializerMethodField() class Meta: - model = Friend - fields = ["user", "state", "common_restaurant_count"] + model = FriendRequest + fields = [ + "id", + "name", + "profile_img", + "reliability", + "common_restaurant_count", + ] def get_common_restaurant_count(self, obj): try: - user = obj.user + user = obj.from_user # friend_user = self.context.get('request').user friend_user = User.objects.get(id=21) @@ -80,13 +89,28 @@ def get_common_restaurants(self, obj): ).values("restaurant__name", "restaurant__image_url")[:2] return friend_restaurants + def to_representation(self, instance): + representation = super().to_representation(instance) + include_restaurants = self.context.get("include_restaurants", False) + if not include_restaurants: + representation.pop("common_restaurants") + return representation + class FriendSerializer(serializers.ModelSerializer): - friend = UserSerializer(read_only=True) + id = serializers.IntegerField(source="friend.id") + name = serializers.CharField(source="friend.name") + profile_img = serializers.URLField(source="friend.profile_img.url") + reliability = serializers.IntegerField(source="friend.reliability") class Meta: model = Friend - fields = ["friend", "state"] + fields = [ + "id", + "name", + "profile_img", + "reliability", + ] class RestaurantlistSerializer(serializers.ModelSerializer): diff --git a/friends/views.py b/friends/views.py index f59d06d..39406c6 100644 --- a/friends/views.py +++ b/friends/views.py @@ -12,7 +12,7 @@ FriendRecommendSerializer, ) from accounts.models import User -from .models import Friend +from .models import Friend, FriendRequest from django.views.decorators.csrf import csrf_exempt from django.db.models import Count, Q import random @@ -44,14 +44,15 @@ def friend_restaurant_list(request, id): # @login_required def friend_list(request): try: + # user = request.user user = User.objects.get(id=21) - friend_request = Friend.objects.filter(friend=user, state="request") + friend_request = FriendRequest.objects.filter(to_user=user, state="pending") friend_request_serialized = FriendRequestSerializer( friend_request, context={"request": request}, many=True ).data - friends = Friend.objects.filter(user=user, state="approve") + friends = Friend.objects.filter(user=user) friends_serialized = FriendSerializer(friends, many=True).data user_restaurants = set( @@ -71,7 +72,9 @@ def friend_list(request): ) friend_recommend_serialized = FriendRecommendSerializer( - potential_friends, many=True, context={"request": request, "user": user} + potential_friends, + many=True, + context={"request": request, "user": user, "include_restaurants": False}, ).data data = { @@ -91,6 +94,7 @@ def friend_list(request): # @login_required def friend_recommend(request): try: + # user = request.user user = User.objects.get(id=21) user_restaurants = set( @@ -112,7 +116,8 @@ def friend_recommend(request): if potential_friends: random_friend = random.choice(potential_friends) friend_recommend_serialized = FriendRecommendSerializer( - random_friend, context={"request": request, "user": user} + random_friend, + context={"request": request, "user": user, "include_restaurants": True}, ).data return Response(friend_recommend_serialized)