Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove create part of update_clinics management command #597

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions registrations/management/commands/tests/test_update_clinics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
31 changes: 12 additions & 19 deletions registrations/management/commands/update_clinics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ast
import csv

import pycountry
Expand Down Expand Up @@ -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}")
Loading