Skip to content

Commit c7c4afc

Browse files
committed
It turns out that the get_user_email is called for every nominee listed on a feedback page, which is insane. But until the template can be rewritten to do this in a more sane way, we cache the lookup result.
- Legacy-Id: 6651
1 parent 8e8cfc0 commit c7c4afc

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

ietf/nomcom/utils.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
from django.conf import settings
1313
from django.contrib.sites.models import Site
14-
from django.core.exceptions import PermissionDenied
14+
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
1515
from django.core.urlresolvers import reverse
1616
from django.template.loader import render_to_string
1717
from django.shortcuts import get_object_or_404
1818
from django.utils.encoding import smart_str
19+
from django.core.validators import email_re
1920

2021
from ietf.dbtemplate.models import DBTemplate
2122
from ietf.person.models import Email, Person
@@ -24,6 +25,8 @@
2425
from ietf.utils.mail import send_mail_text, send_mail
2526
from ietf.utils.log import log
2627

28+
import debug
29+
2730
MAIN_NOMCOM_TEMPLATE_PATH = '/nomcom/defaults/'
2831
QUESTIONNAIRE_TEMPLATE = 'position/questionnaire.txt'
2932
HEADER_QUESTIONNAIRE_TEMPLATE = 'position/header_questionnaire.txt'
@@ -61,14 +64,25 @@ def get_year_by_nomcom(nomcom):
6164

6265

6366
def get_user_email(user):
64-
emails = user.person.email_set.filter(active=True).order_by('-time')
65-
if emails:
66-
for email in emails:
67-
if email.address == user.username:
68-
return email
69-
return emails[0]
70-
return None
71-
67+
# a user object already has an email field, but we don't want to
68+
# overwrite anything that might be there, and we don't know that
69+
# what's there is the right thing, so we cache the lookup results in a
70+
# separate attribute
71+
if not hasattr(user, "_email_cache"):
72+
user._email_cache = None
73+
if hasattr(user, "person"):
74+
emails = user.person.email_set.filter(active=True).order_by('-time')
75+
if emails:
76+
user._email_cache = emails[0]
77+
for email in emails:
78+
if email.address == user.username:
79+
user._email_cache = email
80+
else:
81+
try:
82+
user._email_cache = Email.objects.get(address=user.username)
83+
except ObjectDoesNotExist:
84+
pass
85+
return user._email_cache
7286

7387
def is_nomcom_member(user, nomcom):
7488
is_group_member = nomcom.group.is_member(user)

0 commit comments

Comments
 (0)