<commit_before>from django_filters import FilterSet, MultipleChoiceFilter, RangeFilter
from django.forms import CheckboxSelectMultiple

from django_prices.models import PriceField

from .models import Product, AttributeChoiceValue


class ProductFilter(FilterSet):
    def __init__(self, *args, **kwargs):
        super(ProductFilter, self).__init__(*args, **kwargs)
        product_attributes = set()
        variant_attributes = set()
        for product in self.queryset:
            for attribute in product.product_class.variant_attributes.all():
                variant_attributes.add(attribute)
            for attribute in product.product_class.product_attributes.all():
                product_attributes.add(attribute)

        for attribute in product_attributes:
            self.filters[attribute.slug] = \
                MultipleChoiceFilter(
                    name='product_class__product_attributes__values__slug',
                    label=attribute.name,
                    widget=CheckboxSelectMultiple,
                    choices=get_attribute_choices(attribute))

        for attribute in variant_attributes:
            self.filters[attribute.slug] = \
                MultipleChoiceFilter(
                    name='product_class__variant_attributes__values__slug',
                    label=attribute.name,
                    widget=CheckboxSelectMultiple,
                    choices=get_attribute_choices(attribute))

    class Meta:
        model = Product
        fields = ['price']
        exclude = []
        filter_overrides = {
            PriceField: {
                'filter_class': RangeFilter
            }
        }


def get_attribute_choices(attribute):
    result = [(choice.slug, choice.name)
              for choice in AttributeChoiceValue.objects.filter(
            attribute__slug=attribute.slug)]
    return result<commit_msg>Fix filters<commit_after>from django_filters import FilterSet, MultipleChoiceFilter, RangeFilter
from django.forms import CheckboxSelectMultiple

from django_prices.models import PriceField

from .models import Product, AttributeChoiceValue


class ProductFilter(FilterSet):
    def __init__(self, *args, **kwargs):
        super(ProductFilter, self).__init__(*args, **kwargs)
        product_attributes = set()
        variant_attributes = set()
        for product in self.queryset:
            for attribute in product.product_class.variant_attributes.all():
                variant_attributes.add(attribute)
            for attribute in product.product_class.product_attributes.all():
                product_attributes.add(attribute)

        for attribute in product_attributes:
            self.filters[attribute.slug] = \
                MultipleChoiceFilter(
                    name='attributes__%s' % attribute.pk,
                    label=attribute.name,
                    widget=CheckboxSelectMultiple,
                    choices=get_attribute_choices(attribute))

        for attribute in variant_attributes:
            self.filters[attribute.slug] = \
                MultipleChoiceFilter(
                    name='variants__attributes__%s' % attribute.pk,
                    label=attribute.name,
                    widget=CheckboxSelectMultiple,
                    choices=get_attribute_choices(attribute))

    class Meta:
        model = Product
        fields = ['price']
        exclude = []
        filter_overrides = {
            PriceField: {
                'filter_class': RangeFilter
            }
        }


def get_attribute_choices(attribute):
    result = [(choice.pk, choice.name)
              for choice in AttributeChoiceValue.objects.filter(
            attribute__pk=attribute.pk)]
    return result
{'fraction_non_alphanumeric': 0.07349785407725322, 'fraction_numerical': 0.0, 'mean_word_length': 2.187179487179487, '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': '3711759', 'n_tokens_mistral': 895, 'n_tokens_neox': 865, 'n_words': 180}