forked from kimnamheeee/UnivGPT_BE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
54 lines (45 loc) · 2.37 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from django.shortcuts import render
# Create your views here.
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Comment
from prompt.models import Prompt
from .serializers import CommentSerializer
class CommentListView(APIView):
def get(self, request):
prompt_id = request.GET.get('prompt')
print("****************")
print(prompt_id)
if not prompt_id:
return Response({"detail": "missing fields ['prompt']"}, status=status.HTTP_400_BAD_REQUEST)
if not Prompt.objects.filter(id=prompt_id).exists():
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)
comments = Comment.objects.filter(prompt_id=prompt_id)
serializer = CommentSerializer(comments, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request):
author = request.user
prompt_id = request.data.get('prompt')
content = request.data.get('content')
if not request.user.is_authenticated:
return Response({"detail": "Authentication credentials not provided"}, status=status.HTTP_401_UNAUTHORIZED)
if not prompt_id or not content:
return Response({"detail": "missing fields ['prompt', 'content']"}, status=status.HTTP_400_BAD_REQUEST)
if not Prompt.objects.filter(id=prompt_id).exists():
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)
comment = Comment.objects.create(prompt_id=prompt_id, author=author, content=content)
serializer = CommentSerializer(comment)
return Response(serializer.data, status=status.HTTP_201_CREATED)
class CommentDetailView(APIView):
def delete(self, request, comment_id):
if not request.user.is_authenticated:
return Response({"detail": "Authentication credentials not provided"}, status=status.HTTP_401_UNAUTHORIZED)
try:
comment = Comment.objects.get(id=comment_id)
except:
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)
if request.user != comment.author:
return Response({"detail": "Permission denied"}, status=status.HTTP_401_UNAUTHORIZED)
comment.delete()
return Response(status=status.HTTP_204_NO_CONTENT)