Implement break view

main
Luca 2 years ago
parent 74cdcd9077
commit aca17d8c98

@ -0,0 +1,22 @@
# Generated by Django 4.1.4 on 2022-12-20 23:12
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
("core", "0004_alter_artist_link_1_alter_artist_link_2"),
]
operations = [
migrations.AddField(
model_name="session",
name="last_break",
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
]

@ -22,4 +22,5 @@ def session_token():
class Session(models.Model):
token = models.CharField(max_length=43, default=session_token, primary_key=True)
offset = models.PositiveIntegerField(default=0)
last_break = models.DateTimeField(auto_now_add=True)
playlist = models.ForeignKey(Playlist, on_delete=models.CASCADE)

@ -3,6 +3,16 @@
src: local("Maven Pro"), url(./MavenPro-VariableFont:wght.ttf);
}
@keyframes rainbow {
0% { color: #ff0000; }
16.66% { color: #ff8000; }
33.33% { color: #ffff00; }
50% { color: #00ff00; }
66.66% { color: #0000ff; }
83.33% { color: #8000ff; }
100% { color: #ff0000; }
}
* {
box-sizing: border-box;
margin: 0;
@ -74,6 +84,11 @@ svg {
text-align: right;
}
.large-text {
font-size: 5em;
margin-bottom: 0.2em;
}
.links {
display: flex;
flex-wrap: wrap;
@ -137,6 +152,10 @@ svg {
text-align: right;
}
.rainbow {
animation: rainbow 3s linear 0s infinite;
}
.rating-container {
display: flex;
flex-direction: column;

@ -2,3 +2,6 @@ from django import forms
class CreatePlaylistForm(forms.Form):
spreadsheet_url = forms.URLField(label='Link zur Tabelle')
class AddBreakContentForm(forms.Form):
video_url = forms.URLField(label='YouTube-Link')

@ -0,0 +1,23 @@
# Generated by Django 4.1.4 on 2022-12-20 23:44
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="BreakContent",
fields=[
(
"video_id",
models.CharField(max_length=11, primary_key=True, serialize=False),
),
("use_count", models.PositiveIntegerField()),
],
),
]

@ -1,3 +1,7 @@
from django.db import models
# Create your models here.
class BreakContent(models.Model):
video_id = models.CharField(max_length=11, primary_key=True)
use_count = models.PositiveIntegerField()

@ -0,0 +1,20 @@
{% extends "core/base.html" %}
{% block title %}Pauseninhalt hinzufügen{% endblock %}
{% block body %}
<h1>Pauseninhalt hinzufügen</h1>
<form action="" method="post">
{% csrf_token %}
<div class="form">
{% for field in form %}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% endfor %}
<label for="submit_add_break_content"></label>
<button id="submit_add_break_content" type="submit">Hinzufügen</button>
</div>
</form>
{% endblock %}

@ -0,0 +1,17 @@
{% extends "core/base.html" %}
{% load youtube %}
{% block title %}Pause{% endblock %}
{% block body_class %}center{% endblock %}
{% block body %}
<h1 class="large-text rainbow">PAUSE</h1>
{% youtube video_id %}
<form action="" method="post">
{% csrf_token %}
<button type="submit">Fortfahren</button>
</form>
{% endblock %}

@ -4,6 +4,10 @@ from . import views
urlpatterns = [
path('', views.create_playlist, name='create_playlist'),
path('break/', include([
path('', views.break_view, name='break'),
path('add/', views.add_break_content, name='add_break_content'),
])),
path('session/', views.session, name='session'),
path('<slug:playlist>/', include([
path('', views.playlist, name='playlist'),

@ -1,9 +1,12 @@
from django.conf import settings
from django.db.models import F, Min
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.utils import timezone
from re import match
from .forms import CreatePlaylistForm
from .forms import AddBreakContentForm, CreatePlaylistForm
from .models import BreakContent
from .spreadsheet import get_sheet_data
from ..core.models import Artist, Playlist, Session
@ -81,6 +84,41 @@ def create_playlist(request):
return render(request, 'host/create_playlist.html', {'error': None, 'form': form})
def break_view(request):
if request.method == 'POST':
session = get_object_or_404(Session, pk=request.session.get('session'))
session.last_break = timezone.now()
session.save()
return redirect('view_artist', session.playlist.pk, session.offset)
min_use_count = BreakContent.objects.aggregate(Min('use_count'))['use_count__min']
content = BreakContent.objects.filter(use_count=min_use_count).order_by('?')[0]
content.use_count = F('use_count') + 1
content.save()
return render(request, 'host/break.html', {'video_id': content.video_id})
def add_break_content(request):
form = AddBreakContentForm()
if request.method == 'POST':
form = AddBreakContentForm(request.POST)
if not form.is_valid():
return redirect('add_break_content')
m = match(settings.YOUTUBE_RE, form.cleaned_data['video_url'])
if m == None:
return redirect('add_break_content')
min_use_count = BreakContent.objects.aggregate(Min('use_count'))['use_count__min']
content = BreakContent(video_id=m[1], use_count=min_use_count or 0)
content.save()
return redirect('add_break_content')
return render(request, 'host/add_break_content.html', {'form': form})
def session(request):
if 'session' in request.session:
try:
@ -124,6 +162,9 @@ def view_artist(request, playlist, offset):
if session.offset < offset:
session.offset = offset
session.save()
if (timezone.now()-session.last_break).total_seconds() >= settings.BREAK_INTERVAL*60:
return redirect('break')
except Session.DoesNotExist:
del request.session['session']

@ -160,3 +160,5 @@ RATING_CHOICES = (
( '2', 'ziemlich schlecht'),
( '1', 'sehr schlecht'),
)
BREAK_INTERVAL = int(getenv('BREAK_INTERVAL', 30)) # minutes

@ -51,16 +51,16 @@ class Migration(migrations.Migration):
models.CharField(
blank=True,
choices=[
("1", "sehr schlecht"),
("2", "ziemlich schlecht"),
("3", "schlecht"),
("4", "eher schlecht"),
("5", "naja"),
("6", "ok"),
("7", "eher gut"),
("8", "gut"),
("9", "ziemlich gut"),
("10", "sehr gut"),
("9", "ziemlich gut"),
("8", "gut"),
("7", "eher gut"),
("6", "ok"),
("5", "naja"),
("4", "eher schlecht"),
("3", "schlecht"),
("2", "ziemlich schlecht"),
("1", "sehr schlecht"),
],
max_length=2,
),

Loading…
Cancel
Save