]> git.0d.be Git - django-panik-combo.git/blob - panikombo/forms.py
make it possible to filter newsitems by category
[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         NewsItemAutoSelectionCell, Topik)
9 from .views import soundfiles, episodes
10
11 class SoundFileWidget(HeavySelect2Widget):
12     def render_texts(self, selected_choices, choices):
13         queryset = SoundFile.objects.filter(id__in=selected_choices)
14         def fmt(soundfile):
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)
20
21
22 class SoundCellForm(forms.ModelForm):
23     class Meta:
24         model = SoundCell
25         fields = ('soundfile',)
26
27     def __init__(self, *args, **kwargs):
28         super(SoundCellForm, self).__init__(*args, **kwargs)
29         self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)
30
31
32 class EpisodeWidget(HeavySelect2Widget):
33     def render_texts(self, selected_choices, choices):
34         queryset = Episode.objects.filter(id__in=selected_choices)
35         def fmt(episode):
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)
39
40
41 class EpisodeCellForm(forms.ModelForm):
42     class Meta:
43         model = EpisodeCell
44         fields = ('episode', )
45
46     def __init__(self, *args, **kwargs):
47         super(EpisodeCellForm, self).__init__(*args, **kwargs)
48         self.fields['episode'].widget = EpisodeWidget(data_view=episodes)
49
50
51 class EpisodeAutoSelectionCellForm(forms.ModelForm):
52     class Meta:
53         model = EpisodeAutoSelectionCell
54         fields = ('title', 'tags', 'category')
55         widgets = {'tags': TagWidget()}
56
57
58 class NewsItemAutoSelectionCellForm(forms.ModelForm):
59     class Meta:
60         model = NewsItemAutoSelectionCell
61         fields = ('title', 'tags', 'category', 'future')
62         widgets = {'tags': TagWidget()}
63
64
65 class TopikEditForm(forms.ModelForm):
66     class Meta:
67         model = Topik
68         fields = ('image',)