diff --git a/intranet/apps/dashboard/views.py b/intranet/apps/dashboard/views.py index afe87da7f7..d82b256ee5 100644 --- a/intranet/apps/dashboard/views.py +++ b/intranet/apps/dashboard/views.py @@ -331,25 +331,23 @@ class RawPaginationData(TypedDict, Generic[T]): page_obj: Paginator[T] -def filter_announcements_by_search(request, items): - if "search" not in request.GET: +def filter_announcements_by_search(request, items) -> list[Announcement]: + query = request.GET.get("search", "").strip().lower() + if not query: return items - searchParam = request.GET["search"] - if len(searchParam.strip()) == 0: - return items - searchParam = searchParam.lower() - def filterMethod(item): - return searchParam in item.title.lower() or searchParam in item.content.lower() + def search(item): + return query in item.title.lower() or query in item.content.lower() - filteredItems = list(filter(filterMethod, items)) + filtered = [item for item in items if search(item)] - sorted_announcements = sorted( - filteredItems, - key=lambda announcement: (searchParam.lower() not in announcement.title.lower(), searchParam.lower() not in announcement.content.lower()), - ) + # order by title then content + def order_by(item): + return (query in item.title.lower(), query in item.content.lower()) + + filtered.sort(key=order_by, reverse=True) - return sorted_announcements + return filtered def paginate_announcements_list_raw( diff --git a/intranet/templates/dashboard/dashboard.html b/intranet/templates/dashboard/dashboard.html index d3228c331c..964eb2b5a0 100644 --- a/intranet/templates/dashboard/dashboard.html +++ b/intranet/templates/dashboard/dashboard.html @@ -107,9 +107,11 @@

{{ dashboard_header }}

{% if view_announcements_url == "club_announcements" %} - + + {% endif %} {% if show_expired or not show_widgets %}