Rajoute un manager à Participant

On rajoute un manager qui annote les querysets avec si le participant a payé ou non
This commit is contained in:
Ludovic Stephan 2019-06-08 14:59:45 +02:00 committed by Ludovic Stephan
parent 9776a18e4c
commit 29111059f9

View file

@ -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)