Commit d8bc6bef authored by fred's avatar fred

add csv download field to search results

parent d699e3f1
...@@ -42,6 +42,9 @@ ...@@ -42,6 +42,9 @@
<a href="?{{ qs }}&page={{ tracks.next_page_number }}"></a> <a href="?{{ qs }}&page={{ tracks.next_page_number }}"></a>
{% endif %} {% endif %}
</span> </span>
<span class="download">
<a href="csv?{{ qs }}" download="nonstop.csv">CSV</a>
</span>
</div> </div>
{% endif %} {% endif %}
......
from django.conf.urls import url from django.conf.urls import url
from .views import SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView, TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView, UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView from .views import SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView, TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView, UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView, SearchCsvView
urlpatterns = [ urlpatterns = [
# Example: /2012/nov/10/ # Example: /2012/nov/10/
...@@ -19,6 +19,7 @@ urlpatterns = [ ...@@ -19,6 +19,7 @@ urlpatterns = [
url(r'^upload/$', UploadTracksView.as_view(), name='nonstop-upload-tracks'), url(r'^upload/$', UploadTracksView.as_view(), name='nonstop-upload-tracks'),
url(r'^recent/$', RecentTracksView.as_view(), name='nonstop-recent-tracks'), url(r'^recent/$', RecentTracksView.as_view(), name='nonstop-recent-tracks'),
url(r'^search/$', SearchView.as_view(), name='nonstop-search'), url(r'^search/$', SearchView.as_view(), name='nonstop-search'),
url(r'^search/csv$', SearchCsvView.as_view(), name='nonstop-search-csv'),
url(r'^quick-links/$', QuickLinksView.as_view(), name='nonstop-quick-links'), url(r'^quick-links/$', QuickLinksView.as_view(), name='nonstop-quick-links'),
url(r'^cleanup/$', CleanupView.as_view(), name='nonstop-cleanup'), url(r'^cleanup/$', CleanupView.as_view(), name='nonstop-cleanup'),
] ]
...@@ -248,9 +248,7 @@ class QuickLinksView(TemplateView): ...@@ -248,9 +248,7 @@ class QuickLinksView(TemplateView):
class SearchView(TemplateView): class SearchView(TemplateView):
template_name = 'nonstop/search.html' template_name = 'nonstop/search.html'
def get_context_data(self, **kwargs): def get_queryset(self):
ctx = super(SearchView, self).get_context_data(**kwargs)
ctx['form'] = TrackSearchForm(self.request.GET)
queryset = Track.objects.all() queryset = Track.objects.all()
q = self.request.GET.get('q') q = self.request.GET.get('q')
...@@ -267,15 +265,20 @@ class SearchView(TemplateView): ...@@ -267,15 +265,20 @@ class SearchView(TemplateView):
else: else:
queryset = queryset.filter(nonstop_zones=zone) queryset = queryset.filter(nonstop_zones=zone)
qs = self.request.GET.copy()
qs.pop('page', None)
ctx['qs'] = qs.urlencode()
order = self.request.GET.get('order_by') or 'title' order = self.request.GET.get('order_by') or 'title'
if order: if order:
if 'added_to_nonstop_timestamp' in order: if 'added_to_nonstop_timestamp' in order:
queryset = queryset.filter(added_to_nonstop_timestamp__isnull=False) queryset = queryset.filter(added_to_nonstop_timestamp__isnull=False)
queryset = queryset.order_by(order) queryset = queryset.order_by(order)
return queryset
def get_context_data(self, **kwargs):
ctx = super(SearchView, self).get_context_data(**kwargs)
ctx['form'] = TrackSearchForm(self.request.GET)
queryset = self.get_queryset()
qs = self.request.GET.copy()
qs.pop('page', None)
ctx['qs'] = qs.urlencode()
tracks = Paginator(queryset.select_related(), 20) tracks = Paginator(queryset.select_related(), 20)
...@@ -290,6 +293,22 @@ class SearchView(TemplateView): ...@@ -290,6 +293,22 @@ class SearchView(TemplateView):
return ctx return ctx
class SearchCsvView(SearchView):
def get(self, request, *args, **kwargs):
out = StringIO()
writer = csv.writer(out)
writer.writerow(['Title', 'Artist', 'Zones', 'Language', 'Instru', 'CFWB'])
for track in self.get_queryset():
writer.writerow([
track.title.encode('utf-8', 'replace') if track.title else 'Inconnu',
track.artist.name.encode('utf-8', 'replace') if (track.artist and track.artist.name) else 'Inconnu',
' + '.join([x.title.encode('utf-8') for x in track.nonstop_zones.all()]),
track.language or '',
track.instru and 'instru' or '',
track.cfwb and 'cfwb' or ''])
return HttpResponse(out.getvalue(), content_type='text/csv; charset=utf-8')
class CleanupView(TemplateView): class CleanupView(TemplateView):
template_name = 'nonstop/cleanup.html' template_name = 'nonstop/cleanup.html'
......
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