--- /dev/null
+from django import forms
+from django_select2.widgets import HeavySelect2Widget, convert_to_js_string_arr
+
+from emissions.models import SoundFile
+
+from .models import SoundCell
+from .views import soundfiles
+
+class SoundFileWidget(HeavySelect2Widget):
+ def render_texts(self, selected_choices, choices):
+ queryset = SoundFile.objects.filter(id__in=selected_choices)
+ def fmt(soundfile):
+ return '%s - %s - %s' % (soundfile.episode.emission.title,
+ soundfile.episode.title,
+ soundfile.title or soundfile.id)
+ texts = [fmt(soundfile) for soundfile in queryset.select_related()]
+ return convert_to_js_string_arr(texts)
+
+
+class SoundCellForm(forms.ModelForm):
+ class Meta:
+ model = SoundCell
+ fields = ('soundfile',)
+
+ def __init__(self, *args, **kwargs):
+ super(SoundCellForm, self).__init__(*args, **kwargs)
+ self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)