From a4561f55f28f19c56e896e569c229f72b5965e0e Mon Sep 17 00:00:00 2001 From: Robert Sparks Date: Wed, 2 Dec 2015 04:31:35 +0000 Subject: [PATCH] improvement that makes only one counting query - Legacy-Id: 10526 --- ietf/nomcom/templatetags/nomcom_tags.py | 8 ++++++-- ietf/nomcom/views.py | 12 +++++++----- ietf/templates/nomcom/feedback.html | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ietf/nomcom/templatetags/nomcom_tags.py b/ietf/nomcom/templatetags/nomcom_tags.py index 8b52114ab..e3ba1dbfa 100644 --- a/ietf/nomcom/templatetags/nomcom_tags.py +++ b/ietf/nomcom/templatetags/nomcom_tags.py @@ -13,6 +13,8 @@ from ietf.person.models import Person from ietf.nomcom.models import Feedback from ietf.nomcom.utils import get_nomcom_by_year, get_user_email, retrieve_nomcom_private_key +import debug # pyflakes:ignore + register = template.Library() @@ -31,8 +33,10 @@ def has_publickey(nomcom): @register.simple_tag -def add_num_nominations(user_comments, position, nominee): - count = user_comments.filter(positions=position, nominees=nominee).count() +def add_num_nominations(counts, position, nominee): + count = 0 + if position.id in counts and nominee.id in counts[position.id]: + count = counts[position.id][nominee.id] if count: return '%s ' % (count , nominee.email.address, position, count) else: diff --git a/ietf/nomcom/views.py b/ietf/nomcom/views.py index f7c03c26b..b556020ab 100644 --- a/ietf/nomcom/views.py +++ b/ietf/nomcom/views.py @@ -1,6 +1,6 @@ import datetime import re -from collections import OrderedDict +from collections import OrderedDict, Counter from django.conf import settings from django.contrib.auth.decorators import login_required @@ -392,8 +392,10 @@ def feedback(request, year, public): user_comments = Feedback.objects.filter(nomcom=nomcom, type='comment', author__in=request.user.person.email_set.filter(active='True')) - - + counter = Counter(user_comments.values_list('positions','nominees')) + counts = dict() + for pos,nom in counter: + counts.setdefault(pos,dict())[nom] = counter[(pos,nom)] if public: base_template = "nomcom/nomcom_public_base.html" @@ -407,7 +409,7 @@ def feedback(request, year, public): 'nomcom': nomcom, 'year': year, 'selected': 'feedback', - 'user_comments' : user_comments, + 'counts' : counts, 'base_template': base_template }) @@ -434,7 +436,7 @@ def feedback(request, year, public): 'year': year, 'positions': positions, 'selected': 'feedback', - 'user_comments' : user_comments, + 'counts': counts, 'base_template': base_template }) diff --git a/ietf/templates/nomcom/feedback.html b/ietf/templates/nomcom/feedback.html index a13eaace0..d0dcd8cd9 100644 --- a/ietf/templates/nomcom/feedback.html +++ b/ietf/templates/nomcom/feedback.html @@ -44,7 +44,7 @@ {% for np in p.nomineeposition_set.accepted.not_duplicated %} {{ np.nominee.name }} - {% add_num_nominations user_comments np.position np.nominee %} + {% add_num_nominations counts np.position np.nominee %} {% endfor %}