Skip to content

Commit a38fa47

Browse files
authored
Merge pull request #88 from cmu-delphi/OKRS24-118
OKRS24-118 Update filters
2 parents 313eca2 + 1d02634 commit a38fa47

File tree

2 files changed

+107
-13
lines changed

2 files changed

+107
-13
lines changed

src/signals/views.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from signals.filters import SignalFilter
1111
from signals.forms import SignalFilterForm
12-
from signals.models import Pathogen, Signal
12+
from signals.models import Signal
1313
from signals.serializers import SignalSerializer
1414

1515

@@ -38,12 +38,8 @@ def get_url_params(self):
3838
url_params_dict = {
3939
"id": self.request.GET.get("id"),
4040
"search": self.request.GET.get("search"),
41-
"pathogen": [el for el in self.request.GET._getlist("pathogen")]
42-
if self.request.GET.get("pathogen")
43-
else [el.id for el in Pathogen.objects.all()],
44-
"active": [el for el in self.request.GET._getlist("active")]
45-
if self.request.GET.get("active")
46-
else [True, False],
41+
"pathogen": [int(el) for el in self.request.GET._getlist("pathogen")],
42+
"active": [el for el in self.request.GET._getlist("active")],
4743
"available_geography": [
4844
int(el) for el in self.request.GET._getlist("available_geography")
4945
]
@@ -79,6 +75,7 @@ def get_context_data(self, **kwargs) -> Dict[str, Any]:
7975

8076
context: Dict[str, Any] = super().get_context_data(**kwargs)
8177
url_params_dict, url_params_str = self.get_url_params()
78+
context["url_params_dict"] = url_params_dict
8279
context["form"] = SignalFilterForm(initial=url_params_dict)
8380
context["url_params_str"] = url_params_str
8481
context["filter"] = SignalFilter(self.request.GET, queryset=self.get_queryset())

src/templates/signals/signals.html

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,19 @@ <h5 class="modal-title">Pathogen</h5>
4242
</div>
4343
</div>
4444
</div>
45-
{{ form.pathogen|as_crispy_field }}
46-
45+
<div id="div_id_pathogen" class="mb-3">
46+
<div>
47+
{% for choice in form.pathogen.field.choices %}
48+
<div class="form-check">
49+
<input type="checkbox" class="form-check-input" name="pathogen" id="id_pathogen_{{ choice.0 }}" value="{{ choice.0 }}"
50+
{% if choice.0 in url_params_dict.pathogen %}
51+
checked
52+
{% endif %}>
53+
<label for="id_pathogen_{{ choice.0 }}" class="form-check-label">{{ choice.1 }}</label>
54+
</div>
55+
{% endfor %}
56+
</div>
57+
</div>
4758
</div>
4859
<div class="card-body">
4960
<label for="id_active" class="form-label">
@@ -103,7 +114,31 @@ <h5 class="modal-title">Available Geography</h5>
103114
<a role="button">All</a>
104115
<a role="button">Clear</a>
105116
</div>
106-
{{ form.available_geography|as_crispy_field }}
117+
<div id="div_id_available_geography" class="mb-3">
118+
{% for choice in form.available_geography.field.choices %}
119+
<div class="form-check"
120+
{% if forloop.counter > 5 and not url_params_dict.available_geography %}
121+
style="display: none;"
122+
{% endif %}
123+
>
124+
<input type="checkbox" class="form-check-input" name="available_geography" id="id_available_geography_{{ choice.0 }}" value="{{ choice.0 }}"
125+
{% if choice.0 in url_params_dict.available_geography %}
126+
checked
127+
128+
{% endif %}>
129+
<label for="id_available_geography_{{ choice.0 }}" class="form-check-label">{{ choice.1 }}</label>
130+
</div>
131+
{% endfor %}
132+
{% if form.available_geography|length > 5 %}
133+
<a href="" onclick="showAllOptions(event, div_id_available_geography)">
134+
{% if url_params_dict.available_geography %}
135+
Show less...
136+
{% else %}
137+
Show more...
138+
{% endif %}
139+
</a>
140+
{% endif %}
141+
</div>
107142
</div>
108143
<div class="card-body">
109144
<label for="id_signal_type" class="form-label">
@@ -135,7 +170,29 @@ <h5 class="modal-title">Signal Type</h5>
135170
<a role="button">All</a>
136171
<a role="button">Clear</a>
137172
</div>
138-
{{ form.signal_type|as_crispy_field }}
173+
<div id="div_id_signal_type" class="mb-3">
174+
{% for choice in form.signal_type.field.choices %}
175+
<div class="form-check"
176+
{% if forloop.counter > 5 and not url_params_dict.signal_type %}
177+
style="display: none;"
178+
{% endif %}>
179+
<input type="checkbox" class="form-check-input" name="signal_type" id="id_signal_type_{{ choice.0 }}" value="{{ choice.0 }}"
180+
{% if choice.0 in url_params_dict.signal_type %}
181+
checked
182+
{% endif %}>
183+
<label for="id_signal_type_{{ choice.0 }}" class="form-check-label">{{ choice.1 }}</label>
184+
</div>
185+
{% endfor %}
186+
{% if form.signal_type|length > 5 %}
187+
<a href="" onclick="showAllOptions(event, div_id_signal_type)">
188+
{% if url_params_dict.signal_type %}
189+
Show less...
190+
{% else %}
191+
Show more...
192+
{% endif %}
193+
</a>
194+
{% endif %}
195+
</div>
139196
</div>
140197
<div class="card-body">
141198
<label for="id_category" class="form-label">
@@ -231,7 +288,30 @@ <h5 class="modal-title">Source</h5>
231288
<a role="button">All</a>
232289
<a role="button">Clear</a>
233290
</div>
234-
{{ form.source|as_crispy_field }}
291+
<div id="div_id_source" class="mb-3">
292+
{% for choice in form.source.field.choices %}
293+
<div class="form-check"
294+
{% if forloop.counter > 5 and not url_params_dict.source %}
295+
style="display: none;"
296+
{% endif %}
297+
>
298+
<input type="checkbox" class="form-check-input" name="source" id="id_source_{{ choice.0 }}" value="{{ choice.0 }}"
299+
{% if choice.0 in url_params_dict.source %}
300+
checked
301+
{% endif %}>
302+
<label for="id_source_{{ choice.0 }}" class="form-check-label">{{ choice.1 }}</label>
303+
</div>
304+
{% endfor %}
305+
{% if form.source|length > 5 %}
306+
<a href="" onclick="showAllOptions(event, div_id_source)">
307+
{% if url_params_dict.source %}
308+
Show less...
309+
{% else %}
310+
Show more...
311+
{% endif %}
312+
</a>
313+
{% endif %}
314+
</div>
235315
</div>
236316
<div class="card-body">
237317
<label for="id_time_label" class="form-label">
@@ -308,9 +388,26 @@ <h5 class="modal-title">Time Label</h5>
308388

309389
<script>
310390

391+
function showAllOptions(event, parentDiv) {
392+
event.preventDefault();
393+
var childDivs = parentDiv.querySelectorAll('.form-check');
394+
for (i = 5; i < childDivs.length; i++) {
395+
if (childDivs[i].style.display === 'none') {
396+
childDivs[i].style.display = 'block';
397+
} else {
398+
childDivs[i].style.display = 'none';
399+
}
400+
}
401+
if (event.target.innerText === 'Show more...') {
402+
event.target.innerText = 'Show less...';
403+
} else {
404+
event.target.innerText = 'Show more...';
405+
}
406+
}
407+
311408
// Add an event listener to the 'order_by' select element
312409
document.getElementById('id_order_by').addEventListener('change', function() {
313-
document.getElementById('filters-form').submit();
410+
document.getElementById('x-form').submit();
314411
});
315412

316413

0 commit comments

Comments
 (0)