-
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.
- Loading branch information
Showing
4 changed files
with
106 additions
and
7 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
43 changes: 43 additions & 0 deletions
43
reviews/migrations/0004_remove_review_parent_id_review_parent_and_more.py
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,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), | ||
), | ||
] |
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,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) |