Skip to content

Commit d05b98d

Browse files
authored
Better error handling for dates, added submitted filter criteria (#4549)
Fixes #4548. This handles an invalid date the same as invalid/non-existent query params rather than erroring out. ~~In the long term, it would be better to indicate to the user that something wasn't right with their query though or no results were brought.~~ When an invalid query is entered, it'll show the no results prompt! I also added the submitted drop down filter, as staff have been using that a bit recently
1 parent ddd37e6 commit d05b98d

4 files changed

Lines changed: 38 additions & 22 deletions

File tree

hypha/apply/funds/templates/submissions/all.html

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,17 @@
365365
<nav x-show="!showSelectedSubmissions"
366366
class="flex flex-wrap gap-2 items-center menu-filters"
367367
>
368-
<div id="filterupdated" aria-label="Filter by Updated" class="flex items-center">
368+
<div id="filtersubmitted" aria-label="Filter by Submitted" class="flex items-center" data-query="submitted">
369+
<button class="flex justify-between items-center py-1 w-full font-medium text-gray-600 border cursor-pointer md:p-0 md:border-none hover:bg-gray-50 ps-2 pe-2 md:pe-4 md:hover:bg-transparent md:hover:text-blue-700">
370+
{% trans "Submitted" %}
371+
{% heroicon_mini "chevron-down" aria_hidden="true" width=18 height=18 class="hidden md:inline-block" %}
372+
</button>
373+
</div>
374+
<div id="filterupdated" aria-label="Filter by Updated" class="flex items-center" data-query="updated">
369375
<button class="flex justify-between items-center py-1 w-full font-medium text-gray-600 border cursor-pointer md:p-0 md:border-none hover:bg-gray-50 ps-2 pe-2 md:pe-4 md:hover:bg-transparent md:hover:text-blue-700">
370376
{% trans "Updated" %}
371377
{% heroicon_mini "chevron-down" aria_hidden="true" width=18 height=18 class="hidden md:inline-block" %}
372378
</button>
373-
374379
</div>
375380

376381
{% dropdown_menu title="Status" heading="Filter by current status" enable_search=True %}
@@ -609,23 +614,28 @@ <h2 class='text-2xl'>{% trans "No results matched your search" %}</h2>
609614
var start = moment().subtract(29, 'days');
610615
var end = moment();
611616

612-
$('#filterupdated').daterangepicker({
613-
startDate: start,
614-
endDate: end,
615-
ranges: {
616-
'{% trans "Today" %}': [moment(), moment()],
617-
'{% trans "Yesterday" %}': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
618-
'{% trans "Last 7 Days" %}': [moment().subtract(6, 'days'), moment()],
619-
'{% trans "Last 30 Days" %}': [moment().subtract(29, 'days'), moment()],
620-
'{% trans "This Month" %}': [moment().startOf('month'), moment().endOf('month')],
621-
'{% trans "Last Month" %}': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
622-
}
623-
});
624-
625-
$('#filterupdated').on('apply.daterangepicker', function(ev, picker) {
626-
$('#search-navbar').val(`updated:>=${picker.startDate.format('YYYY-MM-DD')} updated:<=${picker.endDate.format('YYYY-MM-DD')}`);
627-
$('#search-navbar').closest('form').trigger('submit');
628-
});
617+
// Add the picker for all elements that need it
618+
$.each(['#filterupdated', '#filtersubmitted'], (index, element) => {
619+
$(element).daterangepicker({
620+
startDate: start,
621+
endDate: end,
622+
ranges: {
623+
'{% trans "Today" %}': [moment(), moment()],
624+
'{% trans "Yesterday" %}': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
625+
'{% trans "Last 7 Days" %}': [moment().subtract(6, 'days'), moment()],
626+
'{% trans "Last 30 Days" %}': [moment().subtract(29, 'days'), moment()],
627+
'{% trans "This Month" %}': [moment().startOf('month'), moment().endOf('month')],
628+
'{% trans "Last Month" %}': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
629+
}
630+
});
631+
632+
var query = $(element).attr("data-query");
633+
634+
$(element).on('apply.daterangepicker', function(ev, picker) {
635+
$('#search-navbar').val(`${query}:>=${picker.startDate.format('YYYY-MM-DD')} ${query}:<=${picker.endDate.format('YYYY-MM-DD')}`);
636+
$('#search-navbar').closest('form').trigger('submit');
637+
});
638+
})
629639
});
630640
</script>
631641
{% endblock %}

hypha/apply/search/filters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def apply_date_filter(qs, field, values):
1414

1515
if q := date_filter_tokens_to_q_obj(tokens=tokens, field=field):
1616
q_obj &= q
17+
else:
18+
return qs.none()
1719

1820
return qs.filter(q_obj)
1921

@@ -66,4 +68,4 @@ def date_filter_tokens_to_q_obj(tokens: list, field: str) -> Q:
6668
return Q(**{f"{field}__year": year})
6769
else:
6870
return Q(**{f"{field}__year": year})
69-
return Q(None)
71+
return None

hypha/apply/search/query_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ def tokenize_date_filter_value(date_str: str) -> list:
8787
# Match the regex pattern to the value
8888
match = re.match(regex_pattern, date_str)
8989

90+
if match is None:
91+
# Invalid date string
92+
return []
93+
9094
# Extract the operator and date from the match object
9195
operator = match.group(1)
9296
date_str = match.group(2)

hypha/apply/search/tests/test_filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
(["<", 2023], "date_field", Q(date_field__year__lt=2023)),
4040
([None, 2023], "date_field", Q(date_field__year=2023)),
4141
([None, 2023], "date_field", Q(date_field__year=2023)),
42-
([], "date_field", Q(None)),
43-
([], "date_field", Q(None)),
42+
([], "date_field", None),
43+
([], "date_field", None),
4444
],
4545
)
4646
def test_date_filter_tokens_to_q_obj(tokens, field, expected):

0 commit comments

Comments
 (0)