From 025e0d1193764153124a2ca2f5ab884b3e77f718 Mon Sep 17 00:00:00 2001 From: dkfla Date: Wed, 7 Aug 2024 00:40:56 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=8B=B5=EA=B8=80=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mustgou/urls.py | 1 + .../0005_remove_review_parent_reply.py | 50 +++++++++++++++++++ reviews/models.py | 10 ++-- reviews/serializers.py | 8 ++- reviews/urls.py | 7 +++ reviews/views.py | 49 +++++++++--------- 6 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 reviews/migrations/0005_remove_review_parent_reply.py create mode 100644 reviews/urls.py diff --git a/mustgou/urls.py b/mustgou/urls.py index c7ec8dd..55a21a3 100644 --- a/mustgou/urls.py +++ b/mustgou/urls.py @@ -22,4 +22,5 @@ path("", include("restaurants.urls")), path("auth/", include("accounts.urls")), path("", include("friends.urls")), + path("", include("reviews.urls")), ] diff --git a/reviews/migrations/0005_remove_review_parent_reply.py b/reviews/migrations/0005_remove_review_parent_reply.py new file mode 100644 index 0000000..596f41d --- /dev/null +++ b/reviews/migrations/0005_remove_review_parent_reply.py @@ -0,0 +1,50 @@ +# Generated by Django 4.2.14 on 2024-08-06 15:08 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("reviews", "0004_remove_review_parent_id_review_parent_and_more"), + ] + + operations = [ + migrations.RemoveField( + model_name="review", + name="parent", + ), + migrations.CreateModel( + name="Reply", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("content", models.CharField(max_length=255)), + ("date", models.DateTimeField(auto_now_add=True)), + ( + "review", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="replies", + to="reviews.review", + ), + ), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + ] diff --git a/reviews/models.py b/reviews/models.py index 6705ede..c5d3cae 100644 --- a/reviews/models.py +++ b/reviews/models.py @@ -13,9 +13,13 @@ class Review(models.Model): content = models.CharField(max_length=255) recommend_count = models.IntegerField(default=0) decommend_count = models.IntegerField(default=0) - parent = models.ForeignKey( - "self", null=True, blank=True, on_delete=models.CASCADE, related_name="replies" - ) + date = models.DateTimeField(auto_now_add=True) + + +class Reply(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + review = models.ForeignKey(Review, on_delete=models.CASCADE, related_name="replies") + content = models.CharField(max_length=255) date = models.DateTimeField(auto_now_add=True) diff --git a/reviews/serializers.py b/reviews/serializers.py index 051714a..204ee65 100644 --- a/reviews/serializers.py +++ b/reviews/serializers.py @@ -1,5 +1,5 @@ from rest_framework import serializers -from .models import Review, Recommend +from .models import Review, Reply, Recommend class ReviewSerializer(serializers.ModelSerializer): @@ -8,6 +8,12 @@ class Meta: fields = "__all__" +class ReplySerializer(serializers.ModelSerializer): + class Meta: + model = Reply + fields = "__all__" + + class RecommendSerializer(serializers.ModelSerializer): class Meta: model = Recommend diff --git a/reviews/urls.py b/reviews/urls.py new file mode 100644 index 0000000..6658cba --- /dev/null +++ b/reviews/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from . import views + + +urlpatterns = [ + path("reviews//", views.reply_write, name="reply-write"), +] diff --git a/reviews/views.py b/reviews/views.py index a58af4d..64baac4 100644 --- a/reviews/views.py +++ b/reviews/views.py @@ -4,7 +4,7 @@ from accounts.models import User from restaurants.models import Restaurant from .models import Review -from .serializers import ReviewSerializer +from .serializers import ReviewSerializer, ReplySerializer # from rest_framework.authentication import TokenAuthentication # from rest_framework.permissions import IsAuthenticated @@ -23,31 +23,34 @@ def review_write(request, pk): {"error": "Restaurant not found"}, status=status.HTTP_404_NOT_FOUND ) - request.data["user"] = user.id - request.data["restaurant"] = restaurant.id - - try: - data = request.data - except ValueError: - return Response({"detail": "Invalid JSON"}, status=status.HTTP_400_BAD_REQUEST) - - parent_id = data.get("parent") - if parent_id: - try: - parent_review = Review.objects.get(id=parent_id) - if parent_review.parent is not None: - return Response( - {"error": "Replies to replies are not allowed"}, - status=status.HTTP_400_BAD_REQUEST, - ) - data["parent"] = parent_review.id - except Review.DoesNotExist: - return Response( - {"error": "Parent review not found"}, status=status.HTTP_404_NOT_FOUND - ) + data = request.data + data["user"] = user.id + data["restaurant"] = restaurant.id serializer = ReviewSerializer(data=data) if serializer.is_valid(): serializer.save(user=user, restaurant=restaurant) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + +@api_view(["POST"]) +# @authentication_classes([TokenAuthentication]) +# @permission_classes([IsAuthenticated]) +def reply_write(request, pk): + user = User.objects.get(id=21) # 임시 유저 지정, 추후 삭제 + + try: + review = Review.objects.get(id=pk) + except Review.DoesNotExist: + return Response({"error": "Review not found"}, status=status.HTTP_404_NOT_FOUND) + + data = request.data + data["user"] = user.id + data["review"] = review.id + + serializer = ReplySerializer(data=data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)