<commit_before># -*- coding: utf-8 -*-
from django import forms
from django.forms.util import ValidationError
from django.utils.translation import ugettext as _

from selectfilter.forms import FilteredSelectMultiple, SelectBoxFilter, HyperLinksFilter
from selectfilter import utils

class AjaxManyToManyField(forms.ModelMultipleChoiceField):
	"""
	Base many to many form field that display filter choices using
	JQuery ajax requests.
	"""
	def __init__(self, model, lookups, default_index=0, select_related=None,
		widget=FilteredSelectMultiple, filter_widget=SelectBoxFilter, *args, **kwargs):
		"""
		model: the related model
		lookups: a sequence of (label, lookup_dict) that tells how to
			filter the objects
			e.g. (
					('active', {'is_active': True}),
					('inactive', {'is_active': False}),
					)
			you may specify what you want in lookup_dict, give multiple
			filter lookups for the same choice and also set a choice that
			gets all unfiltered objects
			e.g. (
					('some stuff', {
						'field1__startswith': 'a',
						'field2': 'value'
						}),
					('all stuff', {}),
					)

		default_index: the index of the lookup sequence that will
			be the default choice when the field is initially displayed.
			set to None if you want the widget to start empty
			
		select_related: if not None the resulting querydict is performed
			using select_related(select_related), allowing foreign keys
			to be retrieved (e.g. useful when the unicode representation 
			of the model objects contains references to foreign keys)
			
		It is possible to pass all the other args and kwargs accepted by 
		the django field class.
		"""
		# get the default index and queryset
		# queryset is empty if default index is None
		if default_index is None:
			queryset = model.objects.none()
		else:
			lookups_list = utils.getLookups(lookups)
			lookup_dict = lookups_list[default_index][1]
			# get the queryset
			queryset = utils.getObjects(model, lookup_dict, select_related)
		# call the parent constructor
		super(AjaxManyToManyField, self
			).__init__(queryset, widget=widget, *args, **kwargs)
		# populate widget with some data
		self.widget.lookups = self.lookups = lookups
		self.widget.model = self.model = model
		self.widget.select_related = select_related
		self.widget.filter_widget = filter_widget()
		self.widget.default_index = default_index
		
	def clean(self, value):
		if self.required and not value:
			raise ValidationError(self.error_messages['required'])
		elif not self.required and not value:
			return []
		if not isinstance(value, (list, tuple)):
			raise ValidationError(self.error_messages['list'])
		final_values = []
		# if there is only one lookup used to limit choices, then a real
		# validation over that limited choices is performed
		lookups_list = utils.getLookups(self.lookups)
		limit_choices_to = {} if len(lookups_list) != 1 else lookups_list[0][1]
		for val in value:<commit_msg>Fixed bug in setting default_index - index out of range<commit_after># -*- coding: utf-8 -*-
from django import forms
from django.forms.util import ValidationError
from django.utils.translation import ugettext as _

from selectfilter.forms import FilteredSelectMultiple, SelectBoxFilter, HyperLinksFilter
from selectfilter import utils

class AjaxManyToManyField(forms.ModelMultipleChoiceField):
	"""
	Base many to many form field that display filter choices using
	JQuery ajax requests.
	"""
	def __init__(self, model, lookups, default_index=0, select_related=None,
		widget=FilteredSelectMultiple, filter_widget=SelectBoxFilter, *args, **kwargs):
		"""
		model: the related model
		lookups: a sequence of (label, lookup_dict) that tells how to
			filter the objects
			e.g. (
					('active', {'is_active': True}),
					('inactive', {'is_active': False}),
					)
			you may specify what you want in lookup_dict, give multiple
			filter lookups for the same choice and also set a choice that
			gets all unfiltered objects
			e.g. (
					('some stuff', {
						'field1__startswith': 'a',
						'field2': 'value'
						}),
					('all stuff', {}),
					)

		default_index: the index of the lookup sequence that will
			be the default choice when the field is initially displayed.
			set to None if you want the widget to start empty
			
		select_related: if not None the resulting querydict is performed
			using select_related(select_related), allowing foreign keys
			to be retrieved (e.g. useful when the unicode representation 
			of the model objects contains references to foreign keys)
			
		It is possible to pass all the other args and kwargs accepted by 
		the django field class.
		"""
		# get the default index and queryset
		# queryset is empty if default index is None
		if default_index is None:
			queryset = model.objects.none()
		else:
			lookups_list = utils.getLookups(lookups)
			if default_index >= len(lookups_list):
				default_index = 0
			lookup_dict = lookups_list[default_index][1]
			# get the queryset
			queryset = utils.getObjects(model, lookup_dict, select_related)
		# call the parent constructor
		super(AjaxManyToManyField, self
			).__init__(queryset, widget=widget, *args, **kwargs)
		# populate widget with some data
		self.widget.lookups = self.lookups = lookups
		self.widget.model = self.model = model
		self.widget.select_related = select_related
		self.widget.filter_widget = filter_widget()
		self.widget.default_index = default_index
		
	def clean(self, value):
		if self.required and not value:
			raise ValidationError(self.error_messages['required'])
		elif not self.required and not value:
			return []
		if not isinstance(value, (list, tuple)):
			raise ValidationError(self.error_messages['list'])
		final_values = []
		# if there is only one lookup used to limit choices, then a real
		# validation over that limited choices is performed
		lookups_list = utils.getLookups(self.lookups)
		limit_choices_to = {} if len(lookups_list) != 1 else lookups_list[0][1]
		for val in value:
{'fraction_non_alphanumeric': 0.08280147799798455, 'fraction_numerical': 0.0028552233792408467, 'mean_word_length': 4.0508905852417305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5910650', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1694, 'n_words': 624}