...
 
Commits (2)
......@@ -11,7 +11,9 @@ def get_optional_nonstop_zones():
return [('', '')] + get_nonstop_zones()
def get_search_nonstop_zones():
return [('', 'All'), ('', '--------------')] + get_nonstop_zones() + [('', '--------------'), ('none', 'None')]
return [('', 'All'),
('any', _('Any')),
('', '--------------')] + get_nonstop_zones() + [('', '--------------'), ('none', 'None')]
class UploadTracksForm(forms.Form):
tracks = forms.FileField(widget=forms.ClearableFileInput(
......
......@@ -42,6 +42,9 @@
<a href="?{{ qs }}&page={{ tracks.next_page_number }}"></a>
{% endif %}
</span>
<span class="download">
<a href="csv?{{ qs }}" download="nonstop.csv">CSV</a>
</span>
</div>
{% endif %}
......
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 = [
# Example: /2012/nov/10/
......@@ -19,6 +19,7 @@ urlpatterns = [
url(r'^upload/$', UploadTracksView.as_view(), name='nonstop-upload-tracks'),
url(r'^recent/$', RecentTracksView.as_view(), name='nonstop-recent-tracks'),
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'^cleanup/$', CleanupView.as_view(), name='nonstop-cleanup'),
]
......@@ -248,9 +248,7 @@ class QuickLinksView(TemplateView):
class SearchView(TemplateView):
template_name = 'nonstop/search.html'
def get_context_data(self, **kwargs):
ctx = super(SearchView, self).get_context_data(**kwargs)
ctx['form'] = TrackSearchForm(self.request.GET)
def get_queryset(self):
queryset = Track.objects.all()
q = self.request.GET.get('q')
......@@ -262,18 +260,25 @@ class SearchView(TemplateView):
from emissions.models import Nonstop
if zone == 'none':
queryset = queryset.filter(nonstop_zones=None)
elif zone == 'any':
queryset = queryset.filter(nonstop_zones__isnull=False).distinct()
else:
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'
if order:
if 'added_to_nonstop_timestamp' in order:
queryset = queryset.filter(added_to_nonstop_timestamp__isnull=False)
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)
......@@ -288,6 +293,22 @@ class SearchView(TemplateView):
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):
template_name = 'nonstop/cleanup.html'
......