views.py 9.14 KB
Newer Older
1
import copy
fred's avatar
fred committed
2
import csv
3
import datetime
fred's avatar
fred committed
4
from cStringIO import StringIO
fred's avatar
fred committed
5
import os
6
import tempfile
7

fred's avatar
fred committed
8 9 10
import mutagen

from django.core.files.storage import default_storage
11
from django.core.urlresolvers import reverse
fred's avatar
fred committed
12
from django.contrib import messages
13
from django.http import HttpResponse, HttpResponseRedirect
fred's avatar
fred committed
14
from django.utils.translation import ugettext_lazy as _
fred's avatar
fred committed
15
from django.views.generic.base import RedirectView, TemplateView
fred's avatar
fred committed
16
from django.views.generic.dates import DayArchiveView
fred's avatar
fred committed
17
from django.views.generic.detail import DetailView
fred's avatar
fred committed
18
from django.views.generic.edit import FormView
fred's avatar
fred committed
19
from django.views.generic.list import ListView
fred's avatar
fred committed
20

21
from .forms import UploadTracksForm, TrackMetaForm
fred's avatar
fred committed
22
from .models import SomaLogLine, Track, Artist, NonstopFile
fred's avatar
fred committed
23
from emissions.models import Nonstop
fred's avatar
fred committed
24 25 26 27 28 29

class SomaDayArchiveView(DayArchiveView):
    queryset = SomaLogLine.objects.all()
    date_field = "play_timestamp"
    make_object_list = True
    allow_future = False
30 31 32
    month_format = '%m'


fred's avatar
fred committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class SomaDayArchiveCsvView(SomaDayArchiveView):
    def render_to_response(self, context, **response_kwargs):
        out = StringIO()
        writer = csv.writer(out)
        for line in context['object_list']:
            if line.filepath.track:
                writer.writerow([line.play_timestamp.strftime('%Y-%m-%d %H:%M'),
                    line.filepath.short.encode('utf-8', 'replace'),
                    line.filepath.track.title.encode('utf-8', 'replace'),
                    line.filepath.track.artist.name.encode('utf-8', 'replace'),
                    line.filepath.track.language,
                    line.filepath.track.instru and 'instru' or '',
                    line.filepath.track.cfwb and 'cfwb' or ''])
            else:
                writer.writerow([line.play_timestamp.strftime('%Y-%m-%d %H:%M'),
                                line.filepath.short.encode('utf-8', 'replace')])
49
        return HttpResponse(out.getvalue(), content_type='text/csv; charset=utf-8')
fred's avatar
fred committed
50 51


52 53 54 55 56 57 58
class RedirectTodayView(RedirectView):
    def get_redirect_url(self, *args, **kwargs):
        today = datetime.datetime.today()
        return reverse('archive_day', kwargs={
                        'year': today.year,
                        'month': today.month,
                        'day': today.day})
fred's avatar
fred committed
59 60 61 62


class TrackDetailView(DetailView):
    model = Track
fred's avatar
fred committed
63

64 65
    def get_context_data(self, **kwargs):
        ctx = super(TrackDetailView, self).get_context_data(**kwargs)
66
        ctx['metadata_form'] = TrackMetaForm(instance=self.object)
67 68 69 70 71
        return ctx

    def post(self, request, *args, **kwargs):
        instance = self.get_object()
        old_nonstop_zones = copy.copy(instance.nonstop_zones.all())
72 73
        form = TrackMetaForm(request.POST, instance=instance)
        form.save()
74 75 76 77 78
        new_nonstop_zones = self.get_object().nonstop_zones.all()
        if set(old_nonstop_zones) != set(new_nonstop_zones):
            instance.sync_nonstop_zones()
        return HttpResponseRedirect('.')

fred's avatar
fred committed
79 80 81

class ArtistDetailView(DetailView):
    model = Artist
fred's avatar
fred committed
82 83 84 85


class ArtistListView(ListView):
    model = Artist
fred's avatar
fred committed
86 87 88


class ZoneStats(object):
89
    def __init__(self, zone, from_date=None, until_date=None, **kwargs):
fred's avatar
fred committed
90
        self.zone = zone
91 92 93 94 95 96 97
        self.qs = Track.objects.filter(nonstop_zones=self.zone, **kwargs)
        self.from_date = from_date
        if from_date:
            self.qs = self.qs.filter(nonstopfile__somalogline__play_timestamp__gte=from_date)
        if until_date:
            self.qs = self.qs.filter(nonstopfile__somalogline__play_timestamp__lte=until_date)
        self.qs = self.qs.distinct()
fred's avatar
fred committed
98 99

    def count(self, **kwargs):
100
        return self.qs.filter(**kwargs).count()
fred's avatar
fred committed
101 102 103 104 105

    def percentage(self, **kwargs):
        total = self.count()
        if total == 0:
            return '-'
106
        return '%.2f%%' % (100. * self.count(**kwargs) / total)
fred's avatar
fred committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    def instru(self):
        return self.count(instru=True)

    def instru_percentage(self):
        return self.percentage(instru=True)

    def sabam(self):
        return self.count(sabam=True)

    def sabam_percentage(self):
        return self.percentage(sabam=True)

    def cfwb(self):
        return self.count(cfwb=True)

    def cfwb_percentage(self):
        return self.percentage(cfwb=True)

fred's avatar
fred committed
126 127 128 129 130 131 132
    def french(self):
        return self.count(language='fr')

    def french_percentage(self):
        considered_tracks = self.count() - self.instru()
        if considered_tracks == 0:
            return '-'
133
        return '%.2f%%' % (100. * self.french() / considered_tracks)
fred's avatar
fred committed
134

135 136 137 138 139 140 141 142 143 144 145 146
    def new_files(self):
        return self.count(nonstopfile__creation_timestamp__gte=self.from_date)

    def percent_new_files(self):
        return self.percentage(nonstopfile__creation_timestamp__gte=self.from_date)


def parse_date(date):
    if date.endswith('d'):
        return datetime.datetime.today() + datetime.timedelta(int(date.rstrip('d')))
    return datetime.datetime.strptime(date, '%Y-%m-%d').date()

fred's avatar
fred committed
147 148 149 150 151 152
class StatisticsView(TemplateView):
    template_name = 'nonstop/statistics.html'

    def get_context_data(self, **kwargs):
        context = super(StatisticsView, self).get_context_data(**kwargs)
        context['zones'] = Nonstop.objects.all().order_by('start')
153 154 155 156 157 158 159 160
        kwargs = {}
        if 'from' in self.request.GET:
            kwargs['from_date'] = parse_date(self.request.GET['from'])
            context['from_date'] = kwargs['from_date']
        if 'until' in self.request.GET:
            kwargs['until_date'] = parse_date(self.request.GET['until'])
        if 'onair' in self.request.GET:
            kwargs['nonstopfile__somalogline__on_air'] = True
fred's avatar
fred committed
161
        for zone in context['zones']:
162
            zone.stats = ZoneStats(zone, **kwargs)
fred's avatar
fred committed
163
        return context
fred's avatar
fred committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179


class UploadTracksView(FormView):
    form_class = UploadTracksForm
    template_name = 'nonstop/upload.html'
    success_url = '.'

    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        tracks = request.FILES.getlist('tracks')
        if not form.is_valid():
            return self.form_invalid(form)
        missing_metadata = []
        metadatas = {}
        for f in tracks:
180 181 182 183
            with tempfile.NamedTemporaryFile(prefix='track-upload') as tmpfile:
                tmpfile.write(f.read())
                f.seek(0)
                metadata = mutagen.File(tmpfile.name, easy=True)
fred's avatar
fred committed
184 185 186 187 188 189 190 191 192
            if not metadata or not metadata.get('artist') or not metadata.get('title'):
                missing_metadata.append(f.name)
            else:
                metadatas[f.name] = metadata
        if missing_metadata:
            form.add_error('tracks', _('Missing metadata in: ') + ', '.join(missing_metadata))
            return self.form_invalid(form)

        for f in tracks:
193 194 195 196
            metadata = metadatas[f.name]
            artist_name = metadata.get('artist')[0]
            track_title = metadata.get('title')[0]

fred's avatar
fred committed
197
            monthdir = datetime.datetime.today().strftime('%Y-%m')
198 199 200 201 202 203
            filepath = '%s/%s - %s - %s%s' % (monthdir,
                datetime.datetime.today().strftime('%y%m%d'),
                artist_name[:50].replace('/', ' ').strip(),
                track_title[:80].replace('/', ' ').strip(),
                os.path.splitext(f.name)[-1])

fred's avatar
fred committed
204
            default_storage.save(os.path.join('nonstop', 'tracks', filepath), content=f)
205

fred's avatar
fred committed
206 207
            nonstop_file = NonstopFile()
            nonstop_file.set_track_filepath(filepath)
208
            artist, created = Artist.objects.get_or_create(name=artist_name)
fred's avatar
fred committed
209 210
            track, created = Track.objects.get_or_create(title=track_title, artist=artist,
                    defaults={'uploader': self.request.user})
fred's avatar
fred committed
211 212 213 214 215
            nonstop_file.track = track
            nonstop_file.save()
            if request.POST.get('nonstop_zone'):
                track.nonstop_zones.add(
                        Nonstop.objects.get(id=request.POST.get('nonstop_zone')))
216
            nonstop_file.track.sync_nonstop_zones()
fred's avatar
fred committed
217 218 219

        messages.info(self.request, '%d new track(s)' % len(tracks))
        return self.form_valid(form)
220 221 222 223 224 225


class RecentTracksView(ListView):
    template_name = 'nonstop/recent_tracks.html'

    def get_queryset(self):
fred's avatar
fred committed
226
        return Track.objects.exclude(creation_timestamp__isnull=True).order_by('-creation_timestamp')[:50]
227 228 229 230 231 232 233 234 235 236

    def post(self, request, *args, **kwargs):
        for track_id in request.POST.getlist('track'):
            track = Track.objects.get(id=track_id)
            track.language = request.POST.get('lang-%s' % track_id, '')
            track.instru = 'instru-%s' % track_id in request.POST
            track.sabam = 'sabam-%s' % track_id in request.POST
            track.cfwb = 'cfwb-%s' % track_id in request.POST
            track.save()
        return HttpResponseRedirect('.')
fred's avatar
fred committed
237 238 239 240


class QuickLinksView(TemplateView):
    template_name = 'nonstop/quick_links.html'
fred's avatar
fred committed
241 242 243 244 245 246 247 248 249 250


class SearchView(TemplateView):
    template_name = 'nonstop/search.html'

    def get_context_data(self, **kwargs):
        ctx = super(SearchView, self).get_context_data(**kwargs)
        q = self.request.GET.get('q')
        if q:
            ctx['artists'] = Artist.objects.filter(name__icontains=q.lower())
fred's avatar
fred committed
251
            ctx['tracks'] = Track.objects.filter(title__icontains=q.lower()).order_by('title').select_related()
fred's avatar
fred committed
252
        return ctx