nomination quota

This commit is contained in:
2024-02-26 22:51:35 +01:00
parent 2861c856af
commit 4df913c7fe
5 changed files with 59 additions and 3 deletions
+18
View File
@@ -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: