Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Script for importing counties, refs #80, #76
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 8, 2021
1 parent 12ef1ac commit 8ead9a3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions vaccinate/core/management/commands/import_counties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import csv
import io

import httpx
from core.models import County, State
from django.core.management.base import BaseCommand, CommandError


class Command(BaseCommand):
def handle(self, *args, **options):
counties_url = "https://us-counties.datasette.io/counties/county_fips.csv?_stream=on&_size=max"
# Bulk load all states
states = {state.abbreviation: state for state in State.objects.all()}
s = io.StringIO(httpx.get(counties_url).text)
for county in csv.DictReader(s):
if county["state"] not in states:
print(
"Skipping {}, state = {}".format(
county["county_name"], county["state"]
)
)
continue
County.objects.update_or_create(
fips_code=county["county_fips"],
defaults={
"name": county["county_name"],
"state": states[county["state"]],
},
)

0 comments on commit 8ead9a3

Please sign in to comment.