|
6 | 6 |
|
7 | 7 | from ietf.stats.models import AffiliationAlias, AffiliationIgnoredEnding, CountryAlias, MeetingRegistration |
8 | 8 | from ietf.name.models import CountryName |
| 9 | +from ietf.person.models import Person, Email, Alias |
| 10 | +from django.contrib.auth.models import User |
| 11 | +from unidecode import unidecode |
| 12 | + |
9 | 13 |
|
10 | 14 | def compile_affiliation_ending_stripping_regexp(): |
11 | 15 | parts = [] |
@@ -226,23 +230,96 @@ def get_meeting_registration_data(meeting): |
226 | 230 | else: |
227 | 231 | raise RuntimeError("Could not decode response from registrations API: '%s...'" % (response.content[:64], )) |
228 | 232 |
|
| 233 | + |
| 234 | + # for each user identified in the Registration system |
| 235 | + # Create a DataTracker MeetingRegistration object |
229 | 236 | for registration in decoded: |
| 237 | + person = None |
| 238 | + # capture the stripped registration values for later use |
| 239 | + first_name = registration['FirstName'].strip() |
| 240 | + last_name = registration['LastName'].strip() |
| 241 | + affiliation = registration['Company'].strip() |
| 242 | + country_code = registration['Country'].strip() |
| 243 | + address = registration['Email'].strip() |
230 | 244 | object, created = MeetingRegistration.objects.get_or_create( |
231 | 245 | meeting_id=meeting.pk, |
232 | | - first_name=registration['FirstName'].strip(), |
233 | | - last_name=registration['LastName'].strip(), |
234 | | - affiliation=registration['Company'].strip(), |
235 | | - country_code=registration['Country'].strip(), |
236 | | - email=registration['Email'].strip(), |
| 246 | + first_name=first_name, |
| 247 | + last_name=last_name, |
| 248 | + affiliation=affiliation, |
| 249 | + country_code=country_code, |
| 250 | + email=address, |
237 | 251 | ) |
| 252 | + |
| 253 | + # Add a Person object to MeetingRegistration object |
| 254 | + # if valid email is available |
| 255 | + if object and not object.person and address: |
| 256 | + # If the person already exists do not try to create a new one |
| 257 | + emails = Email.objects.filter(address=address) |
| 258 | + # there can only be on Email object with a unique email address (primary key) |
| 259 | + if emails.exists(): |
| 260 | + person = emails.first().person |
| 261 | + # Create a new Person object |
| 262 | + else: |
| 263 | + # Normalize all-caps or all-lower entries. Don't touch |
| 264 | + # others, there might be names properly spelled with |
| 265 | + # internal uppercase letters. |
| 266 | + if ( ( first_name == first_name.upper() or first_name == first_name.lower() ) |
| 267 | + and ( last_name == last_name.upper() or last_name == last_name.lower() ) ): |
| 268 | + first_name = first_name.capitalize() |
| 269 | + last_name = last_name.capitalize() |
| 270 | + regname = "%s %s" % (first_name, last_name) |
| 271 | + # if there are any unicode characters decode the string to ascii |
| 272 | + ascii_name = unidecode(regname).strip() |
| 273 | + |
| 274 | + # Create a new user object if it does not exist already |
| 275 | + # if the user already exists do not try to create a new one |
| 276 | + users = User.objects.filter(username=address) |
| 277 | + if users.exists(): |
| 278 | + user = users.first() |
| 279 | + else: |
| 280 | + # Create a new user. |
| 281 | + user = User.objects.create( |
| 282 | + first_name=first_name, |
| 283 | + last_name=last_name, |
| 284 | + username=address, |
| 285 | + email=address, |
| 286 | + ) |
| 287 | + user.save() |
| 288 | + |
| 289 | + aliases = Alias.objects.filter(name=regname) |
| 290 | + if aliases.exists(): |
| 291 | + person = aliases.first().person |
| 292 | + else: |
| 293 | + # Create the new Person object. |
| 294 | + person = Person.objects.create( |
| 295 | + name=regname, |
| 296 | + ascii=ascii_name, |
| 297 | + affiliation=affiliation, |
| 298 | + user=user, |
| 299 | + ) |
| 300 | + person.save() |
| 301 | + |
| 302 | + # Create an associated Email address for this Person |
| 303 | + email = Email.objects.create( |
| 304 | + person=person, |
| 305 | + address=address, |
| 306 | + ) |
| 307 | + |
| 308 | + # If this is the only email address, set primary to true. |
| 309 | + # If the person already existed (found through Alias) and |
| 310 | + # had email addresses, we don't do this. |
| 311 | + if Email.objects.filter(person=person).count() == 1: |
| 312 | + email.primary = True |
| 313 | + email.save() |
| 314 | + |
| 315 | + # update the person object to an actual value |
| 316 | + object.person = person |
| 317 | + object.save() |
| 318 | + |
238 | 319 | if created: |
239 | 320 | num_created += 1 |
240 | 321 | num_processed += 1 |
241 | 322 | else: |
242 | 323 | raise RuntimeError("Bad response from registrations API: %s, '%s'" % (response.status_code, response.content)) |
243 | 324 | num_total = MeetingRegistration.objects.filter(meeting_id=meeting.pk).count() |
244 | 325 | return num_created, num_processed, num_total |
245 | | - |
246 | | - |
247 | | - |
248 | | - |
|
0 commit comments