]> git.0d.be Git - django-panik-combo.git/blob - panikombo/forms.py
d9f60ec328c9afbfd9ddaa4834a0abfac0de2512
[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 from taggit.forms import TagWidget
4
5 from emissions.models import SoundFile, Episode
6
7 from .models import SoundCell, EpisodeCell, EpisodeAutoSelectionCell
8 from .views import soundfiles, episodes
9
10 class SoundFileWidget(HeavySelect2Widget):
11     def render_texts(self, selected_choices, choices):
12         queryset = SoundFile.objects.filter(id__in=selected_choices)
13         def fmt(soundfile):
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)
19
20
21 class SoundCellForm(forms.ModelForm):
22     class Meta:
23         model = SoundCell
24         fields = ('soundfile',)
25
26     def __init__(self, *args, **kwargs):
27         super(SoundCellForm, self).__init__(*args, **kwargs)
28         self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)
29
30
31 class EpisodeWidget(HeavySelect2Widget):
32     def render_texts(self, selected_choices, choices):
33         queryset = Episode.objects.filter(id__in=selected_choices)
34         def fmt(episode):
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)
38
39
40 class EpisodeCellForm(forms.ModelForm):
41     class Meta:
42         model = EpisodeCell
43         fields = ('episode', )
44
45     def __init__(self, *args, **kwargs):
46         super(EpisodeCellForm, self).__init__(*args, **kwargs)
47         self.fields['episode'].widget = EpisodeWidget(data_view=episodes)
48
49
50 class EpisodeAutoSelectionCellForm(forms.ModelForm):
51     class Meta:
52         model = EpisodeAutoSelectionCell
53         fields = ('title', 'tags', 'category')
54         widgets = {'tags': TagWidget()}