From 30bdeda7f923f29a172c581aed062a1ad2228e3f Mon Sep 17 00:00:00 2001 From: dkfla Date: Tue, 6 Aug 2024 23:05:16 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=95=9C=EC=A4=84=ED=8F=89=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- restaurants/urls.py | 6 ++- ...review_parent_id_review_parent_and_more.py | 43 +++++++++++++++ reviews/models.py | 10 ++-- reviews/views.py | 54 ++++++++++++++++++- 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 reviews/migrations/0004_remove_review_parent_id_review_parent_and_more.py diff --git a/restaurants/urls.py b/restaurants/urls.py index 5e7d72f..86362d5 100644 --- a/restaurants/urls.py +++ b/restaurants/urls.py @@ -1,5 +1,6 @@ from django.urls import path, include, re_path from . import views +from reviews.views import review_write urlpatterns = [ @@ -13,7 +14,10 @@ name="add-remove-restaurant", ), path( - "restaurants//detail", views.restaurant_detail, name="restaurant-detail" + "restaurants//detail/", + views.restaurant_detail, + name="restaurant-detail", ), path("restaurants/", views.user_restaurant_list, name="user-restaurant-list"), + path("restaurants//reviews/", review_write, name="review-write"), ] diff --git a/reviews/migrations/0004_remove_review_parent_id_review_parent_and_more.py b/reviews/migrations/0004_remove_review_parent_id_review_parent_and_more.py new file mode 100644 index 0000000..0820273 --- /dev/null +++ b/reviews/migrations/0004_remove_review_parent_id_review_parent_and_more.py @@ -0,0 +1,43 @@ +# Generated by Django 4.2.14 on 2024-08-06 13:45 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("reviews", "0003_alter_review_restaurant"), + ] + + operations = [ + migrations.RemoveField( + model_name="review", + name="parent_id", + ), + migrations.AddField( + model_name="review", + name="parent", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="replies", + to="reviews.review", + ), + ), + migrations.AlterField( + model_name="review", + name="date", + field=models.DateTimeField(auto_now_add=True), + ), + migrations.AlterField( + model_name="review", + name="decommend_count", + field=models.IntegerField(default=0), + ), + migrations.AlterField( + model_name="review", + name="recommend_count", + field=models.IntegerField(default=0), + ), + ] diff --git a/reviews/models.py b/reviews/models.py index d26c3f1..6705ede 100644 --- a/reviews/models.py +++ b/reviews/models.py @@ -11,10 +11,12 @@ class Review(models.Model): Restaurant, on_delete=models.CASCADE, related_name="reviews" ) content = models.CharField(max_length=255) - recommend_count = models.IntegerField() - decommend_count = models.IntegerField() - parent_id = models.IntegerField(null=True, blank=True) - date = models.DateTimeField() + 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 Recommend(models.Model): diff --git a/reviews/views.py b/reviews/views.py index fd0e044..a58af4d 100644 --- a/reviews/views.py +++ b/reviews/views.py @@ -1,3 +1,53 @@ -# from django.shortcuts import render +from rest_framework.decorators import api_view +from rest_framework.response import Response +from rest_framework import status +from accounts.models import User +from restaurants.models import Restaurant +from .models import Review +from .serializers import ReviewSerializer -# Create your views here. +# from rest_framework.authentication import TokenAuthentication +# from rest_framework.permissions import IsAuthenticated + + +@api_view(["POST"]) +# @authentication_classes([TokenAuthentication]) +# @permission_classes([IsAuthenticated]) +def review_write(request, pk): + user = User.objects.get(id=21) + + try: + restaurant = Restaurant.objects.get(pk=pk) + except Restaurant.DoesNotExist: + return Response( + {"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 + ) + + 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)