1 from django import forms
2 from django_select2.widgets import HeavySelect2Widget, convert_to_js_string_arr
3 from taggit.forms import TagWidget
5 from emissions.models import SoundFile, Episode
7 from .models import SoundCell, EpisodeCell, EpisodeAutoSelectionCell
8 from .views import soundfiles, episodes
10 class SoundFileWidget(HeavySelect2Widget):
11 def render_texts(self, selected_choices, choices):
12 queryset = SoundFile.objects.filter(id__in=selected_choices)
14 return '%s - %s - %s' % (soundfile.episode.emission.title,
15 soundfile.episode.title,
16 soundfile.title or soundfile.id)
17 texts = [fmt(soundfile) for soundfile in queryset.select_related()]
18 return convert_to_js_string_arr(texts)
21 class SoundCellForm(forms.ModelForm):
24 fields = ('soundfile',)
26 def __init__(self, *args, **kwargs):
27 super(SoundCellForm, self).__init__(*args, **kwargs)
28 self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)
31 class EpisodeWidget(HeavySelect2Widget):
32 def render_texts(self, selected_choices, choices):
33 queryset = Episode.objects.filter(id__in=selected_choices)
35 return '%s - %s' % (episode.emission.title, episode.title)
36 texts = [fmt(episode) for episode in queryset.select_related()]
37 return convert_to_js_string_arr(texts)
40 class EpisodeCellForm(forms.ModelForm):
43 fields = ('episode', )
45 def __init__(self, *args, **kwargs):
46 super(EpisodeCellForm, self).__init__(*args, **kwargs)
47 self.fields['episode'].widget = EpisodeWidget(data_view=episodes)
50 class EpisodeAutoSelectionCellForm(forms.ModelForm):
52 model = EpisodeAutoSelectionCell
53 fields = ('title', 'tags', 'category')
54 widgets = {'tags': TagWidget()}