add minimalistic 'episode' cell type
[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, Episode
5
6 from .models import SoundCell, EpisodeCell
7 from .views import soundfiles, episodes
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)
28
29
30 class EpisodeWidget(HeavySelect2Widget):
31     def render_texts(self, selected_choices, choices):
32         queryset = Episode.objects.filter(id__in=selected_choices)
33         def fmt(episode):
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)
37
38
39 class EpisodeCellForm(forms.ModelForm):
40     class Meta:
41         model = EpisodeCell
42         fields = ('episode', )
43
44     def __init__(self, *args, **kwargs):
45         super(EpisodeCellForm, self).__init__(*args, **kwargs)
46         self.fields['episode'].widget = EpisodeWidget(data_view=episodes)