nomination quota
This commit is contained in:
@@ -3,6 +3,10 @@ from django.contrib.auth.models import User
|
||||
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
|
||||
|
||||
NOMINATIONS_PER_DAY = 10
|
||||
|
||||
class Background(models.Model):
|
||||
def __str__(self):
|
||||
@@ -17,6 +21,20 @@ class Profile(models.Model):
|
||||
background = models.ForeignKey(Background, null=True, on_delete=models.SET_NULL)
|
||||
last_voted = models.ForeignKey(Track, null=True, on_delete=models.SET_NULL)
|
||||
|
||||
quota = models.FloatField(default=10.0)
|
||||
quota_last_updated = models.DateTimeField(null=False)
|
||||
def update_quota(self):
|
||||
elapsed = timezone.now() - self.quota_last_updated
|
||||
if elapsed.seconds > 300:
|
||||
noms_per_second = NOMINATIONS_PER_DAY/(60*60*24)
|
||||
self.quota += noms_per_second * elapsed.seconds
|
||||
print(f"Er zijn {elapsed.seconds}s voorbij, je nieuwe quota is {self.quota}")
|
||||
self.quota_last_updated = timezone.now()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.update_quota()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def create_user_profile(sender, instance, created, **kwargs):
|
||||
if created:
|
||||
|
||||
@@ -70,7 +70,14 @@ input#spotify-input + input[type=submit]{
|
||||
z-index: 100;
|
||||
top: calc(50% - 75px);
|
||||
left: calc(50% - 150px);
|
||||
min-width: 300px;
|
||||
width: 300px;
|
||||
}
|
||||
|
||||
.window.popup .title-bar-text {
|
||||
height: 13px;
|
||||
overflow:hidden;
|
||||
text-overflow:ellipsis;
|
||||
white-space:nowrap;
|
||||
}
|
||||
|
||||
.window.popup.login {
|
||||
@@ -314,3 +321,15 @@ iframe {
|
||||
padding: 32px 0 4px 0;
|
||||
height: 50px;
|
||||
}
|
||||
|
||||
.progress-indicator {
|
||||
height: 26px;
|
||||
padding: 4px 3px;
|
||||
width: 360px;
|
||||
}
|
||||
|
||||
.progress-indicator.segmented > .progress-indicator-bar {
|
||||
}
|
||||
|
||||
.status-bar-field {
|
||||
}
|
||||
@@ -38,6 +38,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% block status-bar %}{% endblock%}
|
||||
</div>
|
||||
{% block clippy %}{% endblock %}
|
||||
<script>
|
||||
|
||||
@@ -8,9 +8,8 @@ Nomineren
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<p>Vul een spotify link in om een nummer te nomineren.</p>
|
||||
<p>Deze link mag zowel naar open.spotify.com gaan als naar spotify.link</p>
|
||||
<input type="text" id="spotify-input" name="spotify_link" placeholder="Spotify-link">
|
||||
<input type="submit" value="Insturen">
|
||||
<input type="submit" value="Insturen" {% if user.profile.quota < 1%}disabled{%endif%}>
|
||||
</form>
|
||||
{% if nominated or error or warning %}
|
||||
<script>
|
||||
@@ -49,3 +48,15 @@ Nomineren
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
{% block status-bar%}
|
||||
<div class="status-bar">
|
||||
<div
|
||||
class="status-bar-field progress-indicator segmented"
|
||||
title="{{user.profile.quota | floatformat:2}} nominaties over."
|
||||
>
|
||||
<span class="progress-indicator-bar" style="width: calc({{user.profile.quota | floatformat:0}} * 18px);" />
|
||||
</div>
|
||||
<p class="status-bar-field">{{user.profile.quota|floatformat:0}} nominaties over.</p>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
@@ -47,8 +47,13 @@ class VoteView(LoginRequiredMixin, View):
|
||||
|
||||
class NominateView(LoginRequiredMixin, View):
|
||||
def get(self, request):
|
||||
(profile,_) = Profile.objects.get_or_create(user=request.user)
|
||||
profile.update_quota()
|
||||
return TemplateResponse(request, "Nominate.html", {})
|
||||
def post(self, request):
|
||||
(profile,_) = Profile.objects.get_or_create(user=request.user)
|
||||
if profile.quota < 1:
|
||||
return TemplateResponse(request, "Nominate.html", {"error": "Nee nee nee, dat gaan we dus even niet doen."})
|
||||
try:
|
||||
spotify_link = self.request.POST['spotify_link']
|
||||
|
||||
@@ -115,6 +120,8 @@ class NominateView(LoginRequiredMixin, View):
|
||||
track.album = album
|
||||
track.save()
|
||||
get_banter.delay(track.id)
|
||||
profile.quota -= 1
|
||||
profile.save()
|
||||
|
||||
return TemplateResponse(request, "Nominate.html", {"nominated": track})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user