from rest_framework import generics, permissions
from django.contrib.auth.models import User

from rest_framework.decorators import api_view, action  
from rest_framework.response import Response  
from rest_framework.reverse import reverse  

from rest_framework import viewsets

from .permissions import IsOwnerOrReadOnly
from .models import Post
from .serializers import PostSerializer, UserSerializer


@api_view(['GET'])
def api_root(request, format=None):
    return Response({
        'users': reverse('user-list', request=request, format=format),
        'posts': reverse('post-list', request=request, format=format)
    })

class UserViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list` and `detail` actions.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated,]

class PostViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.

    """
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = [permissions.IsAuthenticated,
                          IsOwnerOrReadOnly]

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user) 

"""    
class PostList(generics.ListCreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user) 


class PostDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer 
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)

class UserList(generics.ListAPIView): 
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveAPIView): 
    queryset = User.objects.all()
    serializer_class = UserSerializer
"""
from rest_framework import generics, permissions
from django.contrib.auth.models import User

from rest_framework.decorators import api_view, action  
from rest_framework.response import Response  
from rest_framework.reverse import reverse  

from rest_framework import viewsets

from .permissions import IsOwnerOrReadOnly
from .models import Post
from .serializers import PostSerializer, UserSerializer


@api_view(['GET'])
def api_root(request, format=None):
    return Response({
        'users': reverse('user-list', request=request, format=format),
        'posts': reverse('post-list', request=request, format=format)
    })

class UserViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list` and `detail` actions.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated,]

class PostViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.

    """
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = [permissions.IsAuthenticated,
                          IsOwnerOrReadOnly]

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user) 

"""    
class PostList(generics.ListCreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user) 


class PostDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Post.objects.all()
    serializer_class = PostSerializer 
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)

class UserList(generics.ListAPIView): 
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveAPIView): 
    queryset = User.objects.all()
    serializer_class = UserSerializer
"""
This viewset automatically provides `list` and `detail` actions. This viewset automatically provides `list`, `create`, `retrieve`,     `update` and `destroy` actions. class PostList(generics.ListCreateAPIView):     queryset = Post.objects.all()     serializer_class = PostSerializer     permission_classes = (permissions.IsAuthenticatedOrReadOnly,)      def perform_create(self, serializer):         serializer.save(owner=self.request.user)    class PostDetail(generics.RetrieveUpdateDestroyAPIView):     queryset = Post.objects.all()     serializer_class = PostSerializer      permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)  class UserList(generics.ListAPIView):      queryset = User.objects.all()     serializer_class = UserSerializer   class UserDetail(generics.RetrieveAPIView):      queryset = User.objects.all()     serializer_class = UserSerializer