diff --git a/friends/serializers.py b/friends/serializers.py index e0d6254..13aee59 100644 --- a/friends/serializers.py +++ b/friends/serializers.py @@ -132,134 +132,6 @@ def get_rating_average(self, obj): return obj.rating_average() -class FriendRequestSerializer(serializers.ModelSerializer): - 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 = FriendRequest - fields = [ - "id", - "name", - "profile_img", - "reliability", - "common_restaurant_count", - ] - - def get_common_restaurant_count(self, obj): - try: - user = obj.from_user - # friend_user = self.context.get('request').user - friend_user = User.objects.get(id=21) - - user_restaurants = set( - UserRestaurantsList.objects.filter(user=user).values_list( - "restaurant_id", flat=True - ) - ) - friend_restaurants = set( - UserRestaurantsList.objects.filter(user=friend_user).values_list( - "restaurant_id", flat=True - ) - ) - return len(user_restaurants.intersection(friend_restaurants)) - except User.DoesNotExist: - return 0 - - -class FriendRecommendSerializer(serializers.ModelSerializer): - common_restaurant_count = serializers.SerializerMethodField() - common_restaurants = serializers.SerializerMethodField() - - class Meta: - model = User - fields = [ - "id", - "name", - "profile_img", - "reliability", - "common_restaurant_count", - "common_restaurants", - ] - - def get_common_restaurant_count(self, obj): - user = self.context.get("user") - user_restaurants = set( - UserRestaurantsList.objects.filter(user=user).values_list( - "restaurant_id", flat=True - ) - ) - friend_restaurants = set( - UserRestaurantsList.objects.filter(user=obj).values_list( - "restaurant_id", flat=True - ) - ) - return len(user_restaurants.intersection(friend_restaurants)) - - def get_common_restaurants(self, obj): - user = self.context.get("user") - user_restaurants = set( - UserRestaurantsList.objects.filter(user=user).values_list( - "restaurant_id", flat=True - ) - ) - friend_restaurants = UserRestaurantsList.objects.filter( - user=obj, restaurant_id__in=user_restaurants - ).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): - 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 = [ - "id", - "name", - "profile_img", - "reliability", - ] - - -class RestaurantlistSerializer(serializers.ModelSerializer): - rating_average = serializers.SerializerMethodField() - - class Meta: - model = Restaurant - fields = [ - "id", - "name", - "food_type", - "rating_average", - "latitude", - "longitude", - "image_url", - ] - - def get_rating_average(self, obj): - return obj.rating_average() - - -class FriendRequestSerializer(serializers.ModelSerializer): - class Meta: - model = Friend - fields = "__all__" - - class RestaurantSerializer(serializers.ModelSerializer): # reviews = serializers.SerializerMethodField() diff --git a/friends/views.py b/friends/views.py index 6117877..b7f6e66 100644 --- a/friends/views.py +++ b/friends/views.py @@ -2,10 +2,6 @@ from rest_framework.decorators import api_view from rest_framework.views import APIView -# from rest_framework.authentication import TokenAuthentication -# from rest_framework.permissions import IsAuthenticated -from rest_framework.views import APIView - # from rest_framework.authentication import TokenAuthentication # from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response @@ -18,7 +14,6 @@ FriendRequestSerializer, RestaurantlistSerializer, FriendRecommendSerializer, - # RestaurantSerializer, ) # from .serializers import FriendSerializer, FriendRequestSerializer @@ -221,4 +216,4 @@ def decline_request(self, request, friend_id): friend_request.state = "declined" friend_request.save() - return Response({"message": "친구 신청을 거절했습니다."}, status=status.HTTP_200_OK) + return Response({"message": "친구 신청을 거절했습니다."}, status=status.HTTP_200_OK) \ No newline at end of file