From 5858cee9003a76074bee85ee2f1ff5736920ddce Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Fri, 19 Jul 2024 17:30:36 +0200 Subject: [PATCH] deadlines --- playlist/admin.py | 5 +- playlist/models/profile.py | 39 ++++++++++- playlist/static/js/countdown.js | 37 +++++++++++ playlist/templates/Nominate.html | 25 ++++++- playlist/templates/Overview.html | 4 +- playlist/templates/Vote.html | 109 +++++++++++++++++++------------ playlist/templates/head.html | 1 + playlist/views.py | 12 +++- 8 files changed, 182 insertions(+), 50 deletions(-) create mode 100644 playlist/static/js/countdown.js diff --git a/playlist/admin.py b/playlist/admin.py index 58b2bbe..d3c7142 100644 --- a/playlist/admin.py +++ b/playlist/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from .models import Track, Artist, Album, Background, Vote +from .models import Track, Artist, Album, Background, Vote, Profile class TrackAdmin(admin.ModelAdmin): pass @@ -11,9 +11,12 @@ class BackgroundAdmin(admin.ModelAdmin): pass class VoteAdmin(admin.ModelAdmin): pass +class ProfileAdmin(admin.ModelAdmin): + pass admin.site.register(Track, TrackAdmin) admin.site.register(Artist, ArtistAdmin) admin.site.register(Album, AlbumAdmin) admin.site.register(Background, BackgroundAdmin) admin.site.register(Vote, VoteAdmin) +admin.site.register(Profile, ProfileAdmin) diff --git a/playlist/models/profile.py b/playlist/models/profile.py index ed701c5..20e5f26 100644 --- a/playlist/models/profile.py +++ b/playlist/models/profile.py @@ -4,8 +4,12 @@ from .track import Track from django.db.models.signals import post_save from django.dispatch import receiver from django.utils import timezone -from datetime import datetime, timedelta +from datetime import datetime, timedelta, UTC from ..spotify import spt +import os +from dotenv import load_dotenv + +load_dotenv() NOMINATIONS_PER_DAY = 10 MAX_QUOTA = 22 @@ -42,6 +46,39 @@ class Profile(models.Model): self.quota = min(self.quota, MAX_QUOTA) print(f"Er zijn {elapsed.seconds}s voorbij, je nieuwe quota is {self.quota}") self.quota_last_updated = timezone.now() + + @property + def can_nominate(self): + """Is this user allowed to nominate at this point in time?""" + end_date = os.getenv('DATE_NOM_END') + if self.user.is_superuser: + return (True,) + if end_date: + end_date = datetime.fromisoformat(end_date) + if end_date <= datetime.now(tz=UTC): + return (False, "over") + if self.quota < 1: + return (False, "quota") + return (True,) + + @property + def can_vote(self): + """Is this user allowed to vote at this point in time?""" + end_date = os.getenv('DATE_VOTE_END') + if self.user.is_superuser or not end_date: + return True + end_date = datetime.fromisoformat(end_date) + if end_date <= datetime.now(tz=UTC): + return False + return True + + @property + def vote_end(self): + return os.getenv('DATE_VOTE_END') + + @property + def nominate_end(self): + return os.getenv('DATE_NOM_END') def init_spt(self, code): """Initial setting of spotify tokens from user accept code""" diff --git a/playlist/static/js/countdown.js b/playlist/static/js/countdown.js new file mode 100644 index 0000000..0ba104d --- /dev/null +++ b/playlist/static/js/countdown.js @@ -0,0 +1,37 @@ +let timer = null; +function startCountdown(dateString) { + if (timer) clearInterval(timer); + let cd = document.getElementById("countdown") + if (cd) { + cd.classList.remove("hide"); + if (dateString == "None") cd.classList.add("hide"); + } + const end = new Date(dateString).getTime(); + const dayEl = document.getElementById('days'); + const hoursEl = document.getElementById('hours'); + const minutesEl = document.getElementById('minutes'); + const secondsEl = document.getElementById('seconds'); + const seconds = 1000; + const minutes = seconds * 60; + const hours = minutes * 60; + const days = hours * 24; + timer = setInterval(updateTime, seconds); + function updateTime() { + if (dayEl) { + let now = new Date().getTime(); + const difference = end - now; + + if (difference < 0) { + if (timer) clearInterval(timer); + cd.classList.add("hide"); + return; + } + + dayEl.innerText = Math.floor(difference / days); + hoursEl.innerText = Math.floor( (difference % days) / hours ); + minutesEl.innerText = Math.floor( (difference % hours) / minutes ); + secondsEl.innerText = Math.floor( (difference % minutes) / seconds ); + } + } + updateTime(); +} \ No newline at end of file diff --git a/playlist/templates/Nominate.html b/playlist/templates/Nominate.html index d83c4f8..159552f 100644 --- a/playlist/templates/Nominate.html +++ b/playlist/templates/Nominate.html @@ -7,10 +7,17 @@ Nomineren {% block main-content %}
{% csrf_token %} -

Vul een spotify link in om een nummer te nomineren.

- - + {% if user.profile.can_nominate.0 %} +

Vul een spotify link in om een nummer te nomineren.

+ + + {% elif user.profile.can_nominate.1 == 'quota' %} +

Je quotum is op. Je zal helaas eventjes moeten wachten met nomineren.

+ {% elif user.profile.can_nominate.1 == 'over' %} +

Het nomineren is voorbij voor dit jaar! Bedankt voor de inzendingen.

+ {% endif %}
+ {% endblock %} {% block status-bar%} @@ -23,4 +30,16 @@ Nomineren

{{user.profile.quota_display}} nominaties over.

+
+

dagen

+

uur

+

minuten

+

seconden

+
+ {% endblock %} diff --git a/playlist/templates/Overview.html b/playlist/templates/Overview.html index ccdfdd0..4260b6d 100644 --- a/playlist/templates/Overview.html +++ b/playlist/templates/Overview.html @@ -5,14 +5,14 @@ Overzicht {% endblock %} {% block multi-row %} - + {% comment %}
  • Tracks
  • Playlists
  • -
    +
    {% endcomment %} {% endblock %} {% block main-content %} diff --git a/playlist/templates/Vote.html b/playlist/templates/Vote.html index dfceba4..adb2646 100644 --- a/playlist/templates/Vote.html +++ b/playlist/templates/Vote.html @@ -5,59 +5,86 @@ Stemmen {% endblock %} {% block main-content %} -{% if track %} -
    - {% if error %}
    {{ error }}
    {% endif %} - {% include 'MediaPlayer.html' with track=track %} +{% if user.profile.can_vote %} + {% if track %} +
    + {% if error %}
    {{ error }}
    {% endif %} + {% include 'MediaPlayer.html' with track=track %} -
    -
    - Hoeveel sterren geef jij dit? -
    - - - - - -
    -
    +
    +
    + Hoeveel sterren geef jij dit? +
    + + + + + +
    +
    + {% if undo %} + +
    + {% csrf_token %} +

    Je hebt {{undo.track}} een score van {{undo.points}} sterren gegeven!

    + +
    + {% endif %} +
    +
    + {% else %} +

    + Je hebt niets meer om te stemmen. Lekker bezig! + Nomineer zelf iets, als je nog quota hebt. +

    {% if undo %} -
    +
    {% csrf_token %}

    Je hebt {{undo.track}} een score van {{undo.points}} sterren gegeven!

    {% endif %} -
    -
    + {% endif %} {% else %} -

    - Je hebt niets meer om te stemmen. Lekker bezig! - Nomineer zelf iets, als je nog quota hebt. -

    +

    Stemmen is voorbij. Joe.

    {% endif %} {% endblock %} {% block clippy %} {% include "Clippy.html" with body=track.banter img="img/draakje/draakje-headphones3.gif" %} +{% endblock %} + +{% block status-bar%} +
    +

    dagen

    +

    uur

    +

    minuten

    +

    seconden

    +
    + {% endblock %} \ No newline at end of file diff --git a/playlist/templates/head.html b/playlist/templates/head.html index 87709ab..e3a52c6 100644 --- a/playlist/templates/head.html +++ b/playlist/templates/head.html @@ -2,6 +2,7 @@ +