Go to next artist automatically when enough participants have voted

main
Luca 2 years ago
parent 55137e5a35
commit bb7e7b5089

@ -41,5 +41,16 @@
{% qrcode session_url %}
{{ session_url }}
</div>
{% if offset < count|add:-1 %}
<script>
setInterval(async () => {
const response = await fetch('/may_advance?');
const may_advance = await response.json();
if (may_advance === true) {
location = '{% url 'view_artist' playlist offset|add:1 %}';
}
}, 3000);
</script>
{% endif %}
{% endif %}
{% endblock %}

@ -8,6 +8,7 @@ urlpatterns = [
path('', views.break_view, name='break'),
path('add/', views.add_break_content, name='add_break_content'),
])),
path('may_advance/', views.may_advance, name='may_advance'),
path('session/', views.session, name='session'),
path('<slug:playlist>/', include([
path('', views.playlist, name='playlist'),

@ -2,6 +2,7 @@ from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.db.models import F, Min
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.utils import timezone
@ -11,6 +12,7 @@ from .forms import AddBreakContentForm, CreatePlaylistForm
from .models import BreakContent
from .spreadsheet import get_sheet_data
from ..core.models import Artist, Playlist, Session
from ..vote.models import Rating
def get_absolute_join_session_url(request, session):
return request.build_absolute_uri(reverse('join_session', args=(session.pk,)))
@ -125,6 +127,18 @@ def add_break_content(request):
return render(request, 'host/add_break_content.html', {'form': form})
@login_required
def may_advance(request):
session = get_object_or_404(Session, pk=request.session.get('session'))
artist = session.playlist.artist_set.order_by('id')[session.offset]
num_participants = session.participant_set.count()
if num_participants == 0:
return JsonResponse(False, safe=False)
ratio = Rating.objects.filter(artist=artist, participant__session=session).count() / num_participants
return JsonResponse(ratio >= settings.ADVANCE_THRESHOLD, safe=False)
@login_required
def session(request):
if 'session' in request.session:

@ -164,3 +164,5 @@ RATING_CHOICES = (
BREAK_INTERVAL = int(getenv('BREAK_INTERVAL', 30)) # minutes
LOGIN_URL = '/admin/login/'
ADVANCE_THRESHOLD = float(getenv('ADVANCE_THRESHOLD', 0.6))

Loading…
Cancel
Save