From 2355e0a04c579318e9beacb7d1ae7920dca2e856 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 7 Jan 2020 13:48:54 +0100 Subject: [PATCH] add possibility to set jingle of diffusions --- nonstop/forms.py | 11 ++++- nonstop/templates/nonstop/add-diffusion.html | 23 ++++++++++ nonstop/templates/nonstop/soma_palinsesti.xml | 13 +++--- nonstop/utils.py | 8 +++- nonstop/views.py | 44 ++++++++++++------- 5 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 nonstop/templates/nonstop/add-diffusion.html diff --git a/nonstop/forms.py b/nonstop/forms.py index bf2e3fb..0b447e0 100644 --- a/nonstop/forms.py +++ b/nonstop/forms.py @@ -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 index 0000000..6e16afc --- /dev/null +++ b/nonstop/templates/nonstop/add-diffusion.html @@ -0,0 +1,23 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block appbar %} +

{% trans "Schedule Diffusion" %}

+{% endblock %} + +{% block content %} +
+
+

+ Attention, il n'est pas possible après validation de modifier le jingle ou de + retirer la diffusion. +

+
+ {% csrf_token %} + {{ form.as_p }} +
+ + {% trans "Cancel" %} +
+
+{% endblock %} diff --git a/nonstop/templates/nonstop/soma_palinsesti.xml b/nonstop/templates/nonstop/soma_palinsesti.xml index b16fb71..e901ea0 100644 --- a/nonstop/templates/nonstop/soma_palinsesti.xml +++ b/nonstop/templates/nonstop/soma_palinsesti.xml @@ -11,18 +11,17 @@ {% if streamed_diffusion %} stream - {% if streamed_diffusion.jingle %} - :/nonstop/SPOTS/{{ streamed_diffusion.jingle.file_path }} - {% else %} - - {% endif %} - {{streamed_diffusion.stream.url}} + {{streamed_diffusion.stream.url}} {% else %} files - :/nonstop/SPOTS/jingles panik/H_ecoutez_RP_zoe_1.wav {% endif %} + {% if jingle %} + :/nonstop/SPOTS/{{ jingle.file_path }} + {% else %} + + {% endif %} diff --git a/nonstop/utils.py b/nonstop/utils.py index ef0662e..9941d79 100644 --- a/nonstop/utils.py +++ b/nonstop/utils.py @@ -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 diff --git a/nonstop/views.py b/nonstop/views.py index 78510ae..ebb952a 100644 --- a/nonstop/views.py +++ b/nonstop/views.py @@ -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}) -- 2.39.2