Added Person object to ietf/stats/utils.py in get_meeting_registration_data

- Legacy-Id: 13949
This commit is contained in:
Seth Birkholz 2017-07-19 16:48:28 +00:00
parent 0c520b0a01
commit bffc7114eb

View file

@ -6,6 +6,11 @@ from django.conf import settings
from ietf.stats.models import AffiliationAlias, AffiliationIgnoredEnding, CountryAlias, MeetingRegistration
from ietf.name.models import CountryName
from ietf.person.models import Person
from django.contrib.auth.models import User
from unidecode import unidecode
def compile_affiliation_ending_stripping_regexp():
parts = []
@ -226,8 +231,12 @@ def get_meeting_registration_data(meeting):
else:
raise RuntimeError("Could not decode response from registrations API: '%s...'" % (response.content[:64], ))
# for each user identified in the Registration system
# Create a DataTracker MeetingRegistration object
for registration in decoded:
object, created = MeetingRegistration.objects.get_or_create(
person = None
obj, created = MeetingRegistration.objects.get_or_create(
meeting_id=meeting.pk,
first_name=registration['FirstName'],
last_name=registration['LastName'],
@ -235,6 +244,52 @@ def get_meeting_registration_data(meeting):
country_code=registration['Country'],
email=registration['Email'],
)
# Add a Person object to MeetingRegistration object
# if valid email is available
if not obj.person and "Email" in registration and '@' in registration["Email"]:\
# If the person already exists do not try to create a new one
persons = Person.objects.filter(user__username=registration["Email"])
if len(persons) > 0:
person = persons[0]
# Create a new Person object
else:
# ascii_name - convert from unicode if necessary
regname = "%s %s" % (registration["FirstName"], registration["LastName"])
# if there are any unicode characters decode the string to ascii
if not all(ord(c) < 128 for c in regname):
ascii_name = unidecode(regname).strip()
# it is already ascii, no need to convert
else:
ascii_name = regname
# Create a new user object if it does not exist already
# if the user already exists do not try ot create a new one
users = User.objects.filter(username=registration["Email"])
if len(users) > 0:
user = users[0]
else:
# Create a new user.
user = User.objects.create(
first_name=registration["FirstName"],
last_name=registration["LastName"],
username=registration["Email"],
email=registration["Email"]
)
# Create the new Person object.
person = Person.objects.create(
name=registration["Email"],
ascii=ascii_name,
affiliation=registration["Company"],
user=user
)
person.save()
# update the person object to an actual value
obj.person = person
obj.save()
if created:
num_created += 1
num_processed += 1