# coding=utf-8
import django_filters

from .models import ExamPaper

class ExamPaperFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name="name", lookup_expr="icontains")
    subject = django_filters.CharFilter(field_name='subject_id')
    type = django_filters.CharFilter(field_name='type')
    status = django_filters.CharFilter(method='status_filter')

    def status_filter(self, queryset, name,value):
        from apps.examination.exam.models import ExamLog
        exampaper_ids = ExamLog.objects.filter(user=self.request.user, delete=False).values_list('exampaper_id', flat=True)
        if value == '1':
            queryset = queryset.filter(id__in=exampaper_ids)
        elif value == '2':
            queryset = queryset.exclude(id__in=exampaper_ids)
        return queryset

    class Meta:
        model = ExamPaper
        fields = "__all__"
