# coding=utf-8

from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework.views import APIView
from utils import response_ok, response_error
from apps.system.models import SysLog
from utils.permission import IsAdministrator, IsStaff
from utils.custom_modelviewset import CustomModelViewSet
from apps.knowledge.models import KnowledgeBase, KnowledgeBaseFeedback
from apps.knowledge.filters import KnowledgeBaseFilter
from apps.knowledge.serializers import KnowledgeBaseSerializer, KnowledgeBaseFeedbackSerializer


class KnowledgeViewSet(ReadOnlyModelViewSet):
    permission_classes = [IsStaff, ]
    queryset = KnowledgeBase.objects.filter(delete=False)
    serializer_class = KnowledgeBaseSerializer

    def filter_queryset(self, queryset):
        f = KnowledgeBaseFilter(self.request.GET, queryset=queryset)
        return f.qs


class KnowledgeBaseFeedbackViewSet(CustomModelViewSet):
    permission_classes = [IsStaff, ]
    queryset = KnowledgeBaseFeedback.objects.filter()
    serializer_class = KnowledgeBaseFeedbackSerializer

    def perform_create(self, serializer):
        super(KnowledgeBaseFeedbackViewSet, self).perform_create(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        SysLog.objects.addnew(self.request.user, SysLog.INSERT, u'添加知识错误反馈，id=%d' % instance.id, validated_data)
