from apps.utils.viewsets import CustomModelViewSet
from apps.cms.models import Article
from apps.cms.serializers import ArticleSerializer
from rest_framework.decorators import action
from rest_framework.serializers import Serializer
from rest_framework.response import Response
# Create your views here.

class ArticleViewSet(CustomModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
    ordering = ["is_top", "-create_time"]

    @action(methods=['put'], detail=True, serializer_class=Serializer, perms_map={"put": "article.update"})
    def toggle_top(self, request, pk=None):
        """文章置顶/取消

        文章置顶/取消
        """
        ins:Article = self.get_object()
        ins.is_top = False if ins.is_top else True
        ins.save()
        return Response({"id": ins.id})