from django_filters import rest_framework as filters
from apps.qm.models import QuaStat, TestItem


class QuaStatFilter(filters.FilterSet):
    class Meta:
        model = QuaStat
        fields = {
            "material": ["exact"],
            "testitem": ["exact"],
            "belong_dept": ["exact"],
            "sflog": ["exact"],
            "sflog__mgroup": ["exact"],
            "sflog__end_time": ["day", "month", "year", "lt"],
        }


class TestItemFilter(filters.FilterSet):
    tag = filters.CharFilter(method='filter_tag')

    class Meta:
        model = TestItem
        fields = ['tag']

    def filter_tag(self, queryset, name, value):
        return queryset.filter(tags__contains=value)
