summaryrefslogtreecommitdiff
path: root/search
diff options
context:
space:
mode:
authorJonathan DeMasi <jonathan.demasi@colorado.edu>2020-03-02 15:31:38 -0700
committerJonathan DeMasi <jonathan.demasi@colorado.edu>2020-03-02 15:31:38 -0700
commit4872b099ccd2ddd4e6baa31b7a2b3cf391237a7a (patch)
tree5bb2626573e30aeb9fe6fddab490170ebe86e43e /search
parent8234155d407d0bfd1f6d6218fde59b09337294cb (diff)
downloadjthanio-4872b099ccd2ddd4e6baa31b7a2b3cf391237a7a.tar
jthanio-4872b099ccd2ddd4e6baa31b7a2b3cf391237a7a.tar.gz
jthanio-4872b099ccd2ddd4e6baa31b7a2b3cf391237a7a.tar.bz2
jthanio-4872b099ccd2ddd4e6baa31b7a2b3cf391237a7a.tar.lz
jthanio-4872b099ccd2ddd4e6baa31b7a2b3cf391237a7a.tar.xz
jthanio-4872b099ccd2ddd4e6baa31b7a2b3cf391237a7a.tar.zst
jthanio-4872b099ccd2ddd4e6baa31b7a2b3cf391237a7a.zip
init wagtail
Diffstat (limited to 'search')
-rw-r--r--search/__init__.py0
-rw-r--r--search/templates/search/search.html38
-rw-r--r--search/views.py34
3 files changed, 72 insertions, 0 deletions
diff --git a/search/__init__.py b/search/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/search/__init__.py
diff --git a/search/templates/search/search.html b/search/templates/search/search.html
new file mode 100644
index 0000000..5f222e5
--- /dev/null
+++ b/search/templates/search/search.html
@@ -0,0 +1,38 @@
+{% extends "base.html" %}
+{% load static wagtailcore_tags %}
+
+{% block body_class %}template-searchresults{% endblock %}
+
+{% block title %}Search{% endblock %}
+
+{% block content %}
+ <h1>Search</h1>
+
+ <form action="{% url 'search' %}" method="get">
+ <input type="text" name="query"{% if search_query %} value="{{ search_query }}"{% endif %}>
+ <input type="submit" value="Search" class="button">
+ </form>
+
+ {% if search_results %}
+ <ul>
+ {% for result in search_results %}
+ <li>
+ <h4><a href="{% pageurl result %}">{{ result }}</a></h4>
+ {% if result.search_description %}
+ {{ result.search_description }}
+ {% endif %}
+ </li>
+ {% endfor %}
+ </ul>
+
+ {% if search_results.has_previous %}
+ <a href="{% url 'search' %}?query={{ search_query|urlencode }}&amp;page={{ search_results.previous_page_number }}">Previous</a>
+ {% endif %}
+
+ {% if search_results.has_next %}
+ <a href="{% url 'search' %}?query={{ search_query|urlencode }}&amp;page={{ search_results.next_page_number }}">Next</a>
+ {% endif %}
+ {% elif search_query %}
+ No results found
+ {% endif %}
+{% endblock %}
diff --git a/search/views.py b/search/views.py
new file mode 100644
index 0000000..eeace7c
--- /dev/null
+++ b/search/views.py
@@ -0,0 +1,34 @@
+from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
+from django.shortcuts import render
+
+from wagtail.core.models import Page
+from wagtail.search.models import Query
+
+
+def search(request):
+ search_query = request.GET.get('query', None)
+ page = request.GET.get('page', 1)
+
+ # Search
+ if search_query:
+ search_results = Page.objects.live().search(search_query)
+ query = Query.get(search_query)
+
+ # Record hit
+ query.add_hit()
+ else:
+ search_results = Page.objects.none()
+
+ # Pagination
+ paginator = Paginator(search_results, 10)
+ try:
+ search_results = paginator.page(page)
+ except PageNotAnInteger:
+ search_results = paginator.page(1)
+ except EmptyPage:
+ search_results = paginator.page(paginator.num_pages)
+
+ return render(request, 'search/search.html', {
+ 'search_query': search_query,
+ 'search_results': search_results,
+ })