diff --git a/ietf/stats/tests.py b/ietf/stats/tests.py index ad30b4ef3..29f1c8c35 100644 --- a/ietf/stats/tests.py +++ b/ietf/stats/tests.py @@ -1,3 +1,4 @@ + import datetime from mock import patch @@ -5,6 +6,7 @@ from pyquery import PyQuery from requests import Response from django.urls import reverse as urlreverse +from django.contrib.auth.models import User from ietf.utils.test_data import make_test_data, make_review_data from ietf.utils.test_utils import login_testing_unauthorized, TestCase, unicontent @@ -199,3 +201,21 @@ class StatisticsTests(TestCase): get_meeting_registration_data(meeting) query = MeetingRegistration.objects.filter(first_name='John',last_name='Smith',country_code='US') self.assertTrue(query.count(), 1) + self.assertTrue(isinstance(query[0].person,Person)) + + @patch('requests.get') + def test_get_meeting_registration_data_user_exists(self, mock_get): + response = Response() + response.status_code = 200 + response._content = '[{"LastName":"Smith","FirstName":"John","Company":"ABC","Country":"US","Email":"john.doe@example.us"}]' + user = User.objects.create(username="john.doe@example.us") + user.save() + + mock_get.return_value = response + meeting = MeetingFactory(type_id='ietf', date=datetime.date(2016,7,14), number="96") + get_meeting_registration_data(meeting) + query = MeetingRegistration.objects.filter(first_name='John',last_name='Smith',country_code='US') + self.assertTrue(query.count(), 1) + self.assertTrue(isinstance(query[0].person, Person)) + + diff --git a/ietf/stats/utils.py b/ietf/stats/utils.py index d700395d6..3702a0a3b 100644 --- a/ietf/stats/utils.py +++ b/ietf/stats/utils.py @@ -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, Email +from django.contrib.auth.models import User +from unidecode import unidecode + + def compile_affiliation_ending_stripping_regexp(): parts = [] @@ -226,7 +231,11 @@ 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: + person = None object, created = MeetingRegistration.objects.get_or_create( meeting_id=meeting.pk, first_name=registration['FirstName'], @@ -235,6 +244,50 @@ 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 object.person: + # If the person already exists do not try to create a new one + emails = Email.objects.filter(address=registration["Email"]) + # there can only be on Email object with a unique email address (primary key) + if len(emails) == 1: + person = emails[0].person + # 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 + ascii_name = unidecode(regname).strip() + + # Create a new user object if it does not exist already + # if the user already exists do not try to 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"] + ) + user.save() + + # Create the new Person object. + person = Person.objects.create( + name=regname, + ascii=ascii_name, + affiliation=registration["Company"], + user=user + ) + person.save() + + # update the person object to an actual value + object.person = person + object.save() + if created: num_created += 1 num_processed += 1