"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 18.08.22

"""
from django import forms
from django.db.models import QuerySet, Q
from django.utils.translation import gettext_lazy as _
import django_filters


class FileNumberTableFilterMixin(django_filters.FilterSet):
    rf = django_filters.CharFilter(
        method="filter_file_number",
        label=_(""),
        label_suffix=_(""),
        widget=forms.TextInput(
            attrs={
                "placeholder": _("File number"),
                "title": _("Search for file number"),
                "class": "form-control",
            }
        ),
    )

    def filter_file_number(self, queryset, name, value) -> QuerySet:
        queryset = queryset.filter(
            Q(responsible__registration_file_number__icontains=value) |
            Q(responsible__conservation_file_number__icontains=value)
        )
        return queryset
