Skip to content

Commit d80a72d

Browse files
committed
Fallback on latest email address if we don't know anything else,
modify email importer to set the timestamp accordingly with high priority email addresses first (to prevent old, inactive email addresses from taking precedence) - Legacy-Id: 3776
1 parent 4a0ee86 commit d80a72d

3 files changed

Lines changed: 65 additions & 20 deletions

File tree

redesign/importing/import-persons.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@
143143
print "importing LiaisonManagers person", o.pk, o.person.first_name.encode('utf-8'), o.person.last_name.encode('utf-8')
144144

145145
email = get_or_create_email(o, create_fake=False)
146-
possibly_import_other_priority_email(email, o.person.email(priority=o.email_priority)[1])
146+
addresses = o.person.emailaddress_set.filter(priority=o.email_priority).filter(address__contains="@")[:1]
147+
if addresses:
148+
possibly_import_other_priority_email(email, addresses[0])
147149

148150
# SDOAuthorizedIndividual persons
149151
for o in PersonOrOrgInfo.objects.filter(sdoauthorizedindividual__pk__gte=1).order_by("pk").distinct():
@@ -160,8 +162,10 @@
160162
# we may also need to import email address used specifically for
161163
# the document
162164
if "@" in email.address:
163-
addr = o.from_email().address
164-
possibly_import_other_priority_email(email, addr)
165+
try:
166+
possibly_import_other_priority_email(email, o.from_email())
167+
except EmailAddress.DoesNotExist:
168+
pass
165169

166170
# WgProceedingsActivities persons
167171
for o in PersonOrOrgInfo.objects.filter(wgproceedingsactivities__id__gte=1).order_by("pk").distinct():
@@ -176,4 +180,7 @@
176180

177181
# we may also need to import email address used specifically for
178182
# the document
179-
possibly_import_other_priority_email(email, o.email())
183+
184+
addresses = o.person.emailaddress_set.filter(type='I-D', priority=o.document_id).filter(address__contains="@")[:1]
185+
if addresses:
186+
possibly_import_other_priority_email(email, addresses[0])

redesign/importing/utils.py

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from ietf.utils import unaccent
22
from redesign.person.models import Person, Email, Alias
3+
from ietf.idtracker.models import EmailAddress
4+
import datetime
35

46
def clean_email_address(addr):
57
addr = addr.replace("!", "@").replace("(at)", "@") # some obvious @ replacements
@@ -53,20 +55,52 @@ def old_person_to_person(person):
5355
return Person.objects.get(alias__name=person_name(person))
5456

5557
def old_person_to_email(person):
58+
# try connected addresses
59+
addresses = person.emailaddress_set.filter(address__contains="@").order_by('priority')[:1]
60+
if addresses:
61+
addr = clean_email_address(addresses[0].address)
62+
priority = addresses[0].priority
63+
return (addr, priority)
64+
65+
# try to see if there's a person with the same name and an email address
66+
addresses = EmailAddress.objects.filter(person_or_org__first_name=person.first_name, person_or_org__last_name=person.last_name).filter(address__contains="@").order_by('priority')[:1]
67+
if addresses:
68+
addr = clean_email_address(addresses[0].address)
69+
priority = addresses[0].priority
70+
return (addr, priority)
71+
72+
# otherwise try the short list
5673
hardcoded_emails = {
5774
"Dinara Suleymanova": "dinaras@ietf.org",
5875
"Dow Street": "dow.street@linquest.com",
76+
"Xiaoya Yang": "xiaoya.yang@itu.int",
5977
}
60-
61-
return clean_email_address(person.email()[1] or hardcoded_emails.get(u"%s %s" % (person.first_name, person.last_name)) or "")
78+
79+
addr = hardcoded_emails.get(u"%s %s" % (person.first_name, person.last_name), "")
80+
priority = 1
81+
return (addr, priority)
82+
83+
84+
85+
def calc_email_import_time(priority):
86+
# we may import some old email addresses that are now
87+
# inactive, to ensure everything is not completely borked, we
88+
# want to ensure that high-priority (< 100) email addresses
89+
# end up later (in reverse of priority - I-D addresses follow
90+
# the normal ordering, since higher I-D id usually means later)
91+
if priority < 100:
92+
d = -priority
93+
else:
94+
d = priority - 36000
95+
return datetime.datetime(1970, 1, 2, 0, 0, 0) + datetime.timedelta(seconds=d)
6296

6397
def get_or_create_email(o, create_fake):
6498
# take o.person (or o) and get or create new Email and Person objects
6599
person = o.person if hasattr(o, "person") else o
66100

67101
name = person_name(person)
68102

69-
email = old_person_to_email(person)
103+
email, priority = old_person_to_email(person)
70104
if not email:
71105
if create_fake:
72106
email = u"unknown-email-%s" % name.replace(" ", "-")
@@ -97,6 +131,7 @@ def get_or_create_email(o, create_fake):
97131
Alias.objects.create(name=p.ascii, person=p)
98132

99133
e.person = p
134+
e.time = calc_email_import_time(priority)
100135
e.save()
101136
else:
102137
if e.person.name != name:
@@ -109,16 +144,19 @@ def get_or_create_email(o, create_fake):
109144

110145
return e
111146

112-
def possibly_import_other_priority_email(email, addr):
113-
addr = clean_email_address(addr or "")
114-
if addr and addr.lower() != email.address.lower():
115-
try:
116-
e = Email.objects.get(address=addr)
117-
if e.person != email.person:
118-
e.person = email.person
119-
e.save()
120-
except Email.DoesNotExist:
121-
Email.objects.create(address=addr, person=email.person)
147+
def possibly_import_other_priority_email(email, old_email):
148+
addr = clean_email_address(old_email.address or "")
149+
if not addr or addr.lower() == email.address.lower():
150+
return
151+
152+
try:
153+
e = Email.objects.get(address=addr)
154+
if e.person != email.person:
155+
e.person = email.person
156+
e.save()
157+
except Email.DoesNotExist:
158+
Email.objects.create(address=addr, person=email.person,
159+
time=calc_email_import_time(old_email.priority))
122160

123161
def dont_save_queries():
124162
# prevent memory from leaking when settings.DEBUG=True

redesign/person/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ def role_email(self, role_name, group=None):
4040
if e:
4141
return e[0]
4242
# no cigar, try the complete set before giving up
43-
e = self.email_set.order_by("-active")
43+
e = self.email_set.order_by("-active", "-time")
4444
if e:
4545
return e[0]
4646
return None
4747
def email_address(self):
48-
e = self.email_set.filter(active=True)
48+
e = self.email_set.filter(active=True).order_by("-time")
4949
if e:
5050
return e[0].address
5151
else:
5252
return ""
5353
def formatted_email(self):
54-
e = self.email_set.order_by("-active")
54+
e = self.email_set.order_by("-active", "-time")
5555
if e:
5656
return e[0].formatted_email()
5757
else:

0 commit comments

Comments
 (0)