-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/crawling
- Loading branch information
Showing
13 changed files
with
252 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# from django.contrib import admin | ||
from django.contrib import admin | ||
from .models import User | ||
|
||
# Register your models here. | ||
admin.site.register(User) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,74 @@ | ||
from rest_framework import serializers | ||
from .models import Friend | ||
from restaurants.models import Restaurant, UserRestaurantsList | ||
from accounts.models import User | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = ["id", "name", "profile_img", "reliability"] | ||
|
||
|
||
class FriendRequestSerializer(serializers.ModelSerializer): | ||
user = UserSerializer(read_only=True) | ||
common_restaurant_count = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Friend | ||
fields = ["user", "state", "common_restaurant_count"] | ||
|
||
def get_common_restaurant_count(self, obj): | ||
try: | ||
user = obj.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 FriendSerializer(serializers.ModelSerializer): | ||
friend = UserSerializer(read_only=True) | ||
|
||
class Meta: | ||
model = Friend | ||
fields = ["friend", "state"] | ||
|
||
|
||
class RestaurantlistSerializer(serializers.ModelSerializer): | ||
rating_average = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Restaurant | ||
fields = ["id", "name", "food_type", "rating_average", "latitude", "longitude"] | ||
|
||
def get_rating_average(self, obj): | ||
return obj.rating_average() | ||
|
||
|
||
class RestaurantSerializer(serializers.ModelSerializer): | ||
# reviews = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Restaurant | ||
fields = "__all__" | ||
|
||
|
||
class FriendRestaurantSerializer(serializers.ModelSerializer): | ||
restaurant = RestaurantlistSerializer() | ||
|
||
class Meta: | ||
model = UserRestaurantsList | ||
fields = "__all__" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path( | ||
"friends/<int:id>/restaurants/", | ||
views.friend_restaurant_list, | ||
name="friend-restaurant-list", | ||
), | ||
path("friends/", views.friend_list, name="friend-list"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,59 @@ | ||
# from django.shortcuts import render | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
|
||
# Create your views here. | ||
# from django.contrib.auth.decorators import login_required | ||
from restaurants.models import UserRestaurantsList | ||
from .serializers import ( | ||
FriendSerializer, | ||
FriendRequestSerializer, | ||
FriendRestaurantSerializer, | ||
) | ||
from accounts.models import User | ||
from .models import Friend | ||
from django.views.decorators.csrf import csrf_exempt | ||
|
||
|
||
@api_view(["GET"]) | ||
# @login_required | ||
def friend_restaurant_list(request, id): | ||
try: | ||
# id에 해당하는 친구를 가져옴 | ||
friend = User.objects.get(id=id) | ||
|
||
# 친구의 맛집 리스트를 가져옴 | ||
friend_restaurants = UserRestaurantsList.objects.filter(user=friend) | ||
serializer = FriendRestaurantSerializer(friend_restaurants, many=True) | ||
|
||
return Response({"restaurants": serializer.data}, status=status.HTTP_200_OK) | ||
except User.DoesNotExist: | ||
return Response( | ||
{"message": "Friend not found"}, status=status.HTTP_404_NOT_FOUND | ||
) | ||
|
||
|
||
@csrf_exempt | ||
@api_view(["GET"]) | ||
# @login_required | ||
def friend_list(request): | ||
try: | ||
user = User.objects.get(id=21) | ||
|
||
friend_request = Friend.objects.filter(friend=user, state="request") | ||
friend_request_serialized = FriendRequestSerializer( | ||
friend_request, context={"request": request}, many=True | ||
).data | ||
|
||
friends = Friend.objects.filter(user=user, state="approve") | ||
friends_serialized = FriendSerializer(friends, many=True).data | ||
|
||
data = { | ||
"friend_request": friend_request_serialized, | ||
"friends": friends_serialized, | ||
} | ||
|
||
return Response(data) | ||
|
||
except User.DoesNotExist: | ||
return Response({"message": "User not found"}, status=status.HTTP_404_NOT_FOUND) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.14 on 2024-08-03 07:13 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("restaurants", "0004_userrestaurantslist"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="restaurant", | ||
name="food_type", | ||
field=models.CharField(blank=True, max_length=20, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,72 @@ | ||
from rest_framework import serializers | ||
from .models import Restaurant, SearchHistory, UserRestaurantsList | ||
|
||
from reviews.serializers import ReviewSerializer | ||
|
||
|
||
class RestaurantSerializer(serializers.ModelSerializer): | ||
reviews = serializers.SerializerMethodField() | ||
rating_average = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Restaurant | ||
fields = "__all__" | ||
|
||
def get_reviews(self, obj): | ||
reviews = obj.reviews.order_by("-recommend_count")[:4] | ||
serializer = ReviewSerializer(reviews, many=True) | ||
return serializer.data | ||
def get_rating_average(self, obj): | ||
return obj.rating_average() | ||
|
||
|
||
class RestaurantListSerializer(serializers.ModelSerializer): | ||
rating_average = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Restaurant | ||
fields = ["name", "rating_average", "address"] | ||
fields = [ | ||
"name", | ||
"rating_average", | ||
"address", | ||
] | ||
|
||
def get_rating_average(self, obj): | ||
return obj.rating_average | ||
return obj.rating_average() | ||
|
||
|
||
class SearchHistorySerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = SearchHistory | ||
fields = ["query", "timestamp"] | ||
fields = ["id", "query", "timestamp"] | ||
|
||
|
||
class RestaurantlistSerializer(serializers.ModelSerializer): | ||
rating_average = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Restaurant | ||
fields = ["id", "name", "food_type", "rating_average", "latitude", "longitude"] | ||
|
||
def get_rating_average(self, obj): | ||
return obj.rating_average() | ||
|
||
|
||
class UserRestaurantListSerializer(serializers.ModelSerializer): | ||
restaurant = RestaurantSerializer() | ||
restaurant = RestaurantlistSerializer() | ||
|
||
class Meta: | ||
model = UserRestaurantsList | ||
fields = "__all__" | ||
|
||
|
||
class RestaurantDetailSerializer(serializers.ModelSerializer): | ||
reviews = serializers.SerializerMethodField() | ||
rating_average = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Restaurant | ||
fields = "__all__" | ||
|
||
def get_reviews(self, obj): | ||
reviews = obj.reviews.order_by("-recommend_count")[:4] | ||
serializer = ReviewSerializer(reviews, many=True) | ||
return serializer.data | ||
|
||
def get_rating_average(self, obj): | ||
return obj.rating_average() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.