Add data migration to set real SA ID numbers for all workers

Reads ID numbers from the workers_list.xlsx data and matches workers
by first name + surname (case-insensitive). Handles name variations
like "Soldier Aphiwe Dobe" matching "Aphiwe" + "Dobe".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Konrad du Plessis 2026-02-23 00:24:15 +02:00
parent b9c0a985c3
commit 3199e52e72

View File

@ -0,0 +1,65 @@
# === DATA MIGRATION: Update Worker ID Numbers ===
# One-time migration to set real SA ID numbers from the workers_list.xlsx file.
# Matches workers by first name + surname (case-insensitive contains).
# Safe: if a worker isn't found, it's skipped. If id_number is already correct, no change.
from django.db import migrations
from django.db.models import Q
# Worker ID data from workers_list.xlsx
# Format: (first_name_part, surname, id_number)
# We search for workers whose name contains BOTH the first name part AND the surname
WORKER_ID_DATA = [
('Mpho', 'Nkoana', '9811125984089'),
('Richard', 'Moleko', '0003185071085'),
('Fikile', 'Masimula', '8606305407088'),
('Clifford', 'Selemela', '0104205798085'),
('Shane', 'Malobela', '9807046054085'),
('Jimmy', 'Moleko', '0101176105084'),
('Johannes', 'Laka', '9809066044087'),
('Tumelo', 'Sinugo', '9009055943080'),
('Goitsimang', 'Moleko', '0403135542068'),
('Sello', 'Matloa', '0407046184088'),
('Aphiwe', 'Dobe', '9212236112084'),
('Tshepo', 'Moganedi', '8112175417083'),
]
def update_id_numbers(apps, schema_editor):
"""Update worker ID numbers from the Excel spreadsheet data."""
Worker = apps.get_model('core', 'Worker')
for first_name, surname, id_number in WORKER_ID_DATA:
# Find worker whose name contains both the first name and surname
# This handles cases like "Soldier Aphiwe Dobe" matching ("Aphiwe", "Dobe")
# or "Clifford Jan Bobby Selemela" matching ("Clifford", "Selemela")
matches = Worker.objects.filter(
Q(name__icontains=first_name) & Q(name__icontains=surname)
)
if matches.count() == 1:
worker = matches.first()
worker.id_number = id_number
worker.save(update_fields=['id_number'])
elif matches.count() > 1:
# Multiple matches — skip to avoid updating the wrong worker
# (shouldn't happen with first name + surname combo)
pass
# If no match found, skip silently — worker might not exist in this env
def reverse_id_numbers(apps, schema_editor):
"""Reverse is a no-op — we can't restore old ID numbers."""
pass
class Migration(migrations.Migration):
dependencies = [
('core', '0001_initial'),
]
operations = [
migrations.RunPython(update_id_numbers, reverse_id_numbers),
]