add Topik object, wrapper around Page
[django-panik-combo.git] / panikombo / views.py
1 from django.core.urlresolvers import reverse
2 from django.db.models import Q
3 from django.views.generic.edit import UpdateView
4 import django_select2
5
6 from emissions.models import SoundFile, Episode
7 from .models import Topik
8
9 class SoundFilesView(django_select2.views.Select2View):
10     def get_results(self, request, terms, page, context):
11         queryset = SoundFile.objects.all()
12         for term in terms.split():
13             queryset = queryset.filter(podcastable=True).filter(Q(title__icontains=term) |
14                     Q(episode__title__icontains=term) |
15                     Q(episode__emission__title__icontains=term))
16         def fmt(soundfile):
17             return '%s - %s - %s' % (soundfile.episode.emission.title,
18                     soundfile.episode.title,
19                     soundfile.title or soundfile.id)
20         return ('nil', False, ((x.id, fmt(x)) for x in queryset.select_related()))
21
22 soundfiles = SoundFilesView.as_view()
23
24
25 class EpisodesView(django_select2.views.Select2View):
26     def get_results(self, request, terms, page, context):
27         queryset = Episode.objects.all()
28         for term in terms.split():
29             queryset = queryset.filter(Q(title__icontains=term) |
30                     Q(emission__title__icontains=term))
31         def fmt(episode):
32             return '%s - %s' % (episode.emission.title, episode.title)
33         return ('nil', False, ((x.id, fmt(x)) for x in queryset.select_related()))
34
35 episodes = EpisodesView.as_view()
36
37
38 class TopikEdit(UpdateView):
39     model = Topik
40
41     def get_form_class(self):
42         from .forms import TopikEditForm
43         return TopikEditForm
44
45     def get_success_url(self):
46         return reverse('combo-manager-page-view', kwargs={'pk': self.object.page.id})
47
48 topik_edit = TopikEdit.as_view()