Skip to content

Commit

Permalink
chore: make search more pythonic
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGrace2282 committed Oct 4, 2024
1 parent 5681005 commit 3553b9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 12 additions & 14 deletions intranet/apps/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions intranet/templates/dashboard/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@
<h2>{{ dashboard_header }}</h2>
<span class="announcements-icon-wrapper" style="display: flex; flex-direction: row;">
{% if view_announcements_url == "club_announcements" %}
<form id="subscribed-announcement-search" style="display: flex; margin-right:1px">
<input id="subscribed-announcement-search-input" type="text" placeholder="Search Club Announcements" style="width: 15rem;" class="dashboard-textinput" onsubmit="" >
</form>
<!-- TODO(2027adeshpan): handle searches containing # -->
<form id="subscribed-announcement-search" style="display: flex; margin-right:1px" method="get" role="search">
<input id="subscribed-announcement-search-input"
type="search" placeholder="Search Club Announcements" style="width: 15rem" class="dashboard-textinput">
</form>
{% endif %}

{% if show_expired or not show_widgets %}
Expand Down

0 comments on commit 3553b9e

Please sign in to comment.