from rest_framework import viewsets, permissions, status, response

from review.models import Review
from review.permissions import ReviewPermission
from review.serializers import ReviewSerializer


class ReviewViewSet(viewsets.ModelViewSet):
    serializer_class = ReviewSerializer
    permission_classes = (permissions.IsAuthenticated, ReviewPermission)
    queryset = Review.objects.filter(is_active=True)
    filter_fields = ('object_review', 'created_by',)
    lookup_field = 'id'

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save(created_by=request.user)
        headers = self.get_success_headers(serializer.data)
        return response.Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
