Commit 843e845d authored by fred's avatar fred

add csv export of day playlists

parent 9ea1d6eb
......@@ -2,6 +2,7 @@
{% block appbar %}
<h2>Nonstop - {{ day|date:"d/m/Y" }}</h2>
<span><a href="csv">export CSV</a></span>
{% endblock %}
{% block content %}
......
from django.conf.urls import url
from .views import SomaDayArchiveView, RedirectTodayView, TrackDetailView, ArtistDetailView
from .views import SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView, TrackDetailView, ArtistDetailView
urlpatterns = [
# Example: /2012/nov/10/
......@@ -10,4 +10,7 @@ urlpatterns = [
url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/$',
SomaDayArchiveView.as_view(),
name="archive_day"),
url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/csv$',
SomaDayArchiveCsvView.as_view(),
name="archive_day_csv"),
]
import csv
import datetime
from cStringIO import StringIO
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.views.generic.base import RedirectView
from django.views.generic.dates import DayArchiveView
from django.views.generic.detail import DetailView
......@@ -15,6 +18,25 @@ class SomaDayArchiveView(DayArchiveView):
month_format = '%m'
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')
class RedirectTodayView(RedirectView):
def get_redirect_url(self, *args, **kwargs):
today = datetime.datetime.today()
......
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