diff --git a/registrations/management/commands/tests/test_update_clinics.py b/registrations/management/commands/tests/test_update_clinics.py index c8b9c6ee..579ea5bf 100644 --- a/registrations/management/commands/tests/test_update_clinics.py +++ b/registrations/management/commands/tests/test_update_clinics.py @@ -16,21 +16,12 @@ def test_update_clinics(self): ) existing_clinic.refresh_from_db() - self.assertEqual(existing_clinic.name, "Test Clinic 1") - self.assertEqual(existing_clinic.location, "-33.54222+025.69077/") - self.assertEqual(existing_clinic.province, "ZA-EC") self.assertEqual(existing_clinic.area_type, "Urban") self.assertEqual(existing_clinic.unit_type, "Clinic") self.assertEqual(existing_clinic.district, "Sarah Baartman DM") self.assertEqual(existing_clinic.municipality, "Sundays River Valley LM") - new_clinic = ClinicCode.objects.get(uid="QYdJjvibz4e") - self.assertEqual(new_clinic.name, "Test Clinic 2") - self.assertEqual(new_clinic.location, "-32.69994+026.29404/") - self.assertEqual(new_clinic.province, "ZA-EC") - self.assertEqual(new_clinic.area_type, "Urban") - self.assertEqual(new_clinic.unit_type, "Clinic") - self.assertEqual(new_clinic.district, "Amathole DM") - self.assertEqual(new_clinic.municipality, "Raymond Mhlaba LM") + new_clinic = ClinicCode.objects.filter(uid="QYdJjvibz4e").first() + self.assertIsNone(new_clinic) - self.assertEqual(ClinicCode.objects.count(), 2) + self.assertEqual(ClinicCode.objects.count(), 1) diff --git a/registrations/management/commands/update_clinics.py b/registrations/management/commands/update_clinics.py index caa3754c..654c80b3 100644 --- a/registrations/management/commands/update_clinics.py +++ b/registrations/management/commands/update_clinics.py @@ -1,4 +1,3 @@ -import ast import csv import pycountry @@ -45,27 +44,21 @@ def get_province(self, row): return PROVINCES[clean_name(row["OU2short"])] def get_location(self, row): - lng, lat = ast.literal_eval(row["coordinates"]) - return self.format_location(lat, lng) + if row["longitude"] and row["latitude"]: + lng = float(row["longitude"]) + lat = float(row["latitude"]) + return self.format_location(lat, lng) def handle(self, *args, **options): - print(options["csv_file"]) for csv_file in options["csv_file"]: reader = csv.DictReader(open(csv_file)) for row in reader: - clinic, created = ClinicCode.objects.update_or_create( - uid=row["OU5uid"], - defaults={ - "code": row["OU5code"], - "value": row["OU5code"], - "name": row["organisationunitname"], - "province": self.get_province(row), - "location": self.get_location(row), - "area_type": row["OrgUnitRuralUrban"], - "unit_type": row["OrgUnitType"], - "district": row["OU3short"], - "municipality": row["OU4short"], - }, - ) - print(f"{'Created' if created else 'Updated'}: {clinic.value}") + clinic = ClinicCode.objects.filter(uid=row["OU5uid"]).first() + if clinic: + clinic.area_type = row["OrgUnitRuralUrban"] + clinic.unit_type = row["OrgUnitType"] + clinic.district = row["OU3short"] + clinic.municipality = row["OU4short"] + clinic.save() + print(f"Updated: {clinic.value}")