Skip to content

Commit 7617961

Browse files
authored
Merge pull request #82 from cmu-delphi/staging
Staging
2 parents 932b409 + d80e374 commit 7617961

File tree

4 files changed

+79
-19
lines changed

4 files changed

+79
-19
lines changed

src/assets/js/selectedIndicatorsModal.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,29 @@ async function checkGeoCoverage(geoValue) {
175175
}
176176
}
177177

178+
function getAvailableGeos(indicators) {
179+
var availableGeos = null;
180+
181+
const csrftoken = Cookies.get("csrftoken");
182+
183+
const submitData = {
184+
indicators: indicators
185+
}
186+
187+
$.ajax({
188+
url: "get_available_geos/",
189+
type: "POST",
190+
async: false, // Synchronous request to ensure availableGeos is populated before returning
191+
dataType: "json",
192+
contentType: "application/json",
193+
headers: { "X-CSRFToken": csrftoken },
194+
data: JSON.stringify(submitData),
195+
}).done(function (data) {
196+
availableGeos = data.geographic_granularities;
197+
});
198+
return availableGeos;
199+
}
200+
178201
$("#geographic_value").on("select2:select", function (e) {
179202
var geo = e.params.data;
180203
checkGeoCoverage(geo.id).then((notCoveredIndicators) => {
@@ -187,6 +210,14 @@ $("#geographic_value").on("select2:select", function (e) {
187210

188211
$("#showSelectedIndicatorsButton").click(function () {
189212
alertPlaceholder.innerHTML = "";
213+
const availableGeos = getAvailableGeos(checkedIndicatorMembers);
214+
const locationIds = $("#location_search").select2("data").map((item) => item.id);
215+
$("#geographic_value").select2({
216+
data: availableGeos,
217+
minimumInputLength: 0,
218+
maximumSelectionLength: 5,
219+
});
220+
$('#geographic_value').val(locationIds).trigger('change');
190221
if (!indicatorHandler.checkForCovidcastIndicators()) {
191222
$('#geographic_value').val(null).trigger('change');
192223
$("#geographic_value").prop("disabled", true);
@@ -200,11 +231,10 @@ $("#showSelectedIndicatorsButton").click(function () {
200231
}
201232
})
202233
});
203-
var otherEndpointLocationsWarning = `<div class="alert alert-info" data-mdb-alert-init role="alert">` +
204-
` <div>Please, note that some indicator sets may require to select location(s) that is/are different from location(s) above.<br> `
205234
nonCovidcastIndicatorSets = [...new Set(checkedIndicatorMembers.filter(indicator => indicator["_endpoint"] != "covidcast").map((indicator) => indicator["indicator_set"]))];
206-
otherEndpointLocationsWarning += `Different location is required for following Indicator Set(s): ${nonCovidcastIndicatorSets.join(", ")}`
207-
otherEndpointLocationsWarning += `</div></div>`
235+
var otherEndpointLocationsWarning = `<div class="alert alert-info" data-mdb-alert-init role="alert">`
236+
otherEndpointLocationsWarning += `For Indicator Set(s): ${nonCovidcastIndicatorSets.join(", ")}, please use the Location menu below:`
237+
otherEndpointLocationsWarning += `</div>`
208238
if (indicatorHandler.getFluviewIndicators().length > 0) {
209239
$("#differentLocationNote").html(otherEndpointLocationsWarning)
210240
if (document.getElementsByName("fluviewRegions").length === 0) {

src/indicatorsets/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from django.urls import path
22
from django.urls.resolvers import URLPattern
33
from indicatorsets.views import (IndicatorSetListView, epivis,
4-
generate_export_data_url, preview_data, create_query_code)
4+
generate_export_data_url, preview_data, create_query_code, get_available_geos)
55

66
urlpatterns: list[URLPattern] = [
77
path("", IndicatorSetListView.as_view(), name="indicatorsets"),
88
path("epivis/", epivis, name="epivis"),
99
path("export/", generate_export_data_url, name="export"),
1010
path("preview_data/", preview_data, name="preview_data"),
1111
path("create_query_code/", create_query_code, name="create_query_code"),
12+
path("get_available_geos/", get_available_geos, name="get_available_geos"),
1213
]

src/indicatorsets/views.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,42 @@ def create_query_code(request):
540540
{"python_code_blocks": python_code_blocks, "r_code_blocks": r_code_blocks},
541541
safe=False,
542542
)
543+
544+
545+
def get_available_geos(request):
546+
if request.method == "POST":
547+
geo_values = []
548+
data = json.loads(request.body)
549+
indicators = data.get("indicators", [])
550+
grouped_indicators = group_by_property(indicators, "data_source")
551+
for data_source, indicators in grouped_indicators.items():
552+
indicators_str = ",".join(indicator["indicator"] for indicator in indicators)
553+
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))
554+
if response.status_code == 200:
555+
data = response.json()
556+
if len(data["epidata"]):
557+
geo_values.extend(data["epidata"])
558+
unique_values = set(geo_values)
559+
geo_levels = set([el.split(":")[0] for el in unique_values])
560+
geo_unit_ids = set([geo_value.split(":")[1] for geo_value in unique_values])
561+
geographic_granularities = [
562+
{
563+
"id": f"{geo_unit.geo_level.name}:{geo_unit.geo_id}",
564+
"geoType": geo_unit.geo_level.name,
565+
"text": geo_unit.display_name,
566+
"geoTypeDisplayName": geo_unit.geo_level.display_name,
567+
}
568+
for geo_unit in GeographyUnit.objects.filter(geo_level__name__in=geo_levels).filter(geo_id__in=geo_unit_ids)
569+
.prefetch_related("geo_level")
570+
.order_by("level")
571+
]
572+
grouped_geographic_granularities = group_by_property(
573+
geographic_granularities, "geoTypeDisplayName"
574+
)
575+
geographic_granularities = []
576+
for key, value in grouped_geographic_granularities.items():
577+
geographic_granularities.append({
578+
"text": key,
579+
"children": value,
580+
})
581+
return JsonResponse({"geographic_granularities": geographic_granularities}, safe=False)

src/templates/indicatorsets/selectedIndicatorsModal.html

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,9 @@ <h5 class="modal-title" id="selectedIndicatorsModalLabel">Selected indicators</h
144144
</div>
145145
</div>
146146
</div>
147-
<script src="{% static 'js/indicatorHandler.js' %}"></script>
148-
<script src="{% static 'js/selectedIndicatorsModal.js' %}"></script>
149-
<script src="{% static 'js/select2_init.js' %}"></script>
150147
<script>
151-
$(document).ready(function () {
152-
const geoValues = {{ geographic_granularities|safe }};
153-
var urlParams = JSON.parse(JSON.stringify({{ url_params_dict|safe }}));
154-
initSelect2('geographic_value', geoValues);
155-
if (urlParams.location_search != "") {
156-
var locationSearch = urlParams.location_search;
157-
var locationIds = locationSearch.map((item) => item);
158-
$('#geographic_value').val(locationIds).trigger('change');
159-
}
160-
});
161-
148+
const geographicGranularities = {{ geographic_granularities|safe }};
162149
</script>
150+
<script src="{% static 'js/indicatorHandler.js' %}"></script>
151+
<script src="{% static 'js/selectedIndicatorsModal.js' %}"></script>
152+
<script src="{% static 'js/select2_init.js' %}"></script>

0 commit comments

Comments
 (0)