initial version
[django-panik-combo.git] / panikombo / forms.py
1 from django import forms
2 from django_select2.widgets import HeavySelect2Widget, convert_to_js_string_arr
3
4 from emissions.models import SoundFile
5
6 from .models import SoundCell
7 from .views import soundfiles
8
9 class SoundFileWidget(HeavySelect2Widget):
10     def render_texts(self, selected_choices, choices):
11         queryset = SoundFile.objects.filter(id__in=selected_choices)
12         def fmt(soundfile):
13             return '%s - %s - %s' % (soundfile.episode.emission.title,
14                     soundfile.episode.title,
15                     soundfile.title or soundfile.id)
16         texts = [fmt(soundfile) for soundfile in queryset.select_related()]
17         return convert_to_js_string_arr(texts)
18
19
20 class SoundCellForm(forms.ModelForm):
21     class Meta:
22         model = SoundCell
23         fields = ('soundfile',)
24
25     def __init__(self, *args, **kwargs):
26         super(SoundCellForm, self).__init__(*args, **kwargs)
27         self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)