3 from django import forms
4 from django_select2.forms import HeavySelect2Widget
5 from taggit.forms import TagWidget
7 from emissions.models import SoundFile, Episode
9 from .models import (SoundCell, EpisodeCell, EpisodeAutoSelectionCell,
10 NewsItemAutoSelectionCell)
11 from .views import soundfiles, episodes
13 class SoundFileWidget(HeavySelect2Widget):
14 def render_texts(self, selected_choices, choices):
15 queryset = SoundFile.objects.filter(id__in=selected_choices)
17 return '%s - %s - %s' % (soundfile.episode.emission.title,
18 soundfile.episode.title,
19 soundfile.title or soundfile.id)
20 texts = [fmt(soundfile) for soundfile in queryset.select_related()]
21 return json.dumps(texts)
24 class SoundCellForm(forms.ModelForm):
27 fields = ('soundfile',)
29 def __init__(self, *args, **kwargs):
30 super(SoundCellForm, self).__init__(*args, **kwargs)
31 self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)
34 class EpisodeWidget(HeavySelect2Widget):
35 def render_texts(self, selected_choices, choices):
36 queryset = Episode.objects.filter(id__in=selected_choices)
38 return '%s - %s' % (episode.emission.title, episode.title)
39 texts = [fmt(episode) for episode in queryset.select_related()]
40 return json.dumps(texts)
43 class EpisodeCellForm(forms.ModelForm):
46 fields = ('episode', )
48 def __init__(self, *args, **kwargs):
49 super(EpisodeCellForm, self).__init__(*args, **kwargs)
50 self.fields['episode'].widget = EpisodeWidget(data_view=episodes)
53 class EpisodeAutoSelectionCellForm(forms.ModelForm):
55 model = EpisodeAutoSelectionCell
56 fields = ('title', 'tags', 'and_tags', 'category', 'period')
57 widgets = {'tags': TagWidget()}
60 class NewsItemAutoSelectionCellForm(forms.ModelForm):
62 model = NewsItemAutoSelectionCell
63 fields = ('title', 'tags', 'and_tags', 'category', 'future')
64 widgets = {'tags': TagWidget()}