# 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 apps.practise.errorbook.models import ErrorBook
from apps.practise.errorbook.filters import *
from apps.practise.errorbook.serializers import *
from apps.foundation.models import Subject, Chapter
from apps.foundation.serializers import SubjectSimpleSerializer, ChapterSimpleSerializer


class ErrorBookViewSet(ReadOnlyModelViewSet):
    permission_classes = [IsStaff, ]
    queryset = ErrorBook.objects.filter()
    serializer_class = ErrorBookSerializer

    def filter_queryset(self, queryset):
        # 排序使用filter排序功能 OrderingFilter  参数 sort=last_time 按最后出错时间排序 sort=-last_time按最后出错时间倒序排序
        queryset = queryset.filter(create_user=self.request.user)
        f = ErrorBookFilter(self.request.GET, queryset=queryset)
        return f.qs


class DictView(APIView):
    permission_classes = [IsStaff, ]

    def get(self, request):
        ret = {
            'subject': SubjectSimpleSerializer(Subject.objects.filter(delete=False), many=True).data
        }
        return response_ok(ret)
