# coding=utf-8
import json
from django.db.models import Sum, F
from rest_framework.decorators import action
from rest_framework.views import APIView
from django.db import transaction
from django.db.models import Q
from utils.custom_modelviewset import CustomModelViewSet
from utils import response_ok, response_error
from utils.permission import IsAdministrator
from apps.examination.examquestion.serializers import *
from apps.examination.examquestion.filters import *
from apps.system.models import SysLog

class ExamQuestionViewSet(CustomModelViewSet):
    permission_classes = [IsAdministrator, ]
    queryset = ExamQuestion.objects.filter(delete=False)
    serializer_class = ExamQuestionSerializer

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

    def destroy(self, request, *args, **kwargs):
        with transaction.atomic():
            instance = self.get_object()
            instance.delete = True
            instance.save()
            SysLog.objects.addnew(request.user, SysLog.INSERT, u"删除试题,id=%d" % instance.id)
        return response_ok()
