From 29111059f9d6e98d6bee770ca6c573be71fb1fed Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Sat, 8 Jun 2019 14:59:45 +0200 Subject: [PATCH] =?UTF-8?q?Rajoute=20un=20manager=20=C3=A0=20`Participant`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On rajoute un manager qui annote les querysets avec si le participant a payƩ ou non --- bda/models.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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)