diff --git a/src/assets/js/selectedIndicatorsModal.js b/src/assets/js/selectedIndicatorsModal.js
index 65048f0..51c799d 100644
--- a/src/assets/js/selectedIndicatorsModal.js
+++ b/src/assets/js/selectedIndicatorsModal.js
@@ -175,6 +175,29 @@ async function checkGeoCoverage(geoValue) {
}
}
+function getAvailableGeos(indicators) {
+ var availableGeos = null;
+
+ const csrftoken = Cookies.get("csrftoken");
+
+ const submitData = {
+ indicators: indicators
+ }
+
+ $.ajax({
+ url: "get_available_geos/",
+ type: "POST",
+ async: false, // Synchronous request to ensure availableGeos is populated before returning
+ dataType: "json",
+ contentType: "application/json",
+ headers: { "X-CSRFToken": csrftoken },
+ data: JSON.stringify(submitData),
+ }).done(function (data) {
+ availableGeos = data.geographic_granularities;
+ });
+ return availableGeos;
+}
+
$("#geographic_value").on("select2:select", function (e) {
var geo = e.params.data;
checkGeoCoverage(geo.id).then((notCoveredIndicators) => {
@@ -187,6 +210,14 @@ $("#geographic_value").on("select2:select", function (e) {
$("#showSelectedIndicatorsButton").click(function () {
alertPlaceholder.innerHTML = "";
+ const availableGeos = getAvailableGeos(checkedIndicatorMembers);
+ const locationIds = $("#location_search").select2("data").map((item) => item.id);
+ $("#geographic_value").select2({
+ data: availableGeos,
+ minimumInputLength: 0,
+ maximumSelectionLength: 5,
+ });
+ $('#geographic_value').val(locationIds).trigger('change');
if (!indicatorHandler.checkForCovidcastIndicators()) {
$('#geographic_value').val(null).trigger('change');
$("#geographic_value").prop("disabled", true);
@@ -200,11 +231,10 @@ $("#showSelectedIndicatorsButton").click(function () {
}
})
});
- var otherEndpointLocationsWarning = `
` +
- `
Please, note that some indicator sets may require to select location(s) that is/are different from location(s) above.
`
nonCovidcastIndicatorSets = [...new Set(checkedIndicatorMembers.filter(indicator => indicator["_endpoint"] != "covidcast").map((indicator) => indicator["indicator_set"]))];
- otherEndpointLocationsWarning += `Different location is required for following Indicator Set(s): ${nonCovidcastIndicatorSets.join(", ")}`
- otherEndpointLocationsWarning += `
`
+ var otherEndpointLocationsWarning = ``
+ otherEndpointLocationsWarning += `For Indicator Set(s): ${nonCovidcastIndicatorSets.join(", ")}, please use the Location menu below:`
+ otherEndpointLocationsWarning += `
`
if (indicatorHandler.getFluviewIndicators().length > 0) {
$("#differentLocationNote").html(otherEndpointLocationsWarning)
if (document.getElementsByName("fluviewRegions").length === 0) {
diff --git a/src/indicatorsets/urls.py b/src/indicatorsets/urls.py
index 8b1da73..f40a5d1 100644
--- a/src/indicatorsets/urls.py
+++ b/src/indicatorsets/urls.py
@@ -1,7 +1,7 @@
from django.urls import path
from django.urls.resolvers import URLPattern
from indicatorsets.views import (IndicatorSetListView, epivis,
- generate_export_data_url, preview_data, create_query_code)
+ generate_export_data_url, preview_data, create_query_code, get_available_geos)
urlpatterns: list[URLPattern] = [
path("", IndicatorSetListView.as_view(), name="indicatorsets"),
@@ -9,4 +9,5 @@
path("export/", generate_export_data_url, name="export"),
path("preview_data/", preview_data, name="preview_data"),
path("create_query_code/", create_query_code, name="create_query_code"),
+ path("get_available_geos/", get_available_geos, name="get_available_geos"),
]
diff --git a/src/indicatorsets/views.py b/src/indicatorsets/views.py
index b1c53b6..760ddd1 100644
--- a/src/indicatorsets/views.py
+++ b/src/indicatorsets/views.py
@@ -540,3 +540,42 @@ def create_query_code(request):
{"python_code_blocks": python_code_blocks, "r_code_blocks": r_code_blocks},
safe=False,
)
+
+
+def get_available_geos(request):
+ if request.method == "POST":
+ geo_values = []
+ data = json.loads(request.body)
+ indicators = data.get("indicators", [])
+ grouped_indicators = group_by_property(indicators, "data_source")
+ for data_source, indicators in grouped_indicators.items():
+ indicators_str = ",".join(indicator["indicator"] for indicator in indicators)
+ response = requests.get(f"{settings.EPIDATA_URL}covidcast/geo_indicator_coverage", params={"data_source": data_source, "signals": indicators_str}, auth=("epidata", settings.EPIDATA_API_KEY))
+ if response.status_code == 200:
+ data = response.json()
+ if len(data["epidata"]):
+ geo_values.extend(data["epidata"])
+ unique_values = set(geo_values)
+ geo_levels = set([el.split(":")[0] for el in unique_values])
+ geo_unit_ids = set([geo_value.split(":")[1] for geo_value in unique_values])
+ geographic_granularities = [
+ {
+ "id": f"{geo_unit.geo_level.name}:{geo_unit.geo_id}",
+ "geoType": geo_unit.geo_level.name,
+ "text": geo_unit.display_name,
+ "geoTypeDisplayName": geo_unit.geo_level.display_name,
+ }
+ for geo_unit in GeographyUnit.objects.filter(geo_level__name__in=geo_levels).filter(geo_id__in=geo_unit_ids)
+ .prefetch_related("geo_level")
+ .order_by("level")
+ ]
+ grouped_geographic_granularities = group_by_property(
+ geographic_granularities, "geoTypeDisplayName"
+ )
+ geographic_granularities = []
+ for key, value in grouped_geographic_granularities.items():
+ geographic_granularities.append({
+ "text": key,
+ "children": value,
+ })
+ return JsonResponse({"geographic_granularities": geographic_granularities}, safe=False)
diff --git a/src/templates/indicatorsets/selectedIndicatorsModal.html b/src/templates/indicatorsets/selectedIndicatorsModal.html
index 07b3e9d..6c43d4f 100644
--- a/src/templates/indicatorsets/selectedIndicatorsModal.html
+++ b/src/templates/indicatorsets/selectedIndicatorsModal.html
@@ -144,19 +144,9 @@ Selected indicators
-
-
-
+
+
+
\ No newline at end of file