views.py 2.01 KB
Newer Older
fred's avatar
fred committed
1
import csv
fred's avatar
fred committed
2
import datetime
fred's avatar
fred committed
3
from cStringIO import StringIO
fred's avatar
fred committed
4 5

from django.core.urlresolvers import reverse
fred's avatar
fred committed
6
from django.http import HttpResponse
fred's avatar
fred committed
7
from django.views.generic.base import RedirectView
fred's avatar
fred committed
8
from django.views.generic.dates import DayArchiveView
fred's avatar
fred committed
9
from django.views.generic.detail import DetailView
fred's avatar
fred committed
10
from django.views.generic.list import ListView
fred's avatar
fred committed
11

fred's avatar
fred committed
12
from .models import SomaLogLine, Track, Artist
fred's avatar
fred committed
13 14 15 16 17 18

class SomaDayArchiveView(DayArchiveView):
    queryset = SomaLogLine.objects.all()
    date_field = "play_timestamp"
    make_object_list = True
    allow_future = False
fred's avatar
fred committed
19 20 21
    month_format = '%m'


fred's avatar
fred committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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')])
        return HttpResponse(out.getvalue(), mimetype='text/csv; charset=utf-8')


fred's avatar
fred committed
41 42 43 44 45 46 47
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
48 49 50 51


class TrackDetailView(DetailView):
    model = Track
fred's avatar
fred committed
52 53 54 55


class ArtistDetailView(DetailView):
    model = Artist
fred's avatar
fred committed
56 57 58 59


class ArtistListView(ListView):
    model = Artist