]> git.0d.be Git - django-panik-combo.git/blob - panikombo/forms.py
add introduction to fullwidth page placeholders
[django-panik-combo.git] / panikombo / forms.py
1 import json
2
3 from django import forms
4 from django.forms.widgets import Widget
5 from taggit.forms import TagWidget
6
7 from emissions.models import SoundFile, Episode
8
9 from .models import (SoundCell, EpisodeCell, EpisodeAutoSelectionCell,
10         NewsItemAutoSelectionCell)
11
12
13 class BaseSelect2Widget(Widget):
14     template_name = "panikombo/select2.html"
15
16     def get_context(self, name, value, attrs):
17         context = super().get_context(name, value, attrs)
18         context['view_name'] = self.view_name
19         if value:
20             context['widget']['view_value'] = self.view_value(value)
21         return context
22
23
24 class SoundFileWidget(BaseSelect2Widget):
25     view_name = 'panikombo-select2-soundfiles'
26
27     def view_value(self, value):
28         try:
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)
35
36
37 class SoundCellForm(forms.ModelForm):
38     class Meta:
39         model = SoundCell
40         fields = ('soundfile',)
41
42     def __init__(self, *args, **kwargs):
43         super(SoundCellForm, self).__init__(*args, **kwargs)
44         self.fields['soundfile'].widget = SoundFileWidget()
45
46
47 class EpisodeWidget(BaseSelect2Widget):
48     view_name = 'panikombo-select2-episodes'
49
50     def view_value(self, value):
51         try:
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)
56
57
58 class EpisodeCellForm(forms.ModelForm):
59     class Meta:
60         model = EpisodeCell
61         fields = ('episode', )
62
63     def __init__(self, *args, **kwargs):
64         super(EpisodeCellForm, self).__init__(*args, **kwargs)
65         self.fields['episode'].widget = EpisodeWidget()
66
67
68 class EpisodeAutoSelectionCellForm(forms.ModelForm):
69     class Meta:
70         model = EpisodeAutoSelectionCell
71         fields = ('title', 'tags', 'and_tags', 'category', 'period')
72         widgets = {'tags': TagWidget()}
73
74
75 class NewsItemAutoSelectionCellForm(forms.ModelForm):
76     class Meta:
77         model = NewsItemAutoSelectionCell
78         fields = ('title', 'tags', 'and_tags', 'category', 'future')
79         widgets = {'tags': TagWidget()}