from django.shortcuts import get_object_or_404
from rest_framework import generics
from rest_framework import viewsets

from . import models
from . import serializers

# Create your views here.

class ListCreateCourse(generics.ListCreateAPIView):
    queryset = models.Course.objects.all()
    serializer_class = serializers.CourseSerializer


class RetrieveUpdateDestroyCourse(generics.RetrieveUpdateDestroyAPIView):
    queryset = models.Course.objects.all()
    serializer_class = serializers.CourseSerializer


class ListCreateReview(generics.ListCreateAPIView):
    queryset = models.Review.objects.all()
    serializer_class = serializers.ReviewSerializer

    def get_queryset(self):
        return self.queryset.filter(course_id=self.kwargs.get('course_pk'))

    def perform_create(self, serializer):
        course = get_object_or_404(models.Course, pk=self.kwargs.get('course_pk'))
        serializer.save(course=course)


class RetrieveUpdateDestroyReview(generics.RetrieveUpdateDestroyAPIView):
    queryset = models.Review.objects.all()
    serializer_class = serializers.ReviewSerializer

    def get_object(self):
        return get_object_or_404(
            self.get_queryset(),
            course_id=self.kwargs.get('course_pk'),
            pk=self.kwargs.get('pk')
        )


class CourseViewSet(viewsets.ModelViewSet):
    queryset = models.Course.objects.all()
    serializer_class = serializers.CourseSerializer


class ReviewViewSet(viewsets.ModelViewSet):
    queryset = models.Review.objects.all()
    serializer_class = serializers.ReviewSerializer
@@ -111,16 +111,112 @@
 viewsets
+%0Afrom rest_framework.decorators import detail_route%0Afrom rest_framework.response import Response
 %0A%0Afrom .
@@ -1519,32 +1519,281 @@
 urseSerializer%0A%0A
+    @detail_route(methods=%5B'get'%5D)%0A    def reviews(self, request, pk=None):%0A        course = self.get_object()%0A        serializer = serializers.ReviewSerializer(%0A            course.reviews.all(), many=True)%0A        return Response(serializer.data)%0A%0A
 %0Aclass ReviewVie

add courses/1/reviews path using detail_route decorator