<commit_before>from django import forms
from django.db.models import Q
from django.utils.translation import npgettext, pgettext_lazy
from django_filters import (
    CharFilter,
    ChoiceFilter,
    DateFromToRangeFilter,
    ModelMultipleChoiceFilter,
    OrderingFilter,
    RangeFilter,
)

from ...core.filters import SortedFilterSet
from ...discount.models import Sale, Voucher
from ...product.models import Category
from ..widgets import DateRangeWidget

SORT_BY_FIELDS_SALE = {
    "name": pgettext_lazy("Sale list sorting option", "name"),
    "value": pgettext_lazy("Sale list sorting option", "value"),
    "start_date": pgettext_lazy("Sale list sorting option", "start_date"),
    "end_date": pgettext_lazy("Sale list sorting option", "end_date"),
}

SORT_BY_FIELDS_LABELS_VOUCHER = {
    "name": pgettext_lazy("Voucher list sorting option", "name"),
    "discount_value": pgettext_lazy("Voucher list sorting option", "discount_value"),
    "countries": pgettext_lazy("Voucher list sorting option", "countries"),
    "start_date": pgettext_lazy("Voucher list sorting option", "start_date"),
    "end_date": pgettext_lazy("Voucher list sorting option", "end_date"),
    "used": pgettext_lazy("Voucher list sorting option", "used"),
    "min_spent_amount": pgettext_lazy(
        "Voucher list sorting option", "min_spent_amount"
    ),
}

DISCOUNT_TYPE_CHOICES = (
    ("fixed", pgettext_lazy("Sale type filter choice", "USD")),
    ("percentage", pgettext_lazy("Sale type filter choice", "%")),
)


def filter_by_date_range(queryset, name, value):
    q = Q()
    if value.start:
        q = Q(start_date__gte=value.start)
    if value.stop:
        if value.start:
            q |= Q(end_date__lte=value.stop)
        else:
            q = Q(end_date__lte=value.stop)
    return queryset.filter(q)


class SaleFilter(SortedFilterSet):
    name = CharFilter(
        label=pgettext_lazy("Sale list filter label", "Name"), lookup_expr="icontains"
    )
    categories = ModelMultipleChoiceFilter(
        label=pgettext_lazy("Sale list filter label", "Categories"),
        field_name="categories",
        queryset=Category.objects.all(),
    )
    type = ChoiceFilter(
        label=pgettext_lazy("Sale list filter label", "Discount type"),
        choices=DISCOUNT_TYPE_CHOICES,
        empty_label=pgettext_lazy("Filter empty choice label", "All"),
        widget=forms.Select,
    )
    value = RangeFilter(label=pgettext_lazy("Sale list filter label", "Value"))
    date = DateFromToRangeFilter(
        label=pgettext_lazy("Sale list sorting filter label", "Period of validity"),
        field_name="created",
        widget=DateRangeWidget,
        method=filter_by_date_range,
    )
    sort_by = OrderingFilter(
        label=pgettext_lazy("Sale list filter label", "Sort by"),
        fields=SORT_BY_FIELDS_SALE.keys(),
        field_labels=SORT_BY_FIELDS_SALE,
    )

    class Meta:
        model = Sale
        fields = []

    def get_summary_message(self):
        counter = self.qs.count()
        return npgettext(
            "Number of matching records in the dashboard sales list",
            "Found %(counter)d matching sale",
            "Found %(counter)d matching sales",
            number=counter,
        ) % {"counter": counter}


class VoucherFilter(SortedFilterSet):
    name = CharFilter(
        label=pgettext_lazy("Voucher list name filter label", "Name"),
        lookup_expr="icontains",
    )
    type = ChoiceFilter(
        field_name="discount_value_type",
        label=pgettext_lazy("Sale list is sale type filter label", "Discount type"),
        choices=DISCOUNT_TYPE_CHOICES,
        empty_label=pgettext_lazy("Filter empty choice label", "All"),
        widget=forms.Select,
    )
    discount_value = RangeFilter(
        label=pgettext_lazy("Sale list filter label", "Discount_value")
    )
    date = DateFromToRangeFilter(
        label=pgettext_lazy("Voucher list sorting filter label", "Period of validity"),
        field_name="created",
        widget=DateRangeWidget,
        method=filter_by_date_range,
    )
    min_spent_amount = RangeFilter(
        label=pgettext_lazy("Voucher list sorting filter", "Minimum amount spent"),
        field_name="min_spent_amount",
    )
    sort_by = OrderingFilter(
        label=pgettext_lazy("Voucher list sorting filter label", "Sort by"),
        fields=SORT_BY_FIELDS_LABELS_VOUCHER.keys(),
        field_labels=SORT_BY_FIELDS_LABELS_VOUCHER,
    )

    class Meta:
        model = Voucher
        fields = []<commit_msg>Update filters.py<commit_after>from django import forms
from django.db.models import Q
from django.utils.translation import npgettext, pgettext_lazy
from django_filters import (
    CharFilter,
    ChoiceFilter,
    DateFromToRangeFilter,
    ModelMultipleChoiceFilter,
    OrderingFilter,
    RangeFilter,
)

from ...core.filters import SortedFilterSet
from ...discount.models import Sale, Voucher
from ...product.models import Category
from ..widgets import DateRangeWidget

SORT_BY_FIELDS_SALE = {
    "name": pgettext_lazy("Sale list sorting option", "name"),
    "value": pgettext_lazy("Sale list sorting option", "value"),
    "start_date": pgettext_lazy("Sale list sorting option", "start_date"),
    "end_date": pgettext_lazy("Sale list sorting option", "end_date"),
}

SORT_BY_FIELDS_LABELS_VOUCHER = {
    "name": pgettext_lazy("Voucher list sorting option", "name"),
    "discount_value": pgettext_lazy("Voucher list sorting option", "discount_value"),
    "countries": pgettext_lazy("Voucher list sorting option", "countries"),
    "start_date": pgettext_lazy("Voucher list sorting option", "start_date"),
    "end_date": pgettext_lazy("Voucher list sorting option", "end_date"),
    "used": pgettext_lazy("Voucher list sorting option", "used"),
    "min_spent_amount": pgettext_lazy(
        "Voucher list sorting option", "min_spent_amount"
    ),
}

DISCOUNT_VALUE_TYPE_CHOICES = (
    ("fixed", pgettext_lazy("Sale type filter choice", "USD")),
    ("percentage", pgettext_lazy("Sale type filter choice", "%")),
)

VOUCHER_TYPE_CHOICES = (
    ("shipping", pgettext_lazy("Voucher type filter choice", "Shipping")),
    ("entire_order", pgettext_lazy("Voucher type filter choice", "Entire Order")),
    ("specific_product", pgettext_lazy("Voucher type filter choice",
                                       "Specific Product")),
)


def filter_by_date_range(queryset, name, value):
    q = Q()
    if value.start:
        q = Q(start_date__gte=value.start)
    if value.stop:
        if value.start:
            q |= Q(end_date__lte=value.stop)
        else:
            q = Q(end_date__lte=value.stop)
    return queryset.filter(q)


class SaleFilter(SortedFilterSet):
    name = CharFilter(
        label=pgettext_lazy("Sale list filter label", "Name"), lookup_expr="icontains"
    )
    categories = ModelMultipleChoiceFilter(
        label=pgettext_lazy("Sale list filter label", "Categories"),
        field_name="categories",
        queryset=Category.objects.all(),
    )
    type = ChoiceFilter(
        label=pgettext_lazy("Sale list filter label", "Discount type"),
        choices=DISCOUNT_VALUE_TYPE_CHOICES,
        empty_label=pgettext_lazy("Filter empty choice label", "All"),
        widget=forms.Select,
    )
    value = RangeFilter(label=pgettext_lazy("Sale list filter label", "Value"))
    date = DateFromToRangeFilter(
        label=pgettext_lazy("Sale list sorting filter label", "Period of validity"),
        field_name="created",
        widget=DateRangeWidget,
        method=filter_by_date_range,
    )
    sort_by = OrderingFilter(
        label=pgettext_lazy("Sale list filter label", "Sort by"),
        fields=SORT_BY_FIELDS_SALE.keys(),
        field_labels=SORT_BY_FIELDS_SALE,
    )

    class Meta:
        model = Sale
        fields = []

    def get_summary_message(self):
        counter = self.qs.count()
        return npgettext(
            "Number of matching records in the dashboard sales list",
            "Found %(counter)d matching sale",
            "Found %(counter)d matching sales",
            number=counter,
        ) % {"counter": counter}


class VoucherFilter(SortedFilterSet):
    name = CharFilter(
        label=pgettext_lazy("Voucher list name filter label", "Name"),
        lookup_expr="icontains",
    )
    type = ChoiceFilter(
        field_name="discount_value_type",
        label=pgettext_lazy("Sale list is sale type filter label", "Discount type"),
        choices=VOUCHER_TYPE_CHOICES,
        empty_label=pgettext_lazy("Filter empty choice label", "All"),
        widget=forms.Select,
    )
    discount_value = RangeFilter(
        label=pgettext_lazy("Sale list filter label", "Discount_value")
    )
    date = DateFromToRangeFilter(
        label=pgettext_lazy("Voucher list sorting filter label", "Period of validity"),
        field_name="created",
        widget=DateRangeWidget,
        method=filter_by_date_range,
    )
    min_spent_amount = RangeFilter(
        label=pgettext_lazy("Voucher list sorting filter", "Minimum amount spent"),
        field_name="min_spent_amount",
    )
    sort_by = OrderingFilter(
        label=pgettext_lazy("Voucher list sorting filter label", "Sort by"),
        fields=SORT_BY_FIELDS_LABELS_VOUCHER.keys(),
        field_labels=SORT_BY_FIELDS_LABELS_VOUCHER,
    )

    class Meta:
        model = Voucher
        fields = []
{'fraction_non_alphanumeric': 0.11014153453229754, 'fraction_numerical': 0.0, 'mean_word_length': 3.430928807166431, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '112386', 'n_tokens_mistral': 3028, 'n_tokens_neox': 2978, 'n_words': 644}