]> git.0d.be Git - django-panik-nonstop.git/commitdiff
add view to add streamed diffusion
authorFrédéric Péters <fpeters@0d.be>
Mon, 6 Jan 2020 13:52:45 +0000 (14:52 +0100)
committerFrédéric Péters <fpeters@0d.be>
Mon, 6 Jan 2020 13:53:21 +0000 (14:53 +0100)
nonstop/migrations/0016_auto_20200106_1427.py [new file with mode: 0644]
nonstop/models.py
nonstop/templates/nonstop/soma_palinsesti.xml
nonstop/templates/nonstop/streamed-diffusion.html [new file with mode: 0644]
nonstop/urls.py
nonstop/utils.py
nonstop/views.py

diff --git a/nonstop/migrations/0016_auto_20200106_1427.py b/nonstop/migrations/0016_auto_20200106_1427.py
new file mode 100644 (file)
index 0000000..34beb10
--- /dev/null
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.24 on 2020-01-06 14:27
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('nonstop', '0015_streameddiffusion'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='streameddiffusion',
+            name='jingle',
+            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='nonstop.Jingle'),
+        ),
+    ]
index c72dd7a86779723ee74c455b03dbe76f8ed3c6f2..d78dee00431651728eec1cbdbe49eac8c3de69f6 100644 (file)
@@ -169,6 +169,9 @@ class Stream(models.Model):
     label = models.CharField(_('Label'), max_length=100)
     url = models.URLField(_('URL'), max_length=255)
 
+    def __str__(self):
+        return self.label
+
 
 class StreamedDiffusion(models.Model):
     class Meta:
@@ -176,5 +179,8 @@ class StreamedDiffusion(models.Model):
         verbose_name_plural = _('Streamed diffusions')
 
     diffusion = models.ForeignKey('emissions.Diffusion')
-    jingle = models.ForeignKey(Jingle, null=True)
+    jingle = models.ForeignKey(Jingle, null=True, blank=True)
     stream = models.ForeignKey(Stream)
+
+    def __str__(self):
+        return self.label
index f4a46f5a6d8625931ec9e952459b3a971d51df8d..aed55922668a8e76fbac34ddc62faba7bae95624 100644 (file)
@@ -9,17 +9,30 @@
   <TimeContinued>1</TimeContinued>
   <SpotController>0</SpotController>
 
+  {% if streamed_diffusion %}
+  <Type>stream</Type>
+     {% if streamed_diffusion.jingle %}
+    <Jingle>:/nonstop/SPOTS/{{ streamed_diffusion.jingle.file_path }}</Jingle>
+    {% else %}
+    <Jingle/>
+    {% endif %}
+    <Stream>{{streamed_diffusion.stream.url}}</Stream>
+  {% else %}
   <Type>files</Type>
-
   <Jingle>:/nonstop/SPOTS/jingles panik/H_ecoutez_RP_zoe_1.wav</Jingle>
+  <Stream></Stream>
+  {% endif %}
 
 
   <Module></Module>
   <ModuleData></ModuleData>
-  <Stream></Stream>
 
   <Random>0</Random>
+  {% if streamed_diffusion %}
+  <SoftStop>0</SoftStop>
+  {% else %}
   <SoftStop>1</SoftStop>
+  {% endif %}
 
   <RatioSong>10</RatioSong>
   <RatioSpot>2</RatioSpot>
diff --git a/nonstop/templates/nonstop/streamed-diffusion.html b/nonstop/templates/nonstop/streamed-diffusion.html
new file mode 100644 (file)
index 0000000..c8eed3a
--- /dev/null
@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+{% load i18n %}
+
+{% block appbar %}
+<h2>{% trans "New Streamed Diffusion" %}</h2>
+{% endblock %}
+
+{% block content %}
+<form method="post" enctype="multipart/form-data">
+  {% csrf_token %}
+  {{ form.as_p }}
+  <div class="buttons">
+    <button class="submit-button">{% trans "Add" %}</button>
+    <a class="cancel" href=".">{% trans "Cancel" %}</a>
+  </div>
+</form>
+{% endblock %}
index e6c077423a57308d54f3b7d201115cc93823a76d..afef4f1a5e96cbfa7049bd8dca60e3e49384ead4 100644 (file)
@@ -3,7 +3,7 @@ from django.conf.urls import url
 from .views import (SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView,
         TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView,
         UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView,
-        SearchCsvView, AddDiffusionView)
+        SearchCsvView, AddDiffusionView, AddStreamedDiffusionView)
 
 urlpatterns = [
     # Example: /2012/nov/10/
@@ -26,6 +26,7 @@ urlpatterns = [
     url(r'^quick-links/$', QuickLinksView.as_view(), name='nonstop-quick-links'),
     url(r'^cleanup/$', CleanupView.as_view(), name='nonstop-cleanup'),
 
-    # soma mangement for episodes
+    # soma management for episodes
     url(r'^diffusion/(?P<pk>\d+)/add/$', AddDiffusionView.as_view(), name='nonstop-add-diffusion'),
+    url(r'^diffusion/(?P<pk>\d+)/add-stream/$', AddStreamedDiffusionView.as_view(), name='nonstop-add-streamed-diffusion'),
 ]
index c6067b90364f5e62eb5376786eb6f8452d6bab14..f165baf88ec89b8373d6ceb67a5f3b79f457e80a 100644 (file)
@@ -7,7 +7,7 @@ import time
 from django.template import loader
 import xml.etree.ElementTree as ET
 
-from .models import Track, SomaLogLine, LOCAL_BASE_PATH
+from .models import Track, SomaLogLine, StreamedDiffusion, LOCAL_BASE_PATH
 
 
 class SomaException(Exception):
@@ -48,32 +48,42 @@ def get_diffusion_file_path(diffusion):
             diffusion.episode.emission.slug)
 
 def is_already_in_soma(diffusion):
+    if StreamedDiffusion.objects.filter(diffusion=diffusion).count():
+        return True
     return os.path.exists(os.path.join(LOCAL_BASE_PATH, get_diffusion_file_path(diffusion)))
 
 class DuplicateDiffusionSlot(Exception):
     pass
 
 def add_diffusion(diffusion):
-    soundfile = diffusion.episode.soundfile_set.filter(fragment=False)[0]
-    diffusion_path = get_diffusion_file_path(diffusion)
-
-    # copy file
-    if os.path.exists(LOCAL_BASE_PATH):
-        if os.path.exists(os.path.join(LOCAL_BASE_PATH, diffusion_path)):
-            raise DuplicateDiffusionSlot()
-        os.mkdir(os.path.join(LOCAL_BASE_PATH, diffusion_path))
-        shutil.copyfile(soundfile.file.path,
-            os.path.join(LOCAL_BASE_PATH, diffusion_path, os.path.basename(soundfile.file.path)))
+    context = {}
+    streamed_diffusion = StreamedDiffusion.objects.filter(diffusion=diffusion).first()
+    if streamed_diffusion:
+        # program a stream
+        context['streamed_diffusion'] = streamed_diffusion
+        context['end'] = diffusion.end_datetime
+    else:
+        # program a soundfile
+        soundfile = diffusion.episode.soundfile_set.filter(fragment=False)[0]
+        diffusion_path = get_diffusion_file_path(diffusion)
+
+        # copy file
+        if os.path.exists(LOCAL_BASE_PATH):
+            if os.path.exists(os.path.join(LOCAL_BASE_PATH, diffusion_path)):
+                raise DuplicateDiffusionSlot()
+            os.mkdir(os.path.join(LOCAL_BASE_PATH, diffusion_path))
+            shutil.copyfile(soundfile.file.path,
+                os.path.join(LOCAL_BASE_PATH, diffusion_path, os.path.basename(soundfile.file.path)))
+
+        context['diffusion_path'] = diffusion_path
+        # end should be a bit before the real end of file so the same file doesn't
+        # get repeated.
+        context['end'] = diffusion.datetime + datetime.timedelta(seconds=(soundfile.duration or 300) - 180)
 
     # create palinsesti
     palinsesti_template = loader.get_template('nonstop/soma_palinsesti.xml')
-    context = {}
-    context['diffusion_path'] = diffusion_path
     context['episode'] = diffusion.episode
     context['start'] = diffusion.datetime
-    # end should be a bit before the real end of file so the same file doesn't
-    # get repeated.
-    context['end'] = diffusion.datetime + datetime.timedelta(seconds=(soundfile.duration or 300) - 180)
 
     palinsesti = palinsesti_template.render(context)
     palinsesti_xml = ET.fromstring(palinsesti.encode('utf-8'))
index 1d86142b57d8ec6898e1e44066d8aac86cac768c..ae3e57f43ae2a0601d72d4754e79628625a73a78 100644 (file)
@@ -17,11 +17,11 @@ from django.utils.translation import ugettext_lazy as _
 from django.views.generic.base import RedirectView, TemplateView
 from django.views.generic.dates import DayArchiveView
 from django.views.generic.detail import DetailView
-from django.views.generic.edit import FormView
+from django.views.generic.edit import CreateView, FormView
 from django.views.generic.list import ListView
 
 from .forms import UploadTracksForm, TrackMetaForm, TrackSearchForm, CleanupForm
-from .models import SomaLogLine, Track, Artist, NonstopFile
+from .models import SomaLogLine, Track, Artist, NonstopFile, StreamedDiffusion
 from emissions.models import Nonstop, Diffusion
 
 from . import utils
@@ -366,3 +366,27 @@ class AddDiffusionView(DetailView):
         return HttpResponseRedirect(reverse('episode-view', kwargs={
             'emission_slug': episode.emission.slug,
             'slug': episode.slug}))
+
+
+class AddStreamedDiffusionView(CreateView):
+    model = StreamedDiffusion
+    fields = ['jingle', 'stream']
+    template_name = 'nonstop/streamed-diffusion.html'
+
+    def form_valid(self, form):
+        form.instance.diffusion_id = self.kwargs['pk']
+        response = super(AddStreamedDiffusionView, self).form_valid(form)
+        return response
+
+    def get_success_url(self):
+        diffusion = Diffusion.objects.get(id=self.kwargs['pk'])
+        episode = diffusion.episode
+        try:
+            utils.add_diffusion(diffusion)
+        except utils.SomaException as e:
+            messages.error(self.request, _('technical soma error (%s)') % e)
+        else:
+            messages.info(self.request, _('%s added to soma') % episode.emission.title)
+        return reverse('episode-view', kwargs={
+            'emission_slug': episode.emission.slug,
+            'slug': episode.slug})