<commit_before>import django_filters
from dal import autocomplete
from django import forms
from django.utils.translation import ugettext_lazy as _

from pola.company.models import Company
from pola.filters import CrispyFilterMixin

from .models import Product


class NullProductFilter(django_filters.Filter):
    field_class = forms.BooleanField

    def filter(self, qs, value):
        if value:
            return qs.filter(company=None)
        return qs


class ProductFilter(CrispyFilterMixin, django_filters.FilterSet):
    company_empty = NullProductFilter(label="Tylko produkty bez producenta")

    company = django_filters.ModelChoiceFilter(
        queryset=Company.objects.all(), widget=autocomplete.ModelSelect2(url='company:company-autocomplete')
    )

    o = django_filters.OrderingFilter(
        # tuple-mapping retains order
        fields=(
            ('name', _('Nazwa (A-Z)')),
            ('company__name', _('Nazwa producenta (A-Z)')),
            ('query_count', _('Liczba zeskanowań (rosnąco)')),
            ('modified', _('Data modyfikacji (rosnąco)')),
            ('-modified', _('Data modyfikacji (malejaco)')),
            ('created', _('Data utworzenia (rosnąco)')),
            ('-created', _('Data utworzenia (malejaco)')),
        )
    )

    class Meta:
        model = Product
        fields = {
            'name': ['icontains'],
            'code': ['icontains'],
        }<commit_msg>Add filter by code prefix (#1660)<commit_after>import django_filters
from dal import autocomplete
from django import forms
from django.utils.translation import ugettext_lazy as _

from pola.company.models import Company
from pola.filters import CrispyFilterMixin

from .models import Product


class NullProductFilter(django_filters.Filter):
    field_class = forms.BooleanField

    def filter(self, qs, value):
        if value:
            return qs.filter(company=None)
        return qs


class ProductFilter(CrispyFilterMixin, django_filters.FilterSet):
    company_empty = NullProductFilter(label="Tylko produkty bez producenta")

    company = django_filters.ModelChoiceFilter(
        queryset=Company.objects.all(), widget=autocomplete.ModelSelect2(url='company:company-autocomplete')
    )

    o = django_filters.OrderingFilter(
        # tuple-mapping retains order
        fields=(
            ('name', _('Nazwa (A-Z)')),
            ('company__name', _('Nazwa producenta (A-Z)')),
            ('query_count', _('Liczba zeskanowań (rosnąco)')),
            ('modified', _('Data modyfikacji (rosnąco)')),
            ('-modified', _('Data modyfikacji (malejaco)')),
            ('created', _('Data utworzenia (rosnąco)')),
            ('-created', _('Data utworzenia (malejaco)')),
        )
    )

    class Meta:
        model = Product
        fields = {
            'name': ['icontains'],
            'code': ['icontains', 'istartswith'],
        }
{'fraction_non_alphanumeric': 0.12158894645941278, 'fraction_numerical': 0.002072538860103627, 'mean_word_length': 2.9616963064295487, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3675533', 'n_tokens_mistral': 886, 'n_tokens_neox': 870, 'n_words': 196}