feature: add a search feature

This commit is contained in:
stupidcomputer 2024-07-27 23:49:06 -05:00
parent 25881d4cb3
commit dce8c1b88c
3 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,15 @@
{% extends "explorer/base.html" %}
{% block content %}
<link rel="stylesheet" type="text/css" href="/static/tn.css" />
<div class="boxed">
<h1>Search legislation by keyword</h1>
<form action="/explorer/search" method="get">
<input type="text" id="search_term" name="search_term" />
<button type="submit">Execute search</button>
<p>
You can use this page to search through legislation by keyword. The search is case sensitive, so be careful when pressing your shift key.
</p>
</form>
</div>
{% endblock %}

View File

@ -10,6 +10,7 @@ urlpatterns = [
path("conference/<int:conference_id>/", views.view_conference, name="viewconf"),
path("topics/<int:model_id>/", views.get_all_classified_by_id, name="LegislationClassification.detail"),
path("topics/", views.get_all_classifications, name="LegislationClassification"),
path("search/", views.handle_search, name="search_legislation"),
# these are named weirdly -- see models.py School and Country definitions
path("schools/<int:model_id>/", views.get_all_by_school, name="School.detail"),

View File

@ -130,6 +130,31 @@ def return_groups(request):
print(listing)
return render(request, "explorer/by_group.html", { "listing": listing })
def handle_search(request):
try:
query = request.GET['search_term']
except KeyError:
return render(request, "explorer/search.html", {})
f = LegislativeText.objects.filter
text_results = f(text__icontains=query)
title_results = f(legislation_title__icontains=query)
school_results = f(school__name__icontains=query)
sponsor_results = f(sponsors__name__icontains=query)
country_results = f(country__name__icontains=query)
return render(request, "explorer/results.html", {
"result_name": "Results for search term '{}'".format(query),
"legislation": text_results.union(
title_results,
school_results,
sponsor_results,
country_results
)
})
get_all_by_school = get_all_by_x(School)
get_all_by_country = get_all_by_x(Country)
get_all_by_sponsor = get_all_by_x(Sponsor)