Skip to content

Commit

Permalink
Merge branch 'develop' into feature/crawling
Browse files Browse the repository at this point in the history
  • Loading branch information
GalaxyDimension authored Aug 3, 2024
2 parents f5471e8 + d299535 commit 02cccf5
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 29 deletions.
5 changes: 3 additions & 2 deletions accounts/admin.py
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)
2 changes: 1 addition & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class User(AbstractUser):
reliability = models.SmallIntegerField(default=100)

USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["name"]
REQUIRED_FIELDS = ["name", "username"]

def __str__(self):
return self.name
66 changes: 66 additions & 0 deletions friends/serializers.py
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__"
11 changes: 11 additions & 0 deletions friends/urls.py
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"),
]
58 changes: 57 additions & 1 deletion friends/views.py
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)
2 changes: 2 additions & 0 deletions mustgou/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,5 @@
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_URL = "/auth/login/"
1 change: 1 addition & 0 deletions mustgou/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
path("admin/", admin.site.urls),
path("", include("restaurants.urls")),
path("auth/", include("accounts.urls")),
path("", include("friends.urls")),
]
17 changes: 17 additions & 0 deletions restaurants/migrations/0005_restaurant_food_type.py
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),
),
]
3 changes: 2 additions & 1 deletion restaurants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Restaurant(models.Model):
name = models.CharField(max_length=255)
food_type = models.CharField(max_length=20, null=True, blank=True)
rating_naver = models.DecimalField(
max_digits=5, decimal_places=2, null=True, blank=True
)
Expand All @@ -24,7 +25,7 @@ def rating_average(self):
valid_ratings = [rating for rating in ratings if rating is not None]
if valid_ratings:
return sum(valid_ratings) / len(valid_ratings)
return None
return 0


class SearchHistory(models.Model):
Expand Down
49 changes: 40 additions & 9 deletions restaurants/serializers.py
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()
2 changes: 1 addition & 1 deletion restaurants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


urlpatterns = [
# path("restaurants/", views.restaurant_list, name="restaurant-list"),
# path("restaurants/", views.restaurant_list, name="restaurant-list"),
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")),
path("search/", views.search, name="search"),
path(
Expand Down
Loading

0 comments on commit 02cccf5

Please sign in to comment.