Add view for creating join urls

main
Luca 2 years ago
parent 77e5dc1a12
commit 55137e5a35

@ -0,0 +1,14 @@
{% extends "core/base.html" %}
{% block title %}Teilnahmelink erstellen{% endblock %}
{% block body %}
<form action="" method="post">
{% csrf_token %}
<button type="submit">Teilnahmelink erstellen</button>
</form>
{% if token %}
<p>{{ token }}</p>
{% endif %}
{% endblock %}

@ -3,7 +3,9 @@ from django.urls import include, path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path('create_token/', views.create_token, name='create_token'),
path('join/<slug:session>/', views.join_session, name='join_session'), path('join/<slug:session>/', views.join_session, name='join_session'),
path('participate/<slug:token>/', views.participate, name='participate'),
path('<slug:playlist>/', include([ path('<slug:playlist>/', include([
path('', views.artist, name='current_artist'), path('', views.artist, name='current_artist'),
path('<int:offset>/', views.artist, name='artist'), path('<int:offset>/', views.artist, name='artist'),

@ -1,4 +1,6 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from .forms import RateArtistForm from .forms import RateArtistForm
from .models import Participant, Rating from .models import Participant, Rating
@ -6,15 +8,31 @@ from ..core.models import Session
# Create your views here. # 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})
def join_session(request, session): def join_session(request, session):
session = get_object_or_404(Session, pk=session) session = get_object_or_404(Session, pk=session)
if 'token' in request.session: if 'token' in request.session:
try: try:
participant = Participant.objects.get(pk=request.session['token']) participant = Participant.objects.get(pk=request.session['token'])
return redirect('current_artist', session.pk) if participant.session == session:
return redirect('current_artist', session.pk)
except Participant.DoesNotExist: except Participant.DoesNotExist:
del request.session['token'] pass
del request.session['token']
if request.method == 'POST': if request.method == 'POST':
participant = Participant(session=session) participant = Participant(session=session)
@ -25,6 +43,22 @@ def join_session(request, session):
return render(request, 'vote/join_session.html', {}) 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): def artist(request, playlist, offset=None):
participant = get_object_or_404(Participant, pk=request.session.get('token')) participant = get_object_or_404(Participant, pk=request.session.get('token'))

Loading…
Cancel
Save