from django.core.validators import EMPTY_VALUES
from django_filters import rest_framework as filters

from main import models


class AwardFilter(filters.FilterSet):
    """Award filter set."""

    establishment_id = filters.NumberFilter(field_name='object_id', )
    product_id = filters.NumberFilter(field_name='object_id', )
    employee_id = filters.NumberFilter(field_name='object_id', )

    class Meta:
        """Meta class."""

        model = models.Award
        fields = (
            'establishment_id',
            'product_id',
            'employee_id',
            'state',
            'award_type',
            'vintage_year',
        )

    def by_establishment_id(self, queryset, name, value):
        if value not in EMPTY_VALUES:
            return queryset.by_establishment_id(value, content_type='establishment')
        return queryset

    def by_product_id(self, queryset, name, value):
        if value not in EMPTY_VALUES:
            return queryset.by_product_id(value, content_type='product')
        return queryset

    def by_employee_id(self, queryset, name, value):
        if value not in EMPTY_VALUES:
            return queryset.by_employee_id(value, content_type='establishmentemployee')
        return queryset
