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

"""
import django_filters
from django import forms
from django.db.models import QuerySet

from django.utils.translation import gettext_lazy as _


class SelfCreatedTableFilterMixin(django_filters.FilterSet):
    """ A mixin for AbstractTableFilter

    Specialized on filtering recordable model types

    """
    sc = django_filters.BooleanFilter(
        method='filter_show_self_created',
        label=_("Show only self created"),
        label_suffix=_(""),
        widget=forms.CheckboxInput(
            attrs={
                "class": "form-check-input",
                "title": _("If activated only shows entries which have been created by you"),
            }
        ),
    )

    class Meta:
        abstract = True

    def filter_show_self_created(self, queryset, name, value) -> QuerySet:
        """ Filters queryset depending on value of 'self_created' setting

        Args:
            queryset ():
            name ():
            value ():

        Returns:

        """
        if value:
            return queryset.filter(
                created__user=self.user,
            )
        else:
            return queryset

