3 from django import forms
4 from django.forms.widgets import Widget
5 from taggit.forms import TagWidget
7 from emissions.models import SoundFile, Episode
9 from .models import (SoundCell, EpisodeCell, EpisodeAutoSelectionCell,
10 NewsItemAutoSelectionCell)
13 class BaseSelect2Widget(Widget):
14 template_name = "panikombo/select2.html"
16 def get_context(self, name, value, attrs):
17 context = super().get_context(name, value, attrs)
18 context['view_name'] = self.view_name
20 context['widget']['view_value'] = self.view_value(value)
24 class SoundFileWidget(BaseSelect2Widget):
25 view_name = 'panikombo-select2-soundfiles'
27 def view_value(self, value):
29 soundfile = SoundFile.objects.get(id=value)
30 except SoundFile.DoesNotExist:
31 return 'missing sound %s' % value
32 return '%s - %s - %s' % (soundfile.episode.emission.title,
33 soundfile.episode.title,
34 soundfile.title or soundfile.id)
37 class SoundCellForm(forms.ModelForm):
40 fields = ('soundfile',)
42 def __init__(self, *args, **kwargs):
43 super(SoundCellForm, self).__init__(*args, **kwargs)
44 self.fields['soundfile'].widget = SoundFileWidget()
47 class EpisodeWidget(BaseSelect2Widget):
48 view_name = 'panikombo-select2-episodes'
50 def view_value(self, value):
52 episode = Episode.objects.get(id=value)
53 except Episode.DoesNotExist:
54 return 'missing episode %s' % value
55 return '%s - %s' % (episode.emission.title, episode.title)
58 class EpisodeCellForm(forms.ModelForm):
61 fields = ('episode', )
63 def __init__(self, *args, **kwargs):
64 super(EpisodeCellForm, self).__init__(*args, **kwargs)
65 self.fields['episode'].widget = EpisodeWidget()
68 class EpisodeAutoSelectionCellForm(forms.ModelForm):
70 model = EpisodeAutoSelectionCell
71 fields = ('title', 'tags', 'and_tags', 'category', 'period')
72 widgets = {'tags': TagWidget()}
75 class NewsItemAutoSelectionCellForm(forms.ModelForm):
77 model = NewsItemAutoSelectionCell
78 fields = ('title', 'tags', 'and_tags', 'category', 'future')
79 widgets = {'tags': TagWidget()}