forked from DGNum/gestioCOF
Use manager from queryset
This commit is contained in:
parent
edd92beadf
commit
4f15b820a5
3 changed files with 14 additions and 10 deletions
|
@ -117,7 +117,7 @@ class ParticipantAdmin(ReadOnlyMixin, admin.ModelAdmin):
|
|||
inlines = [WithListingAttributionInline, WithoutListingAttributionInline]
|
||||
|
||||
def get_queryset(self, request):
|
||||
return self.model.objects_paid.get_queryset().annotate(
|
||||
return self.model.objects.annotate_paid().annotate(
|
||||
nb_places=Count("attributions"),
|
||||
remain=Sum(
|
||||
"attribution__spectacle__price", filter=Q(attribution__paid=False)
|
||||
|
|
|
@ -170,17 +170,20 @@ class Attribution(models.Model):
|
|||
)
|
||||
|
||||
|
||||
class ParticipantPaidManager(models.Manager):
|
||||
class ParticipantPaidQueryset(models.QuerySet):
|
||||
"""
|
||||
Un manager qui annote le queryset avec un champ `paid`,
|
||||
indiquant si un participant a payé toutes ses attributions.
|
||||
"""
|
||||
|
||||
def get_queryset(self):
|
||||
# OuterRef permet de se référer à un champ d'un modèle non encore fixé
|
||||
# Voir:
|
||||
# https://docs.djangoproject.com/en/2.2/ref/models/expressions/#django.db.models.OuterRef
|
||||
def annotate_paid(self):
|
||||
unpaid = Attribution.objects.filter(
|
||||
participant=models.OuterRef("pk"), paid=False
|
||||
)
|
||||
return super().get_queryset().annotate(paid=~Exists(unpaid))
|
||||
return self.annotate(paid=~Exists(unpaid))
|
||||
|
||||
|
||||
class Participant(models.Model):
|
||||
|
@ -196,8 +199,7 @@ class Participant(models.Model):
|
|||
Spectacle, related_name="subscribed", blank=True
|
||||
)
|
||||
|
||||
objects = models.Manager()
|
||||
objects_paid = ParticipantPaidManager()
|
||||
objects = ParticipantPaidQueryset.as_manager()
|
||||
|
||||
def __str__(self):
|
||||
return "%s - %s" % (self.user, self.tirage.title)
|
||||
|
|
10
bda/views.py
10
bda/views.py
|
@ -378,7 +378,7 @@ def revente_manage(request, tirage_id):
|
|||
- Annulation d'une revente après que le tirage a eu lieu
|
||||
"""
|
||||
tirage = get_object_or_404(Tirage, id=tirage_id)
|
||||
participant, created = Participant.objects_paid.get_or_create(
|
||||
participant, created = Participant.annotate_paid().get_or_create(
|
||||
user=request.user, tirage=tirage
|
||||
)
|
||||
|
||||
|
@ -734,9 +734,11 @@ class UnpaidParticipants(BuroRequiredMixin, ListView):
|
|||
template_name = "bda-unpaid.html"
|
||||
|
||||
def get_queryset(self):
|
||||
return Participant.objects_paid.filter(
|
||||
tirage__id=self.kwargs["tirage_id"], paid=False
|
||||
).select_related("user")
|
||||
return (
|
||||
Participant.objects.annotate_paid()
|
||||
.filter(tirage__id=self.kwargs["tirage_id"], paid=False)
|
||||
.select_related("user")
|
||||
)
|
||||
|
||||
|
||||
@buro_required
|
||||
|
|
Loading…
Reference in a new issue