33from django .urls import path
44from import_export .admin import ImportExportModelAdmin
55
6+ from base .models import Geography , Pathogen , SeverityPyramidRung
67from base .utils import download_source_file , import_data
7- from indicators .models import (Category , FormatType , Indicator ,
8- IndicatorGeography , IndicatorType ,
9- NonDelphiIndicator , OtherEndpointIndicator ,
10- USStateIndicator )
11- from indicators .resources import (IndicatorBaseResource , IndicatorResource ,
12- NonDelphiIndicatorResource ,
13- OtherEndpointIndicatorResource ,
14- USStateIndicatorResource )
8+ from indicators .models import (
9+ Category ,
10+ FormatType ,
11+ Indicator ,
12+ IndicatorGeography ,
13+ IndicatorType ,
14+ NonDelphiIndicator ,
15+ OtherEndpointIndicator ,
16+ USStateIndicator ,
17+ )
18+ from indicators .resources import (
19+ IndicatorBaseResource ,
20+ IndicatorResource ,
21+ NonDelphiIndicatorResource ,
22+ OtherEndpointIndicatorResource ,
23+ USStateIndicatorResource ,
24+ )
25+
1526
1627@admin .register (IndicatorType )
1728class IndicatorTypeAdmin (admin .ModelAdmin ):
@@ -47,8 +58,35 @@ class IndicatorGeographyAdmin(admin.ModelAdmin):
4758 list_select_related = True
4859
4960
61+ class BaseIndicatorAdmin (ImportExportModelAdmin ):
62+ def formfield_for_manytomany (self , db_field , request , ** kwargs ):
63+ """
64+ Filter geographic_levels field to show only a subset of Geography objects.
65+ Modify the filter criteria as needed.
66+ """
67+ if db_field .name == "geographic_levels" :
68+ # Filter to show only geographies used in indicatorsets
69+ # You can modify this filter to show a different subset
70+ kwargs ["queryset" ] = Geography .objects .filter (
71+ used_in = "indicators"
72+ ).order_by ("display_order_number" )
73+ if db_field .name == "pathogens" :
74+ # Filter to show only geographies used in indicatorsets
75+ # You can modify this filter to show a different subset
76+ kwargs ["queryset" ] = Pathogen .objects .filter (used_in = "indicators" ).order_by (
77+ "display_order_number"
78+ )
79+ if db_field .name == "severity_pyramid_rungs" :
80+ # Filter to show only geographies used in indicatorsets
81+ # You can modify this filter to show a different subset
82+ kwargs ["queryset" ] = SeverityPyramidRung .objects .filter (
83+ used_in = "indicators"
84+ ).order_by ("display_order_number" )
85+ return super ().formfield_for_manytomany (db_field , request , ** kwargs )
86+
87+
5088@admin .register (Indicator )
51- class IndicatorAdmin (ImportExportModelAdmin ):
89+ class IndicatorAdmin (BaseIndicatorAdmin ):
5290 list_display = (
5391 "name" ,
5492 "description" ,
@@ -102,7 +140,7 @@ def download_indicator(self, request):
102140
103141
104142@admin .register (OtherEndpointIndicator )
105- class OtherEndpointIndicatorAdmin (ImportExportModelAdmin ):
143+ class OtherEndpointIndicatorAdmin (BaseIndicatorAdmin ):
106144 list_display = (
107145 "name" ,
108146 "description" ,
@@ -138,9 +176,7 @@ def get_urls(self):
138176 ),
139177 path (
140178 "download-source-file" ,
141- self .admin_site .admin_view (
142- self .download_other_endpoint_indicator
143- ),
179+ self .admin_site .admin_view (self .download_other_endpoint_indicator ),
144180 name = "download_other_endpoint_indicator" ,
145181 ),
146182 ]
@@ -162,7 +198,7 @@ def download_other_endpoint_indicator(self, request):
162198
163199
164200@admin .register (NonDelphiIndicator )
165- class NonDelphiIndicatorAdmin (ImportExportModelAdmin ):
201+ class NonDelphiIndicatorAdmin (BaseIndicatorAdmin ):
166202 list_display = (
167203 "name" ,
168204 "member_name" ,
@@ -214,7 +250,7 @@ def download_nondelphi_indicator(self, request):
214250
215251
216252@admin .register (USStateIndicator )
217- class USStateIndicatorAdmin (ImportExportModelAdmin ):
253+ class USStateIndicatorAdmin (BaseIndicatorAdmin ):
218254 list_display = ("name" , "indicator_set" )
219255 search_fields = ("name" , "indicator_set" )
220256 ordering = ("name" ,)
@@ -257,4 +293,4 @@ def download_us_state_indicator(self, request):
257293 return download_source_file (
258294 settings .SPREADSHEET_URLS ["us_state_indicators" ],
259295 "US_State_Indicators.csv" ,
260- )
296+ )
0 commit comments