# coding=utf-8

from django.db import transaction
from rest_framework.views import APIView
from utils.permission import IsAdministrator
from utils import response_error, response_ok
from utils.custom_modelviewset import CustomModelViewSet
from apps.system.models import SysLog
from apps.foundation.models import Subject, Chapter
from apps.foundation.filters import SubjectFilter, ChapterFilter
from apps.foundation.serializers import SubjectSerializer, ChapterSerializer, SubjectSimpleSerializer, ChapterSimpleSerializer


class SubjectViewSet(CustomModelViewSet):
    permission_classes = [IsAdministrator, ]
    queryset = Subject.objects.filter(delete=False)
    serializer_class = SubjectSerializer

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

    def perform_create(self, serializer):
        super(SubjectViewSet, self).perform_create(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        SysLog.objects.addnew(self.request.user, SysLog.INSERT, u'添加科目[%s]，id=%d' % (instance.name, instance.id), validated_data)

    def perform_update(self, serializer):
        super(SubjectViewSet, self).perform_update(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        SysLog.objects.addnew(self.request.user, SysLog.UPDATE, u'修改科目[%s]，id=%d' % (instance.name, instance.id), validated_data)

    def destroy(self, request, *args, **kwargs):
        with transaction.atomic():
            instance = self.get_object()
            instance.delete = True
            instance.save()
            SysLog.objects.addnew(self.request.user, SysLog.DELETE, u'删除科目[%s]，id=%d' % (instance.name, instance.id))
        return response_ok()


class ChapterViewSet(CustomModelViewSet):
    permission_classes = [IsAdministrator, ]
    queryset = Chapter.objects.filter(delete=False, subject__delete=False)
    serializer_class = ChapterSerializer

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

    def perform_create(self, serializer):
        super(ChapterViewSet, self).perform_create(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        SysLog.objects.addnew(self.request.user, SysLog.INSERT, u'添加章节[%s]，id=%d' % (instance.name, instance.id), validated_data)

    def perform_update(self, serializer):
        super(ChapterViewSet, self).perform_update(serializer)
        instance = serializer.instance
        validated_data = serializer.validated_data
        SysLog.objects.addnew(self.request.user, SysLog.UPDATE, u'修改章节[%s]，id=%d' % (instance.name, instance.id), validated_data)

    def destroy(self, request, *args, **kwargs):
        with transaction.atomic():
            instance = self.get_object()
            instance.delete = True
            instance.save()
            SysLog.objects.addnew(self.request.user, SysLog.DELETE, u'删除章节[%s]，id=%d' % (instance.name, instance.id))
        return response_ok()


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

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