diff --git a/nonstop/templates/nonstop/soma_palinsesti.xml b/nonstop/templates/nonstop/soma_palinsesti.xml
new file mode 100644
index 0000000000000000000000000000000000000000..87d493b5c0bc88f6334b4f1ba795d4e2ec6feec8
--- /dev/null
+++ b/nonstop/templates/nonstop/soma_palinsesti.xml
@@ -0,0 +1,34 @@
+{% load l10n %}
+{% localize off %}
+
+ {{ episode.emission.title }} - {{ episode.title }}
+
+ 1
+ {{ start|date:"Y-m-d x H:i" }}
+ {{ end|date:"Y-m-d x H:i" }}
+ 0
+ 0
+
+ files
+
+
+
+
+
+
+
+ 0
+ 1
+
+ 10
+ 2
+
+
+ - :/nonstop/{{ diffusion_path }}/
+
+
+
+
+
+
+{% endlocalize %}
diff --git a/nonstop/templatetags/__init__.py b/nonstop/templatetags/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
diff --git a/nonstop/templatetags/nonstop.py b/nonstop/templatetags/nonstop.py
new file mode 100644
index 0000000000000000000000000000000000000000..e5298463528c931e73e34be2152c5666eafc7e26
--- /dev/null
+++ b/nonstop/templatetags/nonstop.py
@@ -0,0 +1,10 @@
+from django.template import Library
+
+from .. import utils
+
+register = Library()
+
+
+@register.filter
+def is_already_in_soma(diffusion):
+ return utils.is_already_in_soma(diffusion)
diff --git a/nonstop/urls.py b/nonstop/urls.py
index a17d5715cb44023d7277f9d6a66332d27456c67d..e6c077423a57308d54f3b7d201115cc93823a76d 100644
--- a/nonstop/urls.py
+++ b/nonstop/urls.py
@@ -1,6 +1,9 @@
from django.conf.urls import url
-from .views import SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView, TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView, UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView, SearchCsvView
+from .views import (SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView,
+ TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView,
+ UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView,
+ SearchCsvView, AddDiffusionView)
urlpatterns = [
# Example: /2012/nov/10/
@@ -22,4 +25,7 @@ urlpatterns = [
url(r'^search/csv$', SearchCsvView.as_view(), name='nonstop-search-csv'),
url(r'^quick-links/$', QuickLinksView.as_view(), name='nonstop-quick-links'),
url(r'^cleanup/$', CleanupView.as_view(), name='nonstop-cleanup'),
+
+ # soma mangement for episodes
+ url(r'^diffusion/(?P\d+)/add/$', AddDiffusionView.as_view(), name='nonstop-add-diffusion'),
]
diff --git a/nonstop/utils.py b/nonstop/utils.py
index f44895b01f9ca5cd12ca1ca321576369e18b3ebc..722b6b6a0b0fbe55b8d93d19867548bc723129a7 100644
--- a/nonstop/utils.py
+++ b/nonstop/utils.py
@@ -1,6 +1,16 @@
import datetime
+import os
+import shutil
-from .models import Track, SomaLogLine
+from django.template import loader, Context
+import xml.etree.ElementTree as ET
+
+try:
+ import pysoma
+except ImportError:
+ pysoma = None
+
+from .models import Track, SomaLogLine, LOCAL_BASE_PATH
def get_current_nonstop_track():
@@ -29,3 +39,50 @@ def get_current_nonstop_track():
if current_track.artist:
d['track_artist'] = current_track.artist.name
return d
+
+
+def get_diffusion_file_path(diffusion):
+ return u'diffusions-auto/%s--%s' % (
+ diffusion.datetime.strftime('%Y%m%d-%H%M'),
+ diffusion.episode.emission.slug)
+
+def is_already_in_soma(diffusion):
+ if not pysoma:
+ # can't tell but as we couldn't add it anyway, let's lie.
+ return True
+ return os.path.exists(os.path.join(LOCAL_BASE_PATH, get_diffusion_file_path(diffusion)))
+
+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):
+ 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)))
+
+ # create palinsesti
+ palinsesti_template = loader.get_template('nonstop/soma_palinsesti.xml')
+ context = 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'))
+
+ # append to soma
+ if pysoma:
+ connection = pysoma.open_tcp('soma.panik', 12521, '', 0)
+ palinsesto_xml = ET.fromstring(connection.get_palinsesto())
+ palinsesto_xml.append(palinsesti_xml)
+ with open('/tmp/soma.pl', 'w') as fd:
+ fd.write(ET.tostring(palinsesto_xml))
+
+ connection.new_palinsesto('/tmp/soma.pl')
+ connection.set_default_palinsesto()
+ del connection
diff --git a/nonstop/views.py b/nonstop/views.py
index 16cab277684fc7646ad1698592c5b7a3d573ccb0..b156987e833a6610bcc8eacdea4829902242b241 100644
--- a/nonstop/views.py
+++ b/nonstop/views.py
@@ -22,7 +22,10 @@ from django.views.generic.list import ListView
from .forms import UploadTracksForm, TrackMetaForm, TrackSearchForm, CleanupForm
from .models import SomaLogLine, Track, Artist, NonstopFile
-from emissions.models import Nonstop
+from emissions.models import Nonstop, Diffusion
+
+from . import utils
+
class SomaDayArchiveView(DayArchiveView):
queryset = SomaLogLine.objects.all()
@@ -341,3 +344,16 @@ class CleanupView(TemplateView):
if count:
messages.info(self.request, 'Removed %d new track(s)' % count)
return HttpResponseRedirect('.')
+
+
+class AddDiffusionView(DetailView):
+ model = Diffusion
+
+ def get(self, request, *args, **kwargs):
+ diffusion = self.get_object()
+ utils.add_diffusion(diffusion)
+ episode = diffusion.episode
+ messages.info(self.request, _('%s added to soma') % episode.emission.title)
+ return HttpResponseRedirect(reverse('episode-view', kwargs={
+ 'emission_slug': episode.emission.slug,
+ 'slug': episode.slug}))