<commit_before>import six

from django import forms
from django.db import models
from django import forms
from django.contrib.contenttypes.generic import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from .registry import registry as default_registry
from .widgets import ChoiceWidget, MultipleChoiceWidget, TextWidget


class AutocompleteFieldMixin(object):
    def __init__(self, autocomplete=None, registry=None, widget=None,
            widget_js_attributes=None, autocomplete_js_attributes=None,
            extra_context=None, *args, **kwargs):
        registry = registry or default_registry
        self.autocomplete = registry.get_autocomplete_from_arg(autocomplete)

        widget = widget or self.widget
        if isinstance(widget, type):
            self.widget = widget(autocomplete, widget_js_attributes,
                    autocomplete_js_attributes, extra_context)

        parents = super(AutocompleteFieldMixin, self).__self_class__.__bases__
        if (forms.ModelChoiceField in parents or
                forms.ModelMultipleChoiceField in parents):
            kwargs['queryset'] = self.autocomplete.choices

        super(AutocompleteFieldMixin, self).__init__(*args, **kwargs)

    def validate(self, value):
        """
        Wrap around Autocomplete.validate_values().
        """
        super(AutocompleteFieldMixin, self).validate(value)

        # FIXME: we might actually want to change the Autocomplete API to
        # support python values instead of raw values, that would probably be
        # more performant.
        values = self.prepare_value(value)

        if value and not self.autocomplete(values=values).validate_values():
            raise forms.ValidationError('%s cannot validate %s' % (
                self.autocomplete_name, value))


class ModelChoiceField(AutocompleteFieldMixin, forms.ModelChoiceField):
    widget = ChoiceWidget
<commit_msg>Fixed dead variable name in AutocompleteFieldMixin<commit_after>import six

from django import forms
from django.db import models
from django import forms
from django.contrib.contenttypes.generic import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from .registry import registry as default_registry
from .widgets import ChoiceWidget, MultipleChoiceWidget, TextWidget


class AutocompleteFieldMixin(object):
    def __init__(self, autocomplete=None, registry=None, widget=None,
            widget_js_attributes=None, autocomplete_js_attributes=None,
            extra_context=None, *args, **kwargs):
        registry = registry or default_registry
        self.autocomplete = registry.get_autocomplete_from_arg(autocomplete)

        widget = widget or self.widget
        if isinstance(widget, type):
            self.widget = widget(autocomplete, widget_js_attributes,
                    autocomplete_js_attributes, extra_context)

        parents = super(AutocompleteFieldMixin, self).__self_class__.__bases__
        if (forms.ModelChoiceField in parents or
                forms.ModelMultipleChoiceField in parents):
            kwargs['queryset'] = self.autocomplete.choices

        super(AutocompleteFieldMixin, self).__init__(*args, **kwargs)

    def validate(self, value):
        """
        Wrap around Autocomplete.validate_values().
        """
        super(AutocompleteFieldMixin, self).validate(value)

        # FIXME: we might actually want to change the Autocomplete API to
        # support python values instead of raw values, that would probably be
        # more performant.
        values = self.prepare_value(value)

        if value and not self.autocomplete(values=values).validate_values():
            raise forms.ValidationError('%s cannot validate %s' % (
                self.autocomplete.__name__, value))


class ModelChoiceField(AutocompleteFieldMixin, forms.ModelChoiceField):
    widget = ChoiceWidget

{'fraction_non_alphanumeric': 0.07192219093933964, 'fraction_numerical': 0.0, 'mean_word_length': 3.4058624577226606, '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': '6931789', 'n_tokens_mistral': 1013, 'n_tokens_neox': 951, 'n_words': 269}