create additional label from title
[django-panik-combo.git] / panikombo / views.py
1 from django.db.models import Q
2 import django_select2
3
4 from emissions.models import SoundFile, Episode
5
6 class SoundFilesView(django_select2.views.Select2View):
7     def get_results(self, request, terms, page, context):
8         queryset = SoundFile.objects.all()
9         for term in terms.split():
10             queryset = queryset.filter(podcastable=True).filter(Q(title__icontains=term) |
11                     Q(episode__title__icontains=term) |
12                     Q(episode__emission__title__icontains=term))
13         def fmt(soundfile):
14             return '%s - %s - %s' % (soundfile.episode.emission.title,
15                     soundfile.episode.title,
16                     soundfile.title or soundfile.id)
17         return ('nil', False, ((x.id, fmt(x)) for x in queryset.select_related()))
18
19 soundfiles = SoundFilesView.as_view()
20
21
22 class EpisodesView(django_select2.views.Select2View):
23     def get_results(self, request, terms, page, context):
24         queryset = Episode.objects.all()
25         for term in terms.split():
26             queryset = queryset.filter(Q(title__icontains=term) |
27                     Q(emission__title__icontains=term))
28         def fmt(episode):
29             return '%s - %s' % (episode.emission.title, episode.title)
30         return ('nil', False, ((x.id, fmt(x)) for x in queryset.select_related()))
31
32 episodes = EpisodesView.as_view()