from django.utils.translation import gettext_lazy as _

from rest_framework import filters
from rest_framework.compat import coreapi, coreschema


class SavedFilter(filters.BaseFilterBackend):
    query_param = "saved"

    def filter_queryset(self, request, queryset, view):
        key = request.query_params.get(self.query_param, None)
        available_values = {"True": True, "true": True, "False": False, "false": False}

        if not key or key not in available_values.keys():
            return queryset

        value = available_values[key]
        return queryset.filter(saved=value)

    def get_schema_fields(self, view):
        return [
            coreapi.Field(
                name=self.query_param,
                required=False,
                location="query",
                schema=coreschema.String(
                    title=str(self.query_param),
                    description=str(_("Wether posts should be saved or not")),
                ),
            )
        ]
