diff --git a/bda/models.py b/bda/models.py index 05c2448a..de87731e 100644 --- a/bda/models.py +++ b/bda/models.py @@ -9,7 +9,7 @@ from django.contrib.auth.models import User from django.contrib.sites.models import Site from django.core import mail from django.db import models -from django.db.models import Count +from django.db.models import Count, Exists from django.utils import formats, timezone @@ -170,6 +170,19 @@ class Attribution(models.Model): ) +class ParticipantPaidManager(models.Manager): + """ + Un manager qui annote le queryset avec un champ `paid`, + indiquant si un participant a payƩ toutes ses attributions. + """ + + def get_queryset(self): + unpaid = Attribution.objects.filter( + participant=models.OuterRef("pk"), paid=False + ) + return super().get_queryset().annotate(paid=~Exists(unpaid)) + + class Participant(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) choices = models.ManyToManyField( @@ -183,6 +196,9 @@ class Participant(models.Model): Spectacle, related_name="subscribed", blank=True ) + objects = models.Manager() + objects_paid = ParticipantPaidManager() + def __str__(self): return "%s - %s" % (self.user, self.tirage.title)