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]
|
inlines = [WithListingAttributionInline, WithoutListingAttributionInline]
|
||||||
|
|
||||||
def get_queryset(self, request):
|
def get_queryset(self, request):
|
||||||
return self.model.objects_paid.get_queryset().annotate(
|
return self.model.objects.annotate_paid().annotate(
|
||||||
nb_places=Count("attributions"),
|
nb_places=Count("attributions"),
|
||||||
remain=Sum(
|
remain=Sum(
|
||||||
"attribution__spectacle__price", filter=Q(attribution__paid=False)
|
"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`,
|
Un manager qui annote le queryset avec un champ `paid`,
|
||||||
indiquant si un participant a payé toutes ses attributions.
|
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(
|
unpaid = Attribution.objects.filter(
|
||||||
participant=models.OuterRef("pk"), paid=False
|
participant=models.OuterRef("pk"), paid=False
|
||||||
)
|
)
|
||||||
return super().get_queryset().annotate(paid=~Exists(unpaid))
|
return self.annotate(paid=~Exists(unpaid))
|
||||||
|
|
||||||
|
|
||||||
class Participant(models.Model):
|
class Participant(models.Model):
|
||||||
|
@ -196,8 +199,7 @@ class Participant(models.Model):
|
||||||
Spectacle, related_name="subscribed", blank=True
|
Spectacle, related_name="subscribed", blank=True
|
||||||
)
|
)
|
||||||
|
|
||||||
objects = models.Manager()
|
objects = ParticipantPaidQueryset.as_manager()
|
||||||
objects_paid = ParticipantPaidManager()
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s - %s" % (self.user, self.tirage.title)
|
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
|
- Annulation d'une revente après que le tirage a eu lieu
|
||||||
"""
|
"""
|
||||||
tirage = get_object_or_404(Tirage, id=tirage_id)
|
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
|
user=request.user, tirage=tirage
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -734,9 +734,11 @@ class UnpaidParticipants(BuroRequiredMixin, ListView):
|
||||||
template_name = "bda-unpaid.html"
|
template_name = "bda-unpaid.html"
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Participant.objects_paid.filter(
|
return (
|
||||||
tirage__id=self.kwargs["tirage_id"], paid=False
|
Participant.objects.annotate_paid()
|
||||||
).select_related("user")
|
.filter(tirage__id=self.kwargs["tirage_id"], paid=False)
|
||||||
|
.select_related("user")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@buro_required
|
@buro_required
|
||||||
|
|
Loading…
Reference in a new issue