"""Fill in full names and tidy phone numbers, emails and nationalities on existing participants.""" import frappe from lukenya_marathon.lukenya_marathon.participant_utils import ( build_full_name, normalize_nationality, normalize_phone, ) def execute(): rows = frappe.db.sql( """select name, first_name, last_name, full_name, nationality, phone, whatsapp, emergency_contact_phone, email, id_number from `tabParticipant`""", as_dict=True, ) for row in rows: wanted = { "full_name": build_full_name(row.first_name, row.last_name), "nationality": normalize_nationality(row.nationality), "phone": normalize_phone(row.phone), "whatsapp": normalize_phone(row.whatsapp), "emergency_contact_phone": normalize_phone(row.emergency_contact_phone), "email": (row.email or "").strip().lower(), "id_number": (row.id_number or "").strip() or None, } changes = {key: value for key, value in wanted.items() if (row.get(key) or None) != (value or None)} if changes: frappe.db.set_value("Participant", row.name, changes, update_modified=False)