Skip to content

Commit

Permalink
chore: 중복 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 5, 2024
1 parent 5c03914 commit cc06ec8
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 123 deletions.
122 changes: 0 additions & 122 deletions friends/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,128 +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 RestaurantSerializer(serializers.ModelSerializer):
# reviews = serializers.SerializerMethodField()

Expand Down
1 change: 0 additions & 1 deletion friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
FriendRequestSerializer,
RestaurantlistSerializer,
FriendRecommendSerializer,
RestaurantSerializer,
)

# from .serializers import FriendSerializer, FriendRequestSerializer
Expand Down

0 comments on commit cc06ec8

Please sign in to comment.