add additional "and_tags" field to auto selection, for ANDing criterias
[django-panik-combo.git] / panikombo / forms.py
1 import json
2
3 from django import forms
4 from django_select2.widgets import HeavySelect2Widget
5 from taggit.forms import TagWidget
6
7 from emissions.models import SoundFile, Episode
8
9 from .models import (SoundCell, EpisodeCell, EpisodeAutoSelectionCell,
10         NewsItemAutoSelectionCell, Topik)
11 from .views import soundfiles, episodes
12
13 class SoundFileWidget(HeavySelect2Widget):
14     def render_texts(self, selected_choices, choices):
15         queryset = SoundFile.objects.filter(id__in=selected_choices)
16         def fmt(soundfile):
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)
22
23
24 class SoundCellForm(forms.ModelForm):
25     class Meta:
26         model = SoundCell
27         fields = ('soundfile',)
28
29     def __init__(self, *args, **kwargs):
30         super(SoundCellForm, self).__init__(*args, **kwargs)
31         self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)
32
33
34 class EpisodeWidget(HeavySelect2Widget):
35     def render_texts(self, selected_choices, choices):
36         queryset = Episode.objects.filter(id__in=selected_choices)
37         def fmt(episode):
38             return '%s - %s' % (episode.emission.title, episode.title)
39         texts = [fmt(episode) for episode in queryset.select_related()]
40         return json.dumps(texts)
41
42
43 class EpisodeCellForm(forms.ModelForm):
44     class Meta:
45         model = EpisodeCell
46         fields = ('episode', )
47
48     def __init__(self, *args, **kwargs):
49         super(EpisodeCellForm, self).__init__(*args, **kwargs)
50         self.fields['episode'].widget = EpisodeWidget(data_view=episodes)
51
52
53 class EpisodeAutoSelectionCellForm(forms.ModelForm):
54     class Meta:
55         model = EpisodeAutoSelectionCell
56         fields = ('title', 'tags', 'and_tags', 'category', 'period')
57         widgets = {'tags': TagWidget()}
58
59
60 class NewsItemAutoSelectionCellForm(forms.ModelForm):
61     class Meta:
62         model = NewsItemAutoSelectionCell
63         fields = ('title', 'tags', 'and_tags', 'category', 'future')
64         widgets = {'tags': TagWidget()}
65
66
67 class TopikEditForm(forms.ModelForm):
68     class Meta:
69         model = Topik
70         fields = ('image',)