Skip to content

Commit

Permalink
refactor: address pr code-review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ajbarnes committed Mar 27, 2024
1 parent f7b0e47 commit a1337ee
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
37 changes: 27 additions & 10 deletions docs/training/admin_guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ as individuals described below.
To set up VA Explorer for the Geographic Access mentioned in that section, you
must load location data into the system.

Locations in VA Explorer follow an assumed three-level hierarchical structure by which each facility or hospital maps to an associated Level 2 ("District") and Level 1 ("Province) hierarchy. Each facility also has a corresponding `key`, which represents the XML option used in the dropdown list within ODK or Kobo, and a `status` which indicates if the Facility is actively producing VAs or not.
Locations in VA Explorer follow an assumed three-level hierarchical structure
by which each facility or hospital maps to an associated Level 2 ("District")
and Level 1 ("Province") hierarchy. Each facility also has a corresponding
`key`, which represents the XML option used in the dropdown list within ODK or
Kobo, and a `status` which indicates if the Facility is actively producing VAs
or not.

The table below shows an example location hierarchy for States, Counties, and
Cities in the United States. In this example, we have one state (California), two
Expand All @@ -28,18 +33,20 @@ California,Los Angeles County,Los Angeles Hospital, los_angeles_hospital, Active
```

The input is similarly structured to support any number of geographic hierarchies
for VA Explorer users. With a {term}`CSV` file in hand, you can now supplement your initial
system set up with the `load_locations` management command. Full usage details
for this are provided in [Management Commands](#management-commands). The specification of the input CSV file is as follows:
for VA Explorer users. With a {term}`CSV` file in hand, you can now supplement
your initial system set up with the `load_locations` management command. Full
usage details for this are provided in [Management Commands](#management-commands).
The specification of the input CSV file is as follows:

```{csv-table} Expected columns for the location file
:header-rows: 1
Column Name, Description, Specifics
Province, Level 1 Administrative Boundary Name, One of the `label::English` values as defined in the VA XLSForm
District, Level 2 Administrative Boundary Name, One of the `label::English` values as defined in the VA XLSForm
Province,Level 1 Administrative Boundary Name,One of the `label::English` values as defined in the VA XLSForm
District,Level 2 Administrative Boundary Name,One of the `label::English` values as defined in the VA XLSForm
Name, Facility or Hospital Name, One of the `label::English` values as defined in the VA XLSForm
Key, Facility or Hospital XML Value, The choice name associated with the `label::English` defined in the previous column
Status, Whether the facility is still actively producing VAs, One of: 'Active' or 'Inactive'
Key, Facility or Hospital XML Value, The choice name associated with the
`label::English` defined in the previous column Status, Whether the facility is
still actively producing VAs, One of: 'Active' or 'Inactive'
```

Following this command, VA Explorer should support geographic restrictions to any
Expand All @@ -51,7 +58,17 @@ Marin County, Los Angeles County, Sausalito, San Rafael, and Los Angeles.

#### Updating locations in VA Explorer

When VAs are imported into VA Explorer, they are matched exactly on the locations loaded into the system in this step. If a VA does not have a valid location field, VA Explorer will track that mismatch as an error that either needs to be corrected in the VA Explorer locations file or in the underlying VA data. To add a location to VA Explorer, re-upload a revised location file following the `load_locations` management command. If a row is deleted from the locations file, it will also be kept in VA Explorer and marked inactive. To permanently delete locations in VA Explorer, re-upload a revised location file following the `load_locations` management command with the `--delete_previous` flag. Warning: doing so may delete all VAs in the database, so make sure to backup the system first.
When VAs are imported into VA Explorer, they are matched exactly on the
locations loaded into the system in this step. If a VA does not have a valid
location field, VA Explorer will track that mismatch as an error that either
needs to be corrected in the VA Explorer locations file or in the underlying
VA data. To add a location to VA Explorer, re-upload a revised location file
following the `load_locations` management command. If a row is deleted from
the locations file, it will also be kept in VA Explorer and marked inactive.
To permanently delete locations in VA Explorer, re-upload a revised location
file following the `load_locations` management command with `--delete_previous`.
Warning: doing so may delete all VAs in the database, so make sure to
backup the system first.

### Creating & Editing Users

Expand Down Expand Up @@ -195,7 +212,7 @@ generally useful to admins. An even fuller list of these can be found under
* - ``export_locations``
- ``--output_file``
- Utility to obtainthe current list of locations in the VA
- Utility to obtain the current list of locations in the VA
Explorer system in the CSV format with header fields
corresponding to fields expected by the system. The intended use case
for this utility is when administrators need to update the location file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ class Command(BaseCommand):
new tree. If False (default), attempts to update existing Locations table
"""

help = "Loads initial location data into the database from a CSV file with \
relevant info. Required Columns: Province, District, Key, Name, Status."
help = (
"Loads initial location data into the database from a CSV file with"
"relevant info. Required Columns: Province, District, Key, Name, Status."
)

def add_arguments(self, parser):
parser.add_argument("csv_file", type=argparse.FileType("r"))
parser.add_argument("--delete_previous", type=bool, nargs="?", default=False)

def handle(self, *args, **options):
print(options)
csv_file = options["csv_file"]
delete_previous = options["delete_previous"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.core.management.base import BaseCommand

from va_explorer.va_data_management.models import Location, VerbalAutopsy
Expand All @@ -8,22 +9,18 @@
class Command(BaseCommand):
help = "Reassigns locations based on most recent facility list"

# def add_arguments(self, parser):
# # parser.add_argument("csv_file", type=argparse.FileType("r"))
# # parser.add_argument("--random_locations", type=str, nargs="?", default=False)

def handle(self, *args, **options):
# verbal_autopsies = VerbalAutopsy.objects
count = VerbalAutopsy.objects.count()
print("Refreshing locations for all " + str(count) + " VAs in the database.")

location_map = {}

verbal_autopsies = list(VerbalAutopsy.objects.filter()[0:count])
if settings.DEBUG:
count = VerbalAutopsy.objects.count()
print(
"Refreshing locations for all " + str(count) + " VAs in the database."
)

# build location mapper to map csv locations to known db locations
h = [va.hospital for va in verbal_autopsies]
# using list comprehension to remove duplicated from list
location_map = {}
verbal_autopsies = list(VerbalAutopsy.objects.all())
h = [va.hospital for va in verbal_autopsies]
hospitals = []
[hospitals.append(x) for x in h if x not in hospitals]

Expand All @@ -33,18 +30,18 @@ def handle(self, *args, **options):
.only("name", "key")
.values_list("key", "name")
}
changedcount = 0

changed_count = 0
for va in verbal_autopsies:
oldlocation = va.location
newva = assign_va_location(va, location_map)
newlocation = newva.location
old_location = va.location
new_va = assign_va_location(va, location_map)
new_location = new_va.location

if oldlocation != newlocation:
changedcount += 1
va.location = newva.location
if old_location != new_location:
changed_count += 1
va.location = new_va.location
va.save_without_historical_record()

validate_vas_for_dashboard(verbal_autopsies)

print(" changed locations for " + str(changedcount) + " VA(s).")
print(" changed locations for " + str(changed_count) + " VA(s).")

0 comments on commit a1337ee

Please sign in to comment.