Implement CSV export

main
Luca 2 years ago
parent 839d56a8b2
commit 1ad0c61daf

@ -4,6 +4,7 @@ from . import views
urlpatterns = [
path('create_token/', views.create_token, name='create_token'),
path('export/<slug:session>/', views.export, name='export'),
path('insert/<slug:session>/', views.insert, name='insert'),
path('join/<slug:session>/', views.join_session, name='join_session'),
path('participate/<slug:token>/', views.participate, name='participate'),

@ -1,5 +1,7 @@
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.db.models import FilteredRelation, Q
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
@ -7,6 +9,8 @@ from .forms import InsertFormSet, RateArtistForm
from .models import Participant, Rating
from ..core.models import Artist, Session
import csv
# Create your views here.
@login_required
@ -22,6 +26,20 @@ def create_token(request):
return render(request, 'vote/create_token.html', {'token': token})
@login_required
def export(request, session):
session = get_object_or_404(Session, pk=session)
participants = session.participant_set.order_by('token')
response = HttpResponse(content_type='text/csv', headers={'Content-Disposition': f'attachment; filename="{session.token}.csv"'})
writer = csv.writer(response)
for artist in session.playlist.artist_set.all():
writer.writerow([artist.name, artist.genre, artist.origin, artist.comment, artist.link_1, artist.link_2] + [value if value else 'Enthaltung' if value == '' else '' for value in participants.annotate(r=FilteredRelation('rating', condition=Q(rating__artist=artist))).values_list('r__value', flat=True)])
return response
@login_required
@transaction.atomic
def insert(request, session):

Loading…
Cancel
Save