update for newer django-select2
authorFrédéric Péters <fpeters@0d.be>
Thu, 26 Sep 2019 12:27:17 +0000 (14:27 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 26 Sep 2019 12:29:15 +0000 (14:29 +0200)
panikombo/forms.py
panikombo/views.py

index 8cb67c5..a366e75 100644 (file)
@@ -1,7 +1,7 @@
 import json
 
 from django import forms
-from django_select2.widgets import HeavySelect2Widget
+from django_select2.forms import HeavySelect2Widget
 from taggit.forms import TagWidget
 
 from emissions.models import SoundFile, Episode
index 744f34e..4541dab 100644 (file)
@@ -1,13 +1,15 @@
 from django.core.urlresolvers import reverse
 from django.db.models import Q
+from django.http import Http404, JsonResponse
 from django.views.generic.edit import UpdateView
 import django_select2.views
 
 from emissions.models import SoundFile, Episode
 from .models import Topik
 
-class SoundFilesView(django_select2.views.Select2View):
-    def get_results(self, request, terms, page, context):
+class SoundFilesView(django_select2.views.AutoResponseView):
+    def get(self, request, *args, **kwargs):
+        terms = kwargs.get('term', request.GET.get('term', ''))
         queryset = SoundFile.objects.all()
         for term in terms.split():
             queryset = queryset.filter(podcastable=True).filter(Q(title__icontains=term) |
@@ -17,20 +19,43 @@ class SoundFilesView(django_select2.views.Select2View):
             return '%s - %s - %s' % (soundfile.episode.emission.title,
                     soundfile.episode.title,
                     soundfile.title or soundfile.id)
-        return ('nil', False, ((x.id, fmt(x)) for x in queryset.select_related()))
+
+        return JsonResponse({
+            'results': [
+                {
+                    'text': fmt(x),
+                    'id': x.pk,
+                }
+                for x in queryset
+                ],
+            'more': False
+        })
+
 
 soundfiles = SoundFilesView.as_view()
 
 
-class EpisodesView(django_select2.views.Select2View):
-    def get_results(self, request, terms, page, context):
+class EpisodesView(django_select2.views.AutoResponseView):
+    def get(self, request, *args, **kwargs):
+        terms = kwargs.get('term', request.GET.get('term', ''))
         queryset = Episode.objects.all()
         for term in terms.split():
             queryset = queryset.filter(Q(title__icontains=term) |
                     Q(emission__title__icontains=term))
         def fmt(episode):
             return '%s - %s' % (episode.emission.title, episode.title)
-        return ('nil', False, ((x.id, fmt(x)) for x in queryset.select_related()))
+
+        return JsonResponse({
+            'results': [
+                {
+                    'text': fmt(x),
+                    'id': x.pk,
+                }
+                for x in queryset
+                ],
+            'more': False
+        })
+
 
 episodes = EpisodesView.as_view()