diff --git a/busManager/migration_scripts/populate_contacts.py b/busManager/migration_scripts/populate_contacts.py index e89ab6e..a0b219d 100644 --- a/busManager/migration_scripts/populate_contacts.py +++ b/busManager/migration_scripts/populate_contacts.py @@ -8,13 +8,21 @@ updated_families = 0 for family in Family.objects.all(): if family.parent_A_firstname or family.parent_A_lastname: + email = family.parent_A_email.strip() if family.parent_A_email else "" + contact_A, created = ContactPerson.objects.get_or_create( first_name=family.parent_A_firstname.strip() if family.parent_A_firstname else "", last_name=family.parent_A_lastname.strip() if family.parent_A_lastname else "", phone=family.parent_A_phone.strip() if family.parent_A_phone else "", - email=family.parent_A_email.strip() if family.parent_A_email else "", + defaults={ + "email": email, + } ) + if not created and email and not contact_A.email: + contact_A.email = email + contact_A.save(update_fields=["email"]) + family.contact_A = contact_A if created: @@ -22,13 +30,21 @@ for family in Family.objects.all(): if family.parent_B_firstname or family.parent_B_lastname: + email = family.parent_B_email.strip() if family.parent_B_email else "" + contact_B, created = ContactPerson.objects.get_or_create( first_name=family.parent_B_firstname.strip() if family.parent_B_firstname else "", last_name=family.parent_B_lastname.strip() if family.parent_B_lastname else "", phone=family.parent_B_phone.strip() if family.parent_B_phone else "", - email=family.parent_B_email.strip() if family.parent_B_email else "", + defaults={ + "email": email, + } ) + if not created and email and not contact_B.email: + contact_B.email = email + contact_B.save(update_fields=["email"]) + family.contact_B = contact_B if created: