Commit ef855a5f authored by fred's avatar fred

make it possible to edit track zones

parent 729bfdd8
from django import forms
from .models import Track
def get_nonstop_zones():
from emissions.models import Nonstop
return [('', '')] + [(x.id, x.title) for x in Nonstop.objects.all()]
return [(x.id, x.title) for x in Nonstop.objects.all()]
def get_optional_nonstop_zones():
return [('', '')] + get_nonstop_zones()
class UploadTracksForm(forms.Form):
tracks = forms.FileField(widget=forms.ClearableFileInput(
attrs={'multiple': True, 'accept': 'audio/*'}))
nonstop_zone = forms.ChoiceField(choices=get_nonstop_zones)
nonstop_zone = forms.ChoiceField(choices=get_optional_nonstop_zones)
class NonstopZonesForm(forms.ModelForm):
class Meta:
model = Track
fields = ['nonstop_zones']
......@@ -7,7 +7,19 @@ from django.db import models
from django.utils.translation import ugettext_lazy as _
REMOTE_BASE_PATH = '/srv/soma/nonstop/'
LOCAL_BASE_PATH = '/media/nonstop/'
TRANCHE_SLUG_DIR_MAPPING = {
'acouphene': 'Acouphene',
'biodiversite': 'Biodiversite',
'l-heure-de-pointe': 'Heure_de_pointe',
'hop-bop-co': 'Hop_Bop_and_co',
'la-panique': 'la_panique',
'le-mange-disque': 'Mange_Disque',
'matin-tranquille': 'Matins_tranquilles',
'reveries': 'Reveries',
'up-beat-tempo': 'Up_Beat_Tempo',
}
class Artist(models.Model):
name = models.CharField(_('Name'), max_length=255)
......@@ -56,6 +68,28 @@ class Track(models.Model):
return SomaLogLine.objects.filter(filepath__track=self
).exclude(on_air=False).order_by('-play_timestamp')
def file_path(self):
return self.nonstopfile_set.all().order_by('creation_timestamp').last().get_local_filepath()
def file_exists(self):
return os.path.exists(self.nonstopfile_set.order_by('creation_timestamp').last().get_local_filepath())
def sync_nonstop_zones(self):
nonstop_file = self.nonstopfile_set.order_by('creation_timestamp').last()
filename = nonstop_file.filename
current_zones = self.nonstop_zones.all()
from emissions.models import Nonstop
for zone in Nonstop.objects.all():
zone_dir = TRANCHE_SLUG_DIR_MAPPING[zone.slug]
zone_path = os.path.join(LOCAL_BASE_PATH, 'Tranches', zone_dir, filename)
if zone in current_zones:
if not os.path.exists(zone_path):
os.symlink(os.path.join('..', '..', nonstop_file.short), zone_path)
else:
if os.path.exists(zone_path):
os.unlink(zone_path)
class NonstopFile(models.Model):
filepath = models.CharField(_('Filepath'), max_length=255)
......@@ -69,6 +103,10 @@ class NonstopFile(models.Model):
def set_track_filepath(self, filepath):
self.filepath = os.path.join(REMOTE_BASE_PATH, 'tracks', filepath)
self.filename = os.path.basename(filepath)
def get_local_filepath(self):
return os.path.join(LOCAL_BASE_PATH, self.short)
class SomaLogLine(models.Model):
......
{% extends "base.html" %}
{% load i18n %}
{% block appbar %}
<h2>Nonstop - Track: {{ object.artist.name }} / {{ object.title }}</h2>
......@@ -6,6 +7,10 @@
{% block content %}
{% if not object.file_exists %}
<p class="missing-file">{% trans "The track is no longer available on the server." %}</p>
{% endif %}
<ul class="soma-list">
{% for somalog in object.recent_diffusions|slice:":20" %}
<li><span class="timestamp">{{ somalog.play_timestamp|date:"d/m/Y H:i" }}</span>:
......@@ -18,4 +23,14 @@
{% endfor %}
</ul>
{% if object.file_exists %}
<form method="POST">
{% csrf_token %}
{{ zones_form.as_p }}
<div class="buttons">
<button class="submit-button">{% trans "Update" %}</button>
</div>
</form>
{% endif %}
{% endblock %}
import copy
import csv
import datetime
from cStringIO import StringIO
......@@ -9,7 +10,7 @@ import mutagen
from django.core.files.storage import default_storage
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.dates import DayArchiveView
......@@ -17,7 +18,7 @@ from django.views.generic.detail import DetailView
from django.views.generic.edit import FormView
from django.views.generic.list import ListView
from .forms import UploadTracksForm
from .forms import UploadTracksForm, NonstopZonesForm
from .models import SomaLogLine, Track, Artist, NonstopFile
from emissions.models import Nonstop
......@@ -60,6 +61,21 @@ class RedirectTodayView(RedirectView):
class TrackDetailView(DetailView):
model = Track
def get_context_data(self, **kwargs):
ctx = super(TrackDetailView, self).get_context_data(**kwargs)
ctx['zones_form'] = NonstopZonesForm(instance=self.object)
return ctx
def post(self, request, *args, **kwargs):
instance = self.get_object()
old_nonstop_zones = copy.copy(instance.nonstop_zones.all())
zones_form = NonstopZonesForm(request.POST, instance=instance)
zones_form.save()
new_nonstop_zones = self.get_object().nonstop_zones.all()
if set(old_nonstop_zones) != set(new_nonstop_zones):
instance.sync_nonstop_zones()
return HttpResponseRedirect('.')
class ArtistDetailView(DetailView):
model = Artist
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment