Flatlogic Bot e083afc1d5 Mock 2
2026-02-04 16:40:09 +00:00

34 lines
1.3 KiB
Python

import csv
from django.core.management.base import BaseCommand
from core.models import MemberOfParliament
class Command(BaseCommand):
help = 'Import MPs from CSV'
def handle(self, *args, **options):
file_path = 'mps.csv'
try:
with open(file_path, mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file)
count = 0
for row in reader:
name = f"{row['First Name']} {row['Last Name']}"
party = row['Political Affiliation']
constituency = row['Constituency']
province = row['Province / Territory']
MemberOfParliament.objects.update_or_create(
name=name,
defaults={
'party': party,
'constituency': constituency,
'province': province,
}
)
count += 1
self.stdout.write(self.style.SUCCESS(f'Successfully imported {count} MPs'))
except FileNotFoundError:
self.stdout.write(self.style.ERROR(f'File {file_path} not found'))
except Exception as e:
self.stdout.write(self.style.ERROR(f'Error: {str(e)}'))