From 2a4c5b1f22303b1e07f82f4ae584e97a207f4e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Bouttier?= Date: Wed, 2 Aug 2017 21:07:36 +0200 Subject: [PATCH] optimizing database requests --- cfp/models.py | 13 +++++++++++++ cfp/templates/cfp/staff/participant_list.html | 6 +++--- cfp/views.py | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/cfp/models.py b/cfp/models.py index 1478e94..38c992a 100644 --- a/cfp/models.py +++ b/cfp/models.py @@ -63,6 +63,17 @@ class Conference(models.Model): return str(self.site) +class ParticipantManager(models.Manager): + def get_queryset(self): + qs = super().get_queryset() + qs = qs.annotate( + accepted_talk_count=models.Sum(models.Case(models.When(talk__accepted=True, then=1), default=0, output_field=models.IntegerField())), + pending_talk_count=models.Sum(models.Case(models.When(talk__accepted=None, then=1), default=0, output_field=models.IntegerField())), + refused_talk_count=models.Sum(models.Case(models.When(talk__accepted=False, then=1), default=0, output_field=models.IntegerField())), + ) + return qs + + class Participant(PonyConfModel): site = models.ForeignKey(Site, on_delete=models.CASCADE) @@ -90,6 +101,8 @@ class Participant(PonyConfModel): conversation = models.OneToOneField(MessageThread) + objects = ParticipantManager() + class Meta: # A User can participe only once to a Conference (= Site) unique_together = ('site', 'email') diff --git a/cfp/templates/cfp/staff/participant_list.html b/cfp/templates/cfp/staff/participant_list.html index e76ec35..ea90495 100644 --- a/cfp/templates/cfp/staff/participant_list.html +++ b/cfp/templates/cfp/staff/participant_list.html @@ -65,11 +65,11 @@ {{ participant }} - {% blocktrans count accepted=participant.accepted_talk_set.count %}accepted: {{ accepted }}{% plural %}accepted: {{ accepted }}{% endblocktrans %} + {% blocktrans count accepted=participant.accepted_talk_count %}accepted: {{ accepted }}{% plural %}accepted: {{ accepted }}{% endblocktrans %} — - {% blocktrans count pending=participant.pending_talk_set.count %}pending: {{ pending }}{% plural %}pending: {{ pending }}{% endblocktrans %} + {% blocktrans count pending=participant.pending_talk_count %}pending: {{ pending }}{% plural %}pending: {{ pending }}{% endblocktrans %} — - {% blocktrans count refused=participant.refused_talk_set.count %}refused: {{ refused }}{% plural %}refused: {{ refused }}{% endblocktrans %} + {% blocktrans count refused=participant.refused_talk_count %}refused: {{ refused }}{% plural %}refused: {{ refused }}{% endblocktrans %} {% comment %} diff --git a/cfp/views.py b/cfp/views.py index e28baf3..0f9b76f 100644 --- a/cfp/views.py +++ b/cfp/views.py @@ -195,7 +195,7 @@ def talk_list(request, conference): glyphicon = 'sort' sort_urls[c] = url.urlencode() sort_glyphicons[c] = glyphicon - talks = talks.prefetch_related('speakers') + talks = talks.prefetch_related('category', 'speakers') return render(request, 'cfp/staff/talk_list.html', { 'show_filters': show_filters, 'talk_list': talks,