Skip to content

Commit

Permalink
feat: 한줄평 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
dkfla committed Aug 6, 2024
1 parent 86401a2 commit 30bdeda
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
6 changes: 5 additions & 1 deletion restaurants/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.urls import path, include, re_path
from . import views
from reviews.views import review_write


urlpatterns = [
Expand All @@ -13,7 +14,10 @@
name="add-remove-restaurant",
),
path(
"restaurants/<int:pk>/detail", views.restaurant_detail, name="restaurant-detail"
"restaurants/<int:pk>/detail/",
views.restaurant_detail,
name="restaurant-detail",
),
path("restaurants/", views.user_restaurant_list, name="user-restaurant-list"),
path("restaurants/<int:pk>/reviews/", review_write, name="review-write"),
]
Original file line number Diff line number Diff line change
@@ -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),
),
]
10 changes: 6 additions & 4 deletions reviews/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
54 changes: 52 additions & 2 deletions reviews/views.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 30bdeda

Please sign in to comment.