Skip to content
Snippets Groups Projects
Commit f34dbc92 authored by Todd Dembrey's avatar Todd Dembrey
Browse files

Add basic search using Postgres for submitted data

parent d1cd7c54
No related branches found
No related tags found
No related merge requests found
...@@ -2,12 +2,15 @@ ...@@ -2,12 +2,15 @@
{% load render_table from django_tables2 %} {% load render_table from django_tables2 %}
{% block title %}OTF Dashboard{% endblock %} {% block title %}OTF Dashboard{% endblock %}
{% block content %} {% block content %}
<div class="wrapper wrapper--breakout wrapper--admin"> <div class="wrapper wrapper--breakout wrapper--admin">
<div class="wrapper wrapper--large"> <div class="wrapper wrapper--large">
<h3>Received submissions</h3> {% block page_header %}
<h3>Received Submissions</h3>
<h5>Track and explore recent submissions</h5> <h5>Track and explore recent submissions</h5>
</div> {% endblock %}
</div> </div>
{% include "dashboard/includes/search.html" %}
</div>
<div class="wrapper wrapper--medium wrapper--top-bottom-inner-space"> <div class="wrapper wrapper--medium wrapper--top-bottom-inner-space">
{% if filter %} {% if filter %}
......
<form action="{% url 'dashboard:search' %}" method="get" role="search" class="form form--header-search-desktop">
<button class="button" type="submit" aria-label="Search">
<svg class="icon icon--magnifying-glass icon--search"><use xlink:href="#magnifying-glass"></use></svg>
</button>
<input class="input input--transparent input--secondary" type="text" placeholder="Search…" name="query"{% if search_query %} value="{{ search_query }}{% endif %}" aria-label="Search input">
</form>
{% extends "dashboard/dashboard.html" %}
{% block page_header %}
<h3>Search Results</h3>
<h5>There are {{ object_list|length }} results for: {{ search_term }}</h5>
{% endblock %}
from django.conf.urls import url from django.conf.urls import url
from .views import DashboardView from .views import DashboardView, SearchView
urlpatterns = [ urlpatterns = [
url(r'^$', DashboardView.as_view(), name="dashboard"), url(r'^$', DashboardView.as_view(), name="dashboard"),
url(r'^search$', SearchView.as_view(), name="search"),
] ]
from django.contrib.postgres.search import SearchVector
from django.db.models import TextField
from django.db.models.functions import Cast
from django.views.generic import DetailView from django.views.generic import DetailView
from django_filters.views import FilterView from django_filters.views import FilterView
...@@ -18,3 +21,21 @@ class DashboardView(SingleTableMixin, FilterView): ...@@ -18,3 +21,21 @@ class DashboardView(SingleTableMixin, FilterView):
class SubmissionDetailView(DetailView): class SubmissionDetailView(DetailView):
model = ApplicationSubmission model = ApplicationSubmission
class SearchView(SingleTableMixin, FilterView):
template_name = 'dashboard/search.html'
table_class = DashboardTable
filterset_class = SubmissionFilter
def get_context_data(self, **kwargs):
search_term = self.request.GET.get('query')
if search_term:
# Postgres <10 doesn't support search on JSON
# Cast to text to make searchable
self.object_list = self.object_list.annotate(
search=SearchVector(Cast('form_data', TextField())),
).filter(search=search_term)
return super().get_context_data(search_term=search_term, **kwargs)
...@@ -67,6 +67,7 @@ INSTALLED_APPS = [ ...@@ -67,6 +67,7 @@ INSTALLED_APPS = [
'django.contrib.contenttypes', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.postgres',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.sitemaps', 'django.contrib.sitemaps',
'django.forms', 'django.forms',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment