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 NewsItemAutoSelectionCell, Topik)
9 from .views import soundfiles, episodes
11 class SoundFileWidget(HeavySelect2Widget):
12 def render_texts(self, selected_choices, choices):
13 queryset = SoundFile.objects.filter(id__in=selected_choices)
15 return '%s - %s - %s' % (soundfile.episode.emission.title,
16 soundfile.episode.title,
17 soundfile.title or soundfile.id)
18 texts = [fmt(soundfile) for soundfile in queryset.select_related()]
19 return convert_to_js_string_arr(texts)
22 class SoundCellForm(forms.ModelForm):
25 fields = ('soundfile',)
27 def __init__(self, *args, **kwargs):
28 super(SoundCellForm, self).__init__(*args, **kwargs)
29 self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)
32 class EpisodeWidget(HeavySelect2Widget):
33 def render_texts(self, selected_choices, choices):
34 queryset = Episode.objects.filter(id__in=selected_choices)
36 return '%s - %s' % (episode.emission.title, episode.title)
37 texts = [fmt(episode) for episode in queryset.select_related()]
38 return convert_to_js_string_arr(texts)
41 class EpisodeCellForm(forms.ModelForm):
44 fields = ('episode', )
46 def __init__(self, *args, **kwargs):
47 super(EpisodeCellForm, self).__init__(*args, **kwargs)
48 self.fields['episode'].widget = EpisodeWidget(data_view=episodes)
51 class EpisodeAutoSelectionCellForm(forms.ModelForm):
53 model = EpisodeAutoSelectionCell
54 fields = ('title', 'tags', 'category')
55 widgets = {'tags': TagWidget()}
58 class NewsItemAutoSelectionCellForm(forms.ModelForm):
60 model = NewsItemAutoSelectionCell
61 fields = ('title', 'tags', 'future')
62 widgets = {'tags': TagWidget()}
65 class TopikEditForm(forms.ModelForm):