Skip to content

Commit

Permalink
feat: 모델 변경에 따른 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 4, 2024
1 parent e8b7e6c commit e3cdb41
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
26 changes: 21 additions & 5 deletions friends/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
38 changes: 31 additions & 7 deletions friends/serializers.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
15 changes: 10 additions & 5 deletions friends/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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 = {
Expand All @@ -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(
Expand All @@ -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)

Expand Down

0 comments on commit e3cdb41

Please sign in to comment.