Skip to content

Commit d01ecd5

Browse files
authored
Merge pull request #109 from cmu-delphi/OKRS24-198
Okrs24 198
2 parents e1ebbca + ebc8500 commit d01ecd5

File tree

4 files changed

+72
-103
lines changed

4 files changed

+72
-103
lines changed

src/signals/serializers.py

-11
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,3 @@ class Meta:
3131
model = Signal
3232
fields = '__all__'
3333

34-
35-
class GeographyUnitSerialializer(ModelSerializer):
36-
"""
37-
Serializer for the GeographyUnit model.
38-
"""
39-
40-
category = SlugRelatedField(read_only=True, slug_field='name')
41-
42-
class Meta:
43-
model = GeographyUnit
44-
fields = '__all__'

src/signals/urls.py

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
SignalsDetailView,
66
SignalsListApiView,
77
SignalsListView,
8-
GeographyUnitListApiView
98
)
109

1110
urlpatterns: list[URLPattern] = [
@@ -15,5 +14,4 @@
1514

1615
# REST API
1716
path('api/v1/signals/', SignalsListApiView.as_view(), name='signals_api'),
18-
path('api/v1/geography_units/', GeographyUnitListApiView.as_view(), name='geography_units_api')
1917
]

src/signals/views.py

+2-32
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
from django.conf import settings
44
from django.views.generic import DetailView, ListView
5-
from django.db.models import Q
65
from django_filters.rest_framework import DjangoFilterBackend
76
from rest_framework.filters import SearchFilter
87
from rest_framework.generics import ListAPIView
98

109
from signals.filters import SignalFilter
1110
from signals.forms import SignalFilterForm
12-
from signals.models import Signal, GeographyUnit
13-
from signals.serializers import SignalSerializer, GeographyUnitSerialializer
11+
from signals.models import Signal
12+
from signals.serializers import SignalSerializer
1413

1514

1615
class SignalsListView(ListView):
@@ -127,32 +126,3 @@ class SignalsListApiView(ListAPIView):
127126
"source__name",
128127
"time_label",
129128
)
130-
131-
132-
class GeographyUnitListApiView(ListAPIView):
133-
"""
134-
ListAPIView for retrieving a list of Signal objects via API.
135-
"""
136-
137-
queryset = GeographyUnit.objects.all()
138-
serializer_class = GeographyUnitSerialializer
139-
search_fields = ("name")
140-
filter_backends = [DjangoFilterBackend]
141-
filterset_fields = (
142-
"name",
143-
"display_name",
144-
"geography__name"
145-
)
146-
147-
def get_queryset(self):
148-
search_term = self.request.GET.get('term')
149-
if search_term:
150-
queries: list[Q] = []
151-
for field in ['name', 'display_name']:
152-
queries.append(Q(**{f'{field}__icontains': search_term}))
153-
query = queries.pop()
154-
155-
for item in queries:
156-
query |= item
157-
return GeographyUnit.objects.filter(query)
158-
return super().get_queryset()

src/templates/signals/epivis_export_data_block.html

+70-58
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ <h5>Plot / Export data</h5>
44
<div class="card">
55
<div class="card-body">
66
<form class="margin-top-1rem" onsubmit="submitMode(event)" id="epivis-form">
7+
8+
<div class="alert alert-warning alert-dismissible fade show" role="alert" id="warning-alert">
9+
<strong>Holy guacamole!</strong> You should check in on some of those fields below.
10+
</div>
11+
712
<div class="row">
813
<div class="col-2">
914
<label class="form-label"
@@ -99,71 +104,46 @@ <h5>Plot / Export data</h5>
99104
</div>
100105
</section>
101106
<script>
102-
function initGeographicValueSelect(mode) {
103-
var maxSelectionLength = 1;
107+
var geoValues = [];
108+
var currentMode = 'epivis';
109+
110+
function getFilteredGeographicValues(geographicType) {
111+
var data = geoValues.reduce((data, geoValue) => {
112+
if (geoValue.geoType === geographicType) {
113+
data.push(geoValue);
114+
}
115+
return data;
116+
}, []);
117+
return data;
118+
}
119+
120+
function initGeographicValueSelect(mode, geographicType = null) {
121+
var maximumSelectionLength = 1;
104122
if (mode === 'epivis') {
105-
maxSelectionLength = 1;
123+
maximumSelectionLength = 1;
124+
} else {
125+
maximumSelectionLength = 10;
126+
}
127+
128+
if (geographicType) {
129+
var data = getFilteredGeographicValues(geographicType);
106130
} else {
107-
maxSelectionLength = 10;
131+
data = [];
108132
}
133+
134+
109135
$('#geographic_value').select2({
110-
ajax: {
111-
url: '{% url 'geography_units_api' %}',
112-
dataType: 'json',
113-
data: function (params) {
114-
var geographic_type = document.getElementById('geographic_type');
115-
return {
116-
geography__name: geographic_type.value,
117-
term: params.term,
118-
limit: 50
119-
};
120-
},
121-
processResults: function (data) {
122-
return {
123-
results: $.map(data.results, function (item) {
124-
if (typeof item.geo_id === 'string') {
125-
return {
126-
text: item.display_name,
127-
id: item.geo_id.toLowerCase()
128-
}
129-
} else {
130-
return {
131-
text: item.display_name,
132-
id: item.geo_id
133-
}
134-
}
135-
})
136-
};
137-
}
138-
},
136+
data: data,
139137
minimumInputLength: 0,
140-
maximumSelectionLength: maxSelectionLength,
138+
maximumSelectionLength: maximumSelectionLength,
141139
});
142140
}
143141

144-
$(document).ready(function () {
145-
initGeographicValueSelect('epivis');
146-
});
147-
148142
document.getElementById('geographic_type').addEventListener("change", (event) => {
149-
$('#geographic_value').val(null).trigger('change');
143+
$('#geographic_value').empty();
144+
initGeographicValueSelect(currentMode, event.target.value);
150145
})
151146

152-
document.getElementsByName('modes').forEach((el) => {
153-
el.addEventListener('change', (event) => {
154-
$('#geographic_value').val(null).trigger('change');
155-
//$('#geographic_value').select2('destroy');
156-
if (event.target.value === 'epivis') {
157-
initGeographicValueSelect(event.target.value);
158-
} else {
159-
initGeographicValueSelect('export');
160-
}
161-
handleModeChange(event.target.value);
162-
});
163-
});
164-
165-
var currentMode = 'epivis';
166-
167147
function handleModeChange(mode) {
168148
document.getElementById("epivis-form").reset();
169149

@@ -186,17 +166,49 @@ <h5>Plot / Export data</h5>
186166
});
187167
}
188168

169+
document.getElementsByName('modes').forEach((el) => {
170+
el.addEventListener('change', (event) => {
171+
$('#geographic_value').empty();
172+
currentMode = event.target.value;
173+
initGeographicValueSelect(event.target.value)
174+
handleModeChange(event.target.value);
175+
});
176+
});
177+
178+
179+
$(document).ready(function () {
180+
{% for geography in signal.available_geography.all %}
181+
{% for unit in geography.geography_units.all %}
182+
geoValues.push({'id': '{{ unit.geo_id }}', 'geoType': '{{ unit.geography }}', 'text': '{{ unit.display_name }}'});
183+
{% endfor %}
184+
{% endfor %}
185+
186+
$('#geographic_value').select2({
187+
data: [],
188+
minimumInputLength: 0,
189+
maximumSelectionLength: 1,
190+
});
191+
});
192+
193+
$("#warning-alert").hide();
194+
195+
function showWarningAlert(warningMessage) {
196+
$("#warning-alert").html(warningMessage);
197+
$("#warning-alert").fadeTo(2000, 500).slideUp(1000, function() {
198+
$("#warning-alert").slideUp(1000);
199+
});
200+
}
189201

190202
function submitMode(event) {
191203
event.preventDefault();
192-
204+
193205
var dataSource = document.getElementById('source').value;
194206
var dataSignal = document.getElementById('signal').value;
195207
var geographicType = document.getElementById('geographic_type').value;
196208
var geographicValue = $('#geographic_value').select2('data').map((el) => el.id).join(',');
197209

198210
if (geographicType === 'Choose...' || geographicValue === '') {
199-
alert('Please select Geographic Type and Geographic Value!');
211+
showWarningAlert("Geographic Type or Geographic Value is not selected.");
200212
return;
201213
}
202214

@@ -206,20 +218,20 @@ <h5>Plot / Export data</h5>
206218

207219
epiVisUrl += `#${urlParamsEncoded}`;
208220
window.open(epiVisUrl, '_blank').focus();
209-
210221
} else {
211222
var startDate = document.getElementById('start_date').value;
212223
var endDate = document.getElementById('end_date').value;
213224

214225
var dataExportUrl = "{{ data_export_url }}";
215226

216227
if (startDate === '' || endDate === '') {
217-
alert('Please select start and end date');
228+
showWarningAlert("Start Date or End Date is not selected.")
218229
return;
219230
}
220231

221232
dataExportUrl += `?signal=${dataSource}:${dataSignal}&start_day=${startDate}&end_day=${endDate}&geo_type=${geographicType}&geo_values=${geographicValue}`;
222233
window.open(dataExportUrl, '_blank').focus();
223234
}
224235
}
236+
225237
</script>

0 commit comments

Comments
 (0)