Skip to content

Commit

Permalink
fix: 충돌 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ybkang1108 committed Aug 7, 2024
1 parent dad6544 commit 59caa0f
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 134 deletions.
128 changes: 0 additions & 128 deletions friends/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 1 addition & 6 deletions friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,7 +14,6 @@
FriendRequestSerializer,
RestaurantlistSerializer,
FriendRecommendSerializer,
# RestaurantSerializer,
)

# from .serializers import FriendSerializer, FriendRequestSerializer
Expand Down Expand Up @@ -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)

0 comments on commit 59caa0f

Please sign in to comment.