1 from django import forms
2 from django_select2.widgets import HeavySelect2Widget, convert_to_js_string_arr
4 from emissions.models import SoundFile, Episode
6 from .models import SoundCell, EpisodeCell
7 from .views import soundfiles, episodes
9 class SoundFileWidget(HeavySelect2Widget):
10 def render_texts(self, selected_choices, choices):
11 queryset = SoundFile.objects.filter(id__in=selected_choices)
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)
20 class SoundCellForm(forms.ModelForm):
23 fields = ('soundfile',)
25 def __init__(self, *args, **kwargs):
26 super(SoundCellForm, self).__init__(*args, **kwargs)
27 self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)
30 class EpisodeWidget(HeavySelect2Widget):
31 def render_texts(self, selected_choices, choices):
32 queryset = Episode.objects.filter(id__in=selected_choices)
34 return '%s - %s' % (episode.emission.title, episode.title)
35 texts = [fmt(episode) for episode in queryset.select_related()]
36 return convert_to_js_string_arr(texts)
39 class EpisodeCellForm(forms.ModelForm):
42 fields = ('episode', )
44 def __init__(self, *args, **kwargs):
45 super(EpisodeCellForm, self).__init__(*args, **kwargs)
46 self.fields['episode'].widget = EpisodeWidget(data_view=episodes)