diff --git a/intranet/apps/dashboard/views.py b/intranet/apps/dashboard/views.py index 6b78768ea6..afe87da7f7 100644 --- a/intranet/apps/dashboard/views.py +++ b/intranet/apps/dashboard/views.py @@ -331,6 +331,27 @@ class RawPaginationData(TypedDict, Generic[T]): page_obj: Paginator[T] +def filter_announcements_by_search(request, items): + if "search" not in request.GET: + 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() + + filteredItems = list(filter(filterMethod, items)) + + sorted_announcements = sorted( + filteredItems, + key=lambda announcement: (searchParam.lower() not in announcement.title.lower(), searchParam.lower() not in announcement.content.lower()), + ) + + return sorted_announcements + + def paginate_announcements_list_raw( request: HttpRequest, items: Sequence[T], @@ -358,6 +379,8 @@ def paginate_announcements_list_raw( else: page_num = DEFAULT_PAGE_NUM + items = filter_announcements_by_search(request, items) + paginator = Paginator(items, 15) if page_num not in paginator.page_range: page_num = DEFAULT_PAGE_NUM diff --git a/intranet/static/js/dashboard/announcements.js b/intranet/static/js/dashboard/announcements.js index 3fb827865e..7fac1b1ee6 100644 --- a/intranet/static/js/dashboard/announcements.js +++ b/intranet/static/js/dashboard/announcements.js @@ -1,5 +1,5 @@ /* global $ */ -$(document).ready(function() { +$(document).ready(function () { updatePartiallyHidden(); filterClubAnnouncements(); @@ -62,6 +62,12 @@ $(document).ready(function() { filterClubAnnouncements(); }); + $("#subscribed-announcement-search").submit((e) => { + e.preventDefault(); + const value = $("#subscribed-announcement-search-input").val(); + window.location.replace("/announcements/club?search=" + value); + }) + }); function updatePartiallyHidden() { diff --git a/intranet/templates/dashboard/dashboard.html b/intranet/templates/dashboard/dashboard.html index 83bbeaf496..d3228c331c 100644 --- a/intranet/templates/dashboard/dashboard.html +++ b/intranet/templates/dashboard/dashboard.html @@ -102,11 +102,16 @@