# Path: Blog/posts/api/paginations.py
# class PostLimitOffsetPagination(LimitOffsetPagination):
#     default_limit = 5
#     max_limit = 20
# 
# Path: Blog/posts/api/serializers.py
# class PostListSerializer(ModelSerializer):
#     url = detail_url
#     user = UserDetailSerializer(read_only=True)
#     image = SerializerMethodField()
#     html = SerializerMethodField()
#     class Meta:
#         model = Post
#         fields = [
#             'id',
#             'url',
#             'user',
#             'title',
#             'image',
#             'content',
#             'html',
#             'published'
#         ]
# 
#     def get_image(self,obj):
#         try:
#             image = obj.image.url
#         except:
#             image = None
#         return image
# 
#     def get_html(self,obj):
#         return obj.get_markdown()
# 
# class PostDetailSerializer(ModelSerializer):
#     url = detail_url
#     comments = SerializerMethodField()
#     user = UserDetailSerializer(read_only=True)
#     class Meta:
#         model = Post
#         fields = [
#             'id',
#             'url',
#             'user',
#             'title',
#             'slug',
#             'content',
#             'published',
#             'comments'
#         ]
# 
#     def get_comments(self, obj):
#         c_qs = Comment.objects.filter_by_instance(obj)
#         comments = CommentListSerializer(c_qs, many=True, context={"request":self.context['request']}).data
#         return comments
# 
# class PostCreateUpdateSerializer(ModelSerializer):
#     class Meta:
#         model = Post
#         fields = [
#             'title',
#             'content',
#             'published'
#         ]
# 
# Path: Blog/posts/api/permissions.py
# class IsOwnerOrReadOnly(BasePermission):
#     message = "You must be the owner of this object."
#     def has_object_permission(self, request, view, obj):
#         if request.method in SAFE_METHODS:  # For read only
#             return True
#         return obj.user == request.user


from django.db.models import Q
from rest_framework.generics import (
    ListAPIView,
    RetrieveAPIView,
    DestroyAPIView,
    CreateAPIView,
    RetrieveUpdateAPIView
)
from rest_framework.filters import OrderingFilter
from .paginations import PostLimitOffsetPagination
from rest_framework.permissions import (
    AllowAny,
    IsAdminUser,
)
from .serializers import (
    PostListSerializer, PostDetailSerializer, PostCreateUpdateSerializer
)
from posts.models import Post
from .permissions import IsOwnerOrReadOnly

class PostCreateAPIView(CreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostCreateUpdateSerializer
    permission_classes = [IsAdminUser]

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

class PostDetailAPIView(RetrieveAPIView):
    queryset = Post.objects.all()
    serializer_class = PostDetailSerializer
    permission_classes = [AllowAny]
    lookup_field = "slug"
    # for reference
    # lookup_url_kwarg = "abc"

class PostDeleteAPIView(DestroyAPIView):
    queryset = Post.objects.all()
    serializer_class = PostDetailSerializer
    permission_classes = [IsOwnerOrReadOnly]
    lookup_field = "slug"

class PostUpdateAPIView(RetrieveUpdateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostCreateUpdateSerializer
    lookup_field = "slug"

# Path: Blog/posts/api/paginations.py
# class PostLimitOffsetPagination(LimitOffsetPagination):
#     default_limit = 5
#     max_limit = 20
# 
# Path: Blog/posts/api/serializers.py
# class PostListSerializer(ModelSerializer):
#     url = detail_url
#     user = UserDetailSerializer(read_only=True)
#     image = SerializerMethodField()
#     html = SerializerMethodField()
#     class Meta:
#         model = Post
#         fields = [
#             'id',
#             'url',
#             'user',
#             'title',
#             'image',
#             'content',
#             'html',
#             'published'
#         ]
# 
#     def get_image(self,obj):
#         try:
#             image = obj.image.url
#         except:
#             image = None
#         return image
# 
#     def get_html(self,obj):
#         return obj.get_markdown()
# 
# class PostDetailSerializer(ModelSerializer):
#     url = detail_url
#     comments = SerializerMethodField()
#     user = UserDetailSerializer(read_only=True)
#     class Meta:
#         model = Post
#         fields = [
#             'id',
#             'url',
#             'user',
#             'title',
#             'slug',
#             'content',
#             'published',
#             'comments'
#         ]
# 
#     def get_comments(self, obj):
#         c_qs = Comment.objects.filter_by_instance(obj)
#         comments = CommentListSerializer(c_qs, many=True, context={"request":self.context['request']}).data
#         return comments
# 
# class PostCreateUpdateSerializer(ModelSerializer):
#     class Meta:
#         model = Post
#         fields = [
#             'title',
#             'content',
#             'published'
#         ]
# 
# Path: Blog/posts/api/permissions.py
# class IsOwnerOrReadOnly(BasePermission):
#     message = "You must be the owner of this object."
#     def has_object_permission(self, request, view, obj):
#         if request.method in SAFE_METHODS:  # For read only
#             return True
#         return obj.user == request.user


# Path: Blog/posts/api/views.py
from django.db.models import Q
from rest_framework.generics import (
    ListAPIView,
    RetrieveAPIView,
    DestroyAPIView,
    CreateAPIView,
    RetrieveUpdateAPIView
)
from rest_framework.filters import OrderingFilter
from .paginations import PostLimitOffsetPagination
from rest_framework.permissions import (
    AllowAny,
    IsAdminUser,
)
from .serializers import (
    PostListSerializer, PostDetailSerializer, PostCreateUpdateSerializer
)
from posts.models import Post
from .permissions import IsOwnerOrReadOnly


class PostCreateAPIView(CreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostCreateUpdateSerializer
    permission_classes = [IsAdminUser]

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

class PostDetailAPIView(RetrieveAPIView):
    queryset = Post.objects.all()
    serializer_class = PostDetailSerializer
    permission_classes = [AllowAny]
    lookup_field = "slug"
    # for reference
    # lookup_url_kwarg = "abc"

class PostDeleteAPIView(DestroyAPIView):
    queryset = Post.objects.all()
    serializer_class = PostDetailSerializer
    permission_classes = [IsOwnerOrReadOnly]
    lookup_field = "slug"

class PostUpdateAPIView(RetrieveUpdateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostCreateUpdateSerializer
    lookup_field = "slug"

    permission_classes = [IsOwnerOrReadOnly]