Skip to content
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
47 changes: 39 additions & 8 deletions src/base/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"display_order_number": 2,
"short_name": "HHS",
},
"hhs-region": {
"display_name": "U.S. HHS Region",
"display_order_number": 2,
"short_name": "HHS",
},
"census-region": {
"display_name": "U.S. Census Region",
"display_order_number": 3,
Expand All @@ -34,40 +39,66 @@
"display_order_number": 7,
"short_name": "ADM 3",
},
"hrr": {
"hsa": {
"display_name": "Hospital Referral Region (HRR)",
"display_order_number": 8,
"short_name": "HSA-NCI",
},
"hrr": {
"display_name": "Hospital Referral Region (HRR)",
"display_order_number": 9,
"short_name": "HRR",
},
"msa": {
"display_name": "Metropolitan Statistical Area (MSA)",
"display_order_number": 9,
"display_order_number": 10,
"short_name": "MSA",
},
"dma": {
"display_name": "Designated Market Area (DMA)",
"display_order_number": 10,
"display_order_number": 11,
"short_name": "DMA",
},
"other_substate_region": {
"display_name": "Other Substate Region",
"display_order_number": 11,
"display_order_number": 12,
"short_name": "Other Substate Region",
},
"FluSurv-Net site": {
"display_name": "FluSurv-Net site (see documentation)",
"display_order_number": 12,
"display_order_number": 13,
"short_name": "FluSurv-Net site",
},
"facility": {
"display_name": 'Hospital ("Facility")',
"display_order_number": 13,
"display_order_number": 14,
"short_name": "Facility",
},
"lat_long": {
"display_name": "Lat/Long",
"display_order_number": 14,
"display_order_number": 15,
"short_name": "Lat/Long",
},
"N/A": {"display_name": "N/A", "display_order_number": 15, "short_name": "N/A"},
"N/A": {"display_name": "N/A", "display_order_number": 16, "short_name": "N/A"},
}


def get_geographic_mapping_by_name(name):
"""
Get geographic granularity mapping by key or by display_name.

First tries to get by key, then searches by display_name if not found.
Returns a tuple (key, value) where key is the dictionary key and value
is the mapping dict, or None if not found.
"""
# First try to get by key
mapping = GEOGRAPHIC_GRANULARITY_MAPPING.get(name)
if mapping:
return (name, mapping)

# If not found, search by display_name
for key, value in GEOGRAPHIC_GRANULARITY_MAPPING.items():
if value.get("display_name") == name:
return (key, value)

return None
6 changes: 3 additions & 3 deletions src/epiportal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"other_endpoint_source_subdivisions": "https://docs.google.com/spreadsheets/d/1zb7ItJzY5oq1n-2xtvnPBiJu2L3AqmCKubrLkKJZVHs/export?format=csv&gid=214580132",
"indicator_sets": "https://docs.google.com/spreadsheets/d/1zb7ItJzY5oq1n-2xtvnPBiJu2L3AqmCKubrLkKJZVHs/export?format=csv&gid=1266808975",
"non_delphi_indicator_sets": "https://docs.google.com/spreadsheets/d/1zb7ItJzY5oq1n-2xtvnPBiJu2L3AqmCKubrLkKJZVHs/export?format=csv&gid=1266477926",
"indicators": "https://docs.google.com/spreadsheets/d/1MdT5y1etftBQd4f1rmcZxfedi7ESSsoBhAs0cf5PCKM/export?format=csv&gid=329338228",
"other_endpoint_indicators": "https://docs.google.com/spreadsheets/d/1MdT5y1etftBQd4f1rmcZxfedi7ESSsoBhAs0cf5PCKM/export?format=csv&gid=1364181703",
"non_delphi_indicators": "https://docs.google.com/spreadsheets/d/1MdT5y1etftBQd4f1rmcZxfedi7ESSsoBhAs0cf5PCKM/export?format=csv&gid=493612863",
"indicators": "https://docs.google.com/spreadsheets/d/1zb7ItJzY5oq1n-2xtvnPBiJu2L3AqmCKubrLkKJZVHs/export?format=csv&gid=329338228",
"other_endpoint_indicators": "https://docs.google.com/spreadsheets/d/1zb7ItJzY5oq1n-2xtvnPBiJu2L3AqmCKubrLkKJZVHs/export?format=csv&gid=1364181703",
"non_delphi_indicators": "https://docs.google.com/spreadsheets/d/1zb7ItJzY5oq1n-2xtvnPBiJu2L3AqmCKubrLkKJZVHs/export?format=csv&gid=493612863",
"us_state_indicator_sets": "https://docs.google.com/spreadsheets/d/1zb7ItJzY5oq1n-2xtvnPBiJu2L3AqmCKubrLkKJZVHs/export?format=csv&gid=232438393",
"us_state_indicators": "https://docs.google.com/spreadsheets/d/1zb7ItJzY5oq1n-2xtvnPBiJu2L3AqmCKubrLkKJZVHs/export?format=csv&gid=1158629578"
}
Expand Down
23 changes: 17 additions & 6 deletions src/fixtures/geographic_granularities.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,24 @@
"short_name": "ADM 3"
}
},
{
"model": "base.Geography",
"pk": 14,
"fields": {
"name": "hsa",
"display_name": "Health Service Area (HSA-NCI)",
"display_order_number": 8,
"used_in": "indicatorsets",
"short_name": "HSA-NCI"
}
},
{
"model": "base.Geography",
"pk": 8,
"fields": {
"name": "hrr",
"display_name": "Hospital Referral Region (HRR)",
"display_order_number": 8,
"display_order_number": 9,
"used_in": "indicatorsets",
"short_name": "HRR"
}
Expand All @@ -93,7 +104,7 @@
"fields": {
"name": "msa",
"display_name": "Metropolitan Statistical Area (MSA)",
"display_order_number": 9,
"display_order_number": 10,
"used_in": "indicatorsets",
"short_name": "MSA"
}
Expand All @@ -104,7 +115,7 @@
"fields": {
"name": "dma",
"display_name": "Designated Market Area (DMA)",
"display_order_number": 10,
"display_order_number": 11,
"used_in": "indicatorsets",
"short_name": "DMA"
}
Expand All @@ -115,7 +126,7 @@
"fields": {
"name": "facility",
"display_name": "Hospital (\"Facility\")",
"display_order_number": 11,
"display_order_number": 12,
"used_in": "indicatorsets",
"short_name": "Facility"
}
Expand All @@ -126,7 +137,7 @@
"fields": {
"name": "FluSurv-Net site",
"display_name": "FluSurv-Net site (see documentation)",
"display_order_number": 12,
"display_order_number": 13,
"used_in": "indicatorsets",
"short_name": "FluSurv-Net site"
}
Expand All @@ -137,7 +148,7 @@
"fields": {
"name": "N/A",
"display_name": "N/A",
"display_order_number": 13,
"display_order_number": 14,
"used_in": "indicatorsets",
"short_name": "N/A"
}
Expand Down
68 changes: 52 additions & 16 deletions src/indicators/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
from django.urls import path
from import_export.admin import ImportExportModelAdmin

from base.models import Geography, Pathogen, SeverityPyramidRung
from base.utils import download_source_file, import_data
from indicators.models import (Category, FormatType, Indicator,
IndicatorGeography, IndicatorType,
NonDelphiIndicator, OtherEndpointIndicator,
USStateIndicator)
from indicators.resources import (IndicatorBaseResource, IndicatorResource,
NonDelphiIndicatorResource,
OtherEndpointIndicatorResource,
USStateIndicatorResource)
from indicators.models import (
Category,
FormatType,
Indicator,
IndicatorGeography,
IndicatorType,
NonDelphiIndicator,
OtherEndpointIndicator,
USStateIndicator,
)
from indicators.resources import (
IndicatorBaseResource,
IndicatorResource,
NonDelphiIndicatorResource,
OtherEndpointIndicatorResource,
USStateIndicatorResource,
)


@admin.register(IndicatorType)
class IndicatorTypeAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -47,8 +58,35 @@ class IndicatorGeographyAdmin(admin.ModelAdmin):
list_select_related = True


class BaseIndicatorAdmin(ImportExportModelAdmin):
def formfield_for_manytomany(self, db_field, request, **kwargs):
"""
Filter geographic_levels field to show only a subset of Geography objects.
Modify the filter criteria as needed.
"""
if db_field.name == "geographic_levels":
# Filter to show only geographies used in indicatorsets
# You can modify this filter to show a different subset
kwargs["queryset"] = Geography.objects.filter(
used_in="indicators"
).order_by("display_order_number")
if db_field.name == "pathogens":
# Filter to show only geographies used in indicatorsets
# You can modify this filter to show a different subset
kwargs["queryset"] = Pathogen.objects.filter(used_in="indicators").order_by(
"display_order_number"
)
if db_field.name == "severity_pyramid_rungs":
# Filter to show only geographies used in indicatorsets
# You can modify this filter to show a different subset
kwargs["queryset"] = SeverityPyramidRung.objects.filter(
used_in="indicators"
).order_by("display_order_number")
return super().formfield_for_manytomany(db_field, request, **kwargs)


@admin.register(Indicator)
class IndicatorAdmin(ImportExportModelAdmin):
class IndicatorAdmin(BaseIndicatorAdmin):
list_display = (
"name",
"description",
Expand Down Expand Up @@ -102,7 +140,7 @@ def download_indicator(self, request):


@admin.register(OtherEndpointIndicator)
class OtherEndpointIndicatorAdmin(ImportExportModelAdmin):
class OtherEndpointIndicatorAdmin(BaseIndicatorAdmin):
list_display = (
"name",
"description",
Expand Down Expand Up @@ -138,9 +176,7 @@ def get_urls(self):
),
path(
"download-source-file",
self.admin_site.admin_view(
self.download_other_endpoint_indicator
),
self.admin_site.admin_view(self.download_other_endpoint_indicator),
name="download_other_endpoint_indicator",
),
]
Expand All @@ -162,7 +198,7 @@ def download_other_endpoint_indicator(self, request):


@admin.register(NonDelphiIndicator)
class NonDelphiIndicatorAdmin(ImportExportModelAdmin):
class NonDelphiIndicatorAdmin(BaseIndicatorAdmin):
list_display = (
"name",
"member_name",
Expand Down Expand Up @@ -214,7 +250,7 @@ def download_nondelphi_indicator(self, request):


@admin.register(USStateIndicator)
class USStateIndicatorAdmin(ImportExportModelAdmin):
class USStateIndicatorAdmin(BaseIndicatorAdmin):
list_display = ("name", "indicator_set")
search_fields = ("name", "indicator_set")
ordering = ("name",)
Expand Down Expand Up @@ -257,4 +293,4 @@ def download_us_state_indicator(self, request):
return download_source_file(
settings.SPREADSHEET_URLS["us_state_indicators"],
"US_State_Indicators.csv",
)
)
34 changes: 31 additions & 3 deletions src/indicatorsets/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.urls import path
from import_export.admin import ImportExportModelAdmin

from base.models import Geography, Pathogen, SeverityPyramidRung
from base.utils import download_source_file, import_data
from indicatorsets.models import (
ColumnDescription,
Expand All @@ -15,9 +16,36 @@
from indicatorsets.resources import USStateIndicatorSetResource


class BaseIndicatorSetAdmin(ImportExportModelAdmin):
def formfield_for_manytomany(self, db_field, request, **kwargs):
"""
Filter geographic_levels field to show only a subset of Geography objects.
Modify the filter criteria as needed.
"""
if db_field.name == "geographic_levels":
# Filter to show only geographies used in indicatorsets
# You can modify this filter to show a different subset
kwargs["queryset"] = Geography.objects.filter(
used_in="indicatorsets"
).order_by("display_order_number")
if db_field.name == "pathogens":
# Filter to show only geographies used in indicatorsets
# You can modify this filter to show a different subset
kwargs["queryset"] = Pathogen.objects.filter(
used_in="indicatorsets"
).order_by("display_order_number")
if db_field.name == "severity_pyramid_rungs":
# Filter to show only geographies used in indicatorsets
# You can modify this filter to show a different subset
kwargs["queryset"] = SeverityPyramidRung.objects.filter(
used_in="indicatorsets"
).order_by("display_order_number")
return super().formfield_for_manytomany(db_field, request, **kwargs)


# Register your models here.
@admin.register(IndicatorSet)
class IndicatorSetAdmin(ImportExportModelAdmin):
class IndicatorSetAdmin(BaseIndicatorSetAdmin):
"""
Admin interface for the IndicatorSet model.
"""
Expand Down Expand Up @@ -78,7 +106,7 @@ def download_indicator_set(self, request):


@admin.register(NonDelphiIndicatorSet)
class NonDelphiIndicatorSetAdmin(ImportExportModelAdmin):
class NonDelphiIndicatorSetAdmin(BaseIndicatorSetAdmin):
"""
Admin interface for the IndicatorSet model.
"""
Expand Down Expand Up @@ -141,7 +169,7 @@ def download_nondelphi_indicator_set(self, request):


@admin.register(USStateIndicatorSet)
class USStateIndicatorSetAdmin(ImportExportModelAdmin):
class USStateIndicatorSetAdmin(BaseIndicatorSetAdmin):
"""
Admin interface for the USStateIndicatorSet model.
"""
Expand Down
4 changes: 2 additions & 2 deletions src/indicatorsets/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IndicatorSetFilterForm(forms.ModelForm):
)

hosted_by_delphi = forms.BooleanField(
label="Hosted by Delphi",
label="Delphi-hosted Only",
required=False,
widget=forms.CheckboxInput(attrs={"value": "on"}),
)
Expand Down Expand Up @@ -90,4 +90,4 @@ def __init__(self, *args, **kwargs) -> None:
if field_name != "hosted_by_delphi":
field.label = ""
else:
field.label = "Hosted by Delphi"
field.label = "Delphi-hosted Only"
10 changes: 6 additions & 4 deletions src/indicatorsets/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from import_export.widgets import ForeignKeyWidget, ManyToManyWidget

from base.models import GeographicScope, Geography, Pathogen, SeverityPyramidRung
from base.resources import GEOGRAPHIC_GRANULARITY_MAPPING
from base.resources import get_geographic_mapping_by_name
from indicatorsets.models import (
IndicatorSet,
NonDelphiIndicatorSet,
Expand Down Expand Up @@ -70,9 +70,11 @@ def process_available_geographies(row) -> None:
for geography in available_geographies:
geography_name = geography.strip()
default_params = {"used_in": "indicatorsets"}
try:
default_params.update(GEOGRAPHIC_GRANULARITY_MAPPING[geography_name])
except KeyError:
result = get_geographic_mapping_by_name(geography_name)
if result:
geography_name, mapping = result
default_params.update(mapping)
else:
max_display_order_number = Geography.objects.filter(
used_in="indicatorsets"
).aggregate(Max("display_order_number"))["display_order_number__max"]
Expand Down
Loading
Loading