Source code for dal_select2.widgets

"""Select2 widget implementation module."""

from dal.widgets import (
    QuerySetSelectMixin,
    Select,
    SelectMultiple,
    WidgetMixin
)

from django import forms
from django.utils import six


[docs]class Select2WidgetMixin(object): """Mixin for Select2 widgets."""
[docs] class Media: """Automatically include static files for the admin.""" css = { 'all': ( 'autocomplete_light/vendor/select2/dist/css/select2.css', 'autocomplete_light/select2.css', ) } js = ( 'autocomplete_light/jquery.init.js', 'autocomplete_light/autocomplete.init.js', 'autocomplete_light/vendor/select2/dist/js/select2.full.js', 'autocomplete_light/select2.js', )
autocomplete_function = 'select2'
[docs]class Select2(Select2WidgetMixin, Select): """Select2 widget for regular choices."""
[docs]class Select2Multiple(Select2WidgetMixin, SelectMultiple): """Select2Multiple widget for regular choices."""
[docs]class ListSelect2(WidgetMixin, Select2WidgetMixin, forms.Select): """Select widget for regular choices and Select2."""
[docs]class ModelSelect2(QuerySetSelectMixin, Select2WidgetMixin, forms.Select): """Select widget for QuerySet choices and Select2."""
[docs]class ModelSelect2Multiple(QuerySetSelectMixin, Select2WidgetMixin, forms.SelectMultiple): """SelectMultiple widget for QuerySet choices and Select2."""
[docs]class TagSelect2(WidgetMixin, Select2WidgetMixin, forms.SelectMultiple): """Select2 in tag mode."""
[docs] def build_attrs(self, *args, **kwargs): """Automatically set data-tags=1.""" attrs = super(TagSelect2, self).build_attrs(*args, **kwargs) attrs.setdefault('data-tags', 1) return attrs
[docs] def value_from_datadict(self, data, files, name): """Return a comma-separated list of options. This is needed because Select2 uses a multiple select even in tag mode, and the model field expects a comma-separated list of tags. """ values = super(TagSelect2, self).value_from_datadict(data, files, name) return six.text_type(',').join(values)
[docs] def option_value(self, value): """Return the HTML option value attribute for a value.""" return value
[docs] def format_value(self, value): """Return the list of HTML option values for a form field value.""" if not isinstance(value, (tuple, list)): value = [value] values = set() for v in value: if not v: continue if isinstance(v, six.string_types): for t in v.split(','): values.add(self.option_value(t)) else: for t in v: values.add(self.option_value(t)) return values
[docs] def options(self, name, value, attrs=None): """Return only select options.""" # When the data hasn't validated, we get the raw input if isinstance(value, six.text_type): value = value.split(',') for v in value: if not v: continue real_values = v.split(',') if hasattr(v, 'split') else v for rv in real_values: yield self.option_value(rv)
[docs] def optgroups(self, name, value, attrs=None): """Return a list of one optgroup and selected values.""" default = (None, [], 0) groups = [default] for i, v in enumerate(self.options(name, value, attrs)): default[1].append( self.create_option(v, v, v, True, i) ) return groups