You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.9 KiB
Python
143 lines
4.9 KiB
Python
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
|
|
|
|
from .forms import InsertFormSet, RateArtistForm
|
|
from .models import Participant, Rating
|
|
from ..core.models import Artist, Session
|
|
|
|
import csv
|
|
|
|
# Create your views here.
|
|
|
|
@login_required
|
|
def create_token(request):
|
|
session = get_object_or_404(Session, pk=request.session.get('session'))
|
|
|
|
token = None
|
|
if request.method == 'POST':
|
|
participant = Participant(session=session)
|
|
participant.save()
|
|
|
|
token = request.build_absolute_uri(reverse('participate', args=(participant.token,)))
|
|
|
|
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):
|
|
session = get_object_or_404(Session, pk=session)
|
|
|
|
formset = InsertFormSet()
|
|
|
|
if request.method == 'POST':
|
|
formset = InsertFormSet(request.POST)
|
|
if formset.is_valid():
|
|
participant = Participant(session=session)
|
|
participant.save()
|
|
|
|
for form in formset:
|
|
if not form.cleaned_data.get('name'):
|
|
continue
|
|
|
|
artist = get_object_or_404(Artist, name=form.cleaned_data['name'])
|
|
|
|
rating = Rating(artist=artist, participant=participant, value=form.cleaned_data['rating'])
|
|
rating.save()
|
|
|
|
formset = InsertFormSet() # clear formset if successful
|
|
|
|
playlist = list(session.playlist.artist_set.values_list('name', flat=True))
|
|
|
|
return render(request, 'vote/insert.html', {'formset': formset, 'playlist': playlist})
|
|
|
|
def join_session(request, session):
|
|
session = get_object_or_404(Session, pk=session)
|
|
|
|
if 'token' in request.session:
|
|
try:
|
|
participant = Participant.objects.get(pk=request.session['token'])
|
|
if participant.session == session:
|
|
return redirect('current_artist', session.pk)
|
|
except Participant.DoesNotExist:
|
|
pass
|
|
|
|
del request.session['token']
|
|
|
|
if request.method == 'POST':
|
|
participant = Participant(session=session)
|
|
participant.save()
|
|
|
|
request.session['token'] = participant.pk
|
|
return redirect('current_artist', session.playlist.pk)
|
|
|
|
return render(request, 'vote/join_session.html', {})
|
|
|
|
def participate(request, token):
|
|
if 'token' in request.session:
|
|
try:
|
|
participant = Participant.objects.get(pk=request.session['token'])
|
|
if participant.token == token:
|
|
return redirect('current_artist', participant.session.pk)
|
|
except Participant.DoesNotExist:
|
|
pass
|
|
|
|
del request.session['token']
|
|
|
|
participant = get_object_or_404(Participant, pk=token)
|
|
|
|
request.session['token'] = participant.pk
|
|
return redirect('current_artist', participant.session.playlist.pk)
|
|
|
|
def artist(request, playlist, offset=None):
|
|
participant = get_object_or_404(Participant, pk=request.session.get('token'))
|
|
|
|
session_offset = participant.session.offset
|
|
offset = offset if offset != None else session_offset
|
|
if offset > session_offset:
|
|
return redirect('artist', playlist, session_offset)
|
|
|
|
playlist = participant.session.playlist
|
|
|
|
artists = playlist.artist_set
|
|
artist = artists.order_by('id')[offset]
|
|
|
|
rating = None
|
|
if Rating.objects.filter(artist=artist, participant=participant).exists():
|
|
rating = Rating.objects.get(artist=artist, participant=participant)
|
|
|
|
form = RateArtistForm()
|
|
|
|
if request.method == 'POST':
|
|
form = RateArtistForm(request.POST)
|
|
if not form.is_valid():
|
|
return redirect('artist', playlist, session_offset)
|
|
|
|
if not rating:
|
|
rating = Rating(artist=artist, participant=participant)
|
|
|
|
rating.value = form.cleaned_data['rating']
|
|
rating.save()
|
|
elif rating and rating.value:
|
|
form = RateArtistForm({'rating': rating.value})
|
|
|
|
has_rating = rating != None
|
|
return render(request, 'vote/artist.html', {'artist': artist, 'count': artists.count(), 'form': form, 'has_rating': has_rating, 'offset': offset, 'playlist': playlist})
|