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
This commit is contained in:
Ole Laursen 2011-12-21 12:49:45 +00:00
parent 4a0ee86261
commit d80a72dff1
3 changed files with 65 additions and 20 deletions

View file

@ -143,7 +143,9 @@ for o in LiaisonManagers.objects.order_by("pk"):
print "importing LiaisonManagers person", o.pk, o.person.first_name.encode('utf-8'), o.person.last_name.encode('utf-8')
email = get_or_create_email(o, create_fake=False)
possibly_import_other_priority_email(email, o.person.email(priority=o.email_priority)[1])
addresses = o.person.emailaddress_set.filter(priority=o.email_priority).filter(address__contains="@")[:1]
if addresses:
possibly_import_other_priority_email(email, addresses[0])
# SDOAuthorizedIndividual persons
for o in PersonOrOrgInfo.objects.filter(sdoauthorizedindividual__pk__gte=1).order_by("pk").distinct():
@ -160,8 +162,10 @@ for o in LiaisonDetail.objects.exclude(person=None).order_by("pk"):
# we may also need to import email address used specifically for
# the document
if "@" in email.address:
addr = o.from_email().address
possibly_import_other_priority_email(email, addr)
try:
possibly_import_other_priority_email(email, o.from_email())
except EmailAddress.DoesNotExist:
pass
# WgProceedingsActivities persons
for o in PersonOrOrgInfo.objects.filter(wgproceedingsactivities__id__gte=1).order_by("pk").distinct():
@ -176,4 +180,7 @@ for o in IDAuthor.objects.all().order_by('id').select_related('person').iterator
# we may also need to import email address used specifically for
# the document
possibly_import_other_priority_email(email, o.email())
addresses = o.person.emailaddress_set.filter(type='I-D', priority=o.document_id).filter(address__contains="@")[:1]
if addresses:
possibly_import_other_priority_email(email, addresses[0])

View file

@ -1,5 +1,7 @@
from ietf.utils import unaccent
from redesign.person.models import Person, Email, Alias
from ietf.idtracker.models import EmailAddress
import datetime
def clean_email_address(addr):
addr = addr.replace("!", "@").replace("(at)", "@") # some obvious @ replacements
@ -53,12 +55,44 @@ def old_person_to_person(person):
return Person.objects.get(alias__name=person_name(person))
def old_person_to_email(person):
# try connected addresses
addresses = person.emailaddress_set.filter(address__contains="@").order_by('priority')[:1]
if addresses:
addr = clean_email_address(addresses[0].address)
priority = addresses[0].priority
return (addr, priority)
# try to see if there's a person with the same name and an email address
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]
if addresses:
addr = clean_email_address(addresses[0].address)
priority = addresses[0].priority
return (addr, priority)
# otherwise try the short list
hardcoded_emails = {
"Dinara Suleymanova": "dinaras@ietf.org",
"Dow Street": "dow.street@linquest.com",
"Xiaoya Yang": "xiaoya.yang@itu.int",
}
return clean_email_address(person.email()[1] or hardcoded_emails.get(u"%s %s" % (person.first_name, person.last_name)) or "")
addr = hardcoded_emails.get(u"%s %s" % (person.first_name, person.last_name), "")
priority = 1
return (addr, priority)
def calc_email_import_time(priority):
# we may import some old email addresses that are now
# inactive, to ensure everything is not completely borked, we
# want to ensure that high-priority (< 100) email addresses
# end up later (in reverse of priority - I-D addresses follow
# the normal ordering, since higher I-D id usually means later)
if priority < 100:
d = -priority
else:
d = priority - 36000
return datetime.datetime(1970, 1, 2, 0, 0, 0) + datetime.timedelta(seconds=d)
def get_or_create_email(o, create_fake):
# take o.person (or o) and get or create new Email and Person objects
@ -66,7 +100,7 @@ def get_or_create_email(o, create_fake):
name = person_name(person)
email = old_person_to_email(person)
email, priority = old_person_to_email(person)
if not email:
if create_fake:
email = u"unknown-email-%s" % name.replace(" ", "-")
@ -97,6 +131,7 @@ def get_or_create_email(o, create_fake):
Alias.objects.create(name=p.ascii, person=p)
e.person = p
e.time = calc_email_import_time(priority)
e.save()
else:
if e.person.name != name:
@ -109,16 +144,19 @@ def get_or_create_email(o, create_fake):
return e
def possibly_import_other_priority_email(email, addr):
addr = clean_email_address(addr or "")
if addr and addr.lower() != email.address.lower():
try:
e = Email.objects.get(address=addr)
if e.person != email.person:
e.person = email.person
e.save()
except Email.DoesNotExist:
Email.objects.create(address=addr, person=email.person)
def possibly_import_other_priority_email(email, old_email):
addr = clean_email_address(old_email.address or "")
if not addr or addr.lower() == email.address.lower():
return
try:
e = Email.objects.get(address=addr)
if e.person != email.person:
e.person = email.person
e.save()
except Email.DoesNotExist:
Email.objects.create(address=addr, person=email.person,
time=calc_email_import_time(old_email.priority))
def dont_save_queries():
# prevent memory from leaking when settings.DEBUG=True

View file

@ -40,18 +40,18 @@ class PersonInfo(models.Model):
if e:
return e[0]
# no cigar, try the complete set before giving up
e = self.email_set.order_by("-active")
e = self.email_set.order_by("-active", "-time")
if e:
return e[0]
return None
def email_address(self):
e = self.email_set.filter(active=True)
e = self.email_set.filter(active=True).order_by("-time")
if e:
return e[0].address
else:
return ""
def formatted_email(self):
e = self.email_set.order_by("-active")
e = self.email_set.order_by("-active", "-time")
if e:
return e[0].formatted_email()
else: