]> git.0d.be Git - django-panik-nonstop.git/commitdiff
add possibility to set jingle of diffusions
authorFrédéric Péters <fpeters@0d.be>
Tue, 7 Jan 2020 12:48:54 +0000 (13:48 +0100)
committerFrédéric Péters <fpeters@0d.be>
Tue, 7 Jan 2020 12:48:54 +0000 (13:48 +0100)
nonstop/forms.py
nonstop/templates/nonstop/add-diffusion.html [new file with mode: 0644]
nonstop/templates/nonstop/soma_palinsesti.xml
nonstop/utils.py
nonstop/views.py

index bf2e3fbd039e8d66ccf10173f4ac503764041866..0b447e07caedff54fc5572a73492ba71a8f84fdc 100644 (file)
@@ -1,7 +1,7 @@
 from django import forms
 from django.utils.translation import ugettext_lazy as _
 
-from .models import Track
+from .models import Track, Jingle
 
 def get_nonstop_zones():
     from emissions.models import Nonstop
@@ -40,3 +40,12 @@ class TrackSearchForm(forms.Form):
 
 class CleanupForm(forms.Form):
     zone = forms.ChoiceField(label=_('Nonstop Zone'), choices=get_optional_nonstop_zones)
+
+
+
+def get_optional_jingle():
+    return [('', '')] + [(x.id, x.label) for x in Jingle.objects.all()]
+
+
+class AddDiffusionForm(forms.Form):
+    jingle = forms.ChoiceField(label=_('Jingle'), choices=get_optional_jingle)
diff --git a/nonstop/templates/nonstop/add-diffusion.html b/nonstop/templates/nonstop/add-diffusion.html
new file mode 100644 (file)
index 0000000..6e16afc
--- /dev/null
@@ -0,0 +1,23 @@
+{% extends "base.html" %}
+{% load i18n %}
+
+{% block appbar %}
+<h2>{% trans "Schedule Diffusion" %}</h2>
+{% endblock %}
+
+{% block content %}
+<form method="post" enctype="multipart/form-data">
+  <div class="infonotice" style="max-width: 30em">
+  <p>
+  Attention, il n'est pas possible après validation de modifier le jingle ou de
+  retirer la diffusion.
+  </p>
+  </div>
+  {% 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 b16fb719a71f956d23cbe3d954a1c8a3556dae5b..e901ea086bf2853f86edf9652efc6bc0679ef818 100644 (file)
 
   {% 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>
+  <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 %}
 
+  {% if jingle %}
+  <Jingle>:/nonstop/SPOTS/{{ jingle.file_path }}</Jingle>
+  {% else %}
+  <Jingle/>
+  {% endif %}
 
   <Module></Module>
   <ModuleData></ModuleData>
index ef0662e5b4e19aa536b03f77b18a4553e75d916d..9941d795343dd1cfab450f14c7713465eeaacc8f 100644 (file)
@@ -7,7 +7,7 @@ import time
 from django.template import loader
 import xml.etree.ElementTree as ET
 
-from .models import Track, SomaLogLine, StreamedDiffusion, LOCAL_BASE_PATH
+from .models import Track, SomaLogLine, StreamedDiffusion, Jingle, LOCAL_BASE_PATH
 
 
 class SomaException(Exception):
@@ -55,13 +55,14 @@ def is_already_in_soma(diffusion):
 class DuplicateDiffusionSlot(Exception):
     pass
 
-def add_diffusion(diffusion):
+def add_diffusion(diffusion, **kwargs):
     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
+        context['jingle'] = streamed_diffusion.jingle
     else:
         # program a soundfile
         soundfile = diffusion.episode.soundfile_set.filter(fragment=False)[0]
@@ -80,6 +81,9 @@ def add_diffusion(diffusion):
         # get repeated.
         context['end'] = diffusion.datetime + datetime.timedelta(seconds=(soundfile.duration or 300) - 180)
 
+        if 'jingle_id' in kwargs:
+            context['jingle'] = Jingle.objects.get(id=kwargs['jingle_id'])
+
     # create palinsesti
     palinsesti_template = loader.get_template('nonstop/soma_palinsesti.xml')
     context['episode'] = diffusion.episode
index 78510ae493f3bc97687e63030c57f8994b2e8c00..ebb952a685ccfbc31b2ec2940c780bd0544d960a 100644 (file)
@@ -20,7 +20,7 @@ from django.views.generic.detail import DetailView
 from django.views.generic.edit import CreateView, FormView
 from django.views.generic.list import ListView
 
-from .forms import UploadTracksForm, TrackMetaForm, TrackSearchForm, CleanupForm
+from .forms import UploadTracksForm, TrackMetaForm, TrackSearchForm, CleanupForm, AddDiffusionForm
 from .models import SomaLogLine, Track, Artist, NonstopFile, StreamedDiffusion, Jingle, LOCAL_BASE_PATH
 from emissions.models import Nonstop, Diffusion
 
@@ -349,23 +349,37 @@ class CleanupView(TemplateView):
         return HttpResponseRedirect('.')
 
 
-class AddDiffusionView(DetailView):
-    model = Diffusion
+class AddDiffusionView(FormView):
+    template_name = 'nonstop/add-diffusion.html'
+    form_class = AddDiffusionForm
 
-    def get(self, request, *args, **kwargs):
-        diffusion = self.get_object()
-        episode = diffusion.episode
+    def get_initial(self):
+        initial = super(AddDiffusionView, self).get_initial()
+        jingle = Jingle.objects.filter(default_for_initial_diffusions=True).first()
+        if jingle:
+            initial['jingle'] = jingle.id
+        return initial
+
+    def form_valid(self, form):
+        diffusion = Diffusion.objects.get(id=self.kwargs['pk'])
         try:
-            utils.add_diffusion(diffusion)
+            utils.add_diffusion(diffusion, jingle_id=form.cleaned_data.get('jingle'))
         except utils.DuplicateDiffusionSlot:
             messages.error(self.request, _('soma slot already in use, the diffusion could not be added.'))
         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 HttpResponseRedirect(reverse('episode-view', kwargs={
+
+        return super(AddDiffusionView, self).form_valid(form)
+
+    def get_success_url(self):
+        diffusion = Diffusion.objects.get(id=self.kwargs['pk'])
+        episode = diffusion.episode
+        return reverse('episode-view', kwargs={
             'emission_slug': episode.emission.slug,
-            'slug': episode.slug}))
+            'slug': episode.slug})
+
 
 
 class AddStreamedDiffusionView(CreateView):
@@ -381,17 +395,17 @@ class AddStreamedDiffusionView(CreateView):
     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)
+            utils.add_diffusion(form.instance.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 response
+
+    def get_success_url(self):
+        diffusion = Diffusion.objects.get(id=self.kwargs['pk'])
+        episode = diffusion.episode
         return reverse('episode-view', kwargs={
             'emission_slug': episode.emission.slug,
             'slug': episode.slug})