comment create and get view
This commit is contained in:
@@ -1,3 +1,29 @@
|
||||
from django.shortcuts import render
|
||||
from django.core.paginator import Paginator
|
||||
from rest_framework.views import APIView
|
||||
from .models import *
|
||||
from .serializers import *
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
||||
|
||||
|
||||
class CommentView(APIView):
|
||||
serializer_class = CommentSerializer
|
||||
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||
def get(self, request, product_pk):
|
||||
product = get_object_or_404(Product, id=product_pk)
|
||||
comments = product.comments.filter(show=True)
|
||||
comments_ser = self.serializer_class(instance=comments, many=True)
|
||||
return Response({'comments': comments_ser.data}, status=status.HTTP_200_OK)
|
||||
|
||||
def post(self, request, product_pk):
|
||||
comment_ser = CommentSerializer(data=request.data)
|
||||
product = get_object_or_404(Product, id=product_pk)
|
||||
if comment_ser.is_valid():
|
||||
comment_ser.save(product=product)
|
||||
#TODO comment_ser.save(product=product, user=request.user)
|
||||
return Response(comment_ser.data, status=status.HTTP_201_CREATED)
|
||||
return Response(comment_ser.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Create your views here.
|
||||
|
||||
Reference in New Issue
Block a user