]> git.0d.be Git - panikdb.git/blobdiff - panikdb/views.py
regie: add button to grab tracks from nonstop
[panikdb.git] / panikdb / views.py
index 0981582b4bc0c8911bd9f7d59eae8950dbb156c2..396081cc287e11aa2c064551b7119040a0a4eebf 100644 (file)
@@ -4,6 +4,7 @@ from django.contrib.auth.decorators import login_required
 from django.contrib.auth.forms import AuthenticationForm
 from django.core.exceptions import PermissionDenied
 from django.core.urlresolvers import reverse
+from django.http import JsonResponse
 from django.template import loader
 from django.views.generic.base import TemplateView, RedirectView
 from django.views.generic.list import ListView
@@ -56,6 +57,38 @@ class RegieHome(TemplateView):
 regie_home = RegieHome.as_view()
 
 
+def regie_tracks(request):
+    from emissions.models import Nonstop
+    from nonstop.models import Track
+
+    now = datetime.datetime.now()
+    current_zone = None
+    for nonstop in Nonstop.objects.all():
+        if (nonstop.start < nonstop.end and (
+                now.time() >= nonstop.start and now.time() < nonstop.end)) or \
+           (nonstop.start > nonstop.end and (
+                now.time() >= nonstop.start or now.time() < nonstop.end)):
+            current_zone = nonstop
+            break
+
+    tracks = Track.objects.filter(duration__isnull=False)
+    tracks = tracks.filter(duration__gte=datetime.timedelta(minutes=2, seconds=30))
+    tracks = tracks.filter(duration__lt=datetime.timedelta(minutes=4, seconds=30))
+    if current_zone:
+        tracks = tracks.filter(nonstop_zones__in=[current_zone.id])
+    tracks = tracks.exclude(language__isnull=True).exclude(language='')
+    tracks = tracks.order_by('?')
+
+    return JsonResponse({'data': [
+        {
+            'id': track.id,
+            'title': track.title,
+            'artist': track.artist.name if track.artist_id else None,
+            'instru': track.instru,
+            'language': track.language,
+            } for track in tracks[:5]]})
+
+
 class EmissionListView(emissions.views.EmissionListView):
     pass