Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #338 from TakwimuAfrica/feature/search_backend
Browse files Browse the repository at this point in the history
Add Search
  • Loading branch information
kilemensi authored Aug 20, 2018
2 parents b05eacc + 1bb5079 commit dc66680
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 8 deletions.
12 changes: 11 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,31 @@ services:
- POSTGRES_PASSWORD=takwimu
- POSTGRES_DB=takwimu

es:
image: elasticsearch:5.6.10
ports:
- "9200:9200"

web:
build: .
ports:
- "8000:8000"
depends_on:
- db
- es
volumes:
- "./:/srv/takwimu"
environment:
- DATABASE_URL=postgresql://takwimu:takwimu@db:5432/takwimu
- DJANGO_SECRET_KEY=somethingsecret
- HURUMAP_URL='http://localhost:8000'
- HURUMAP_URL=http://localhost:8000
- PGHOST=db
- PGDATABASE=takwimu
- PGUSER=takwimu
- PGPASSWORD=takwimu
- TAKWIMU_ES_INDEX=takwimu
# A lot of ConnectionReadTimeout errors, when value this is low
- TAKWIMU_ES_TIMEOUT=30
- TAKWIMU_ES_URL=http://es:9200
- PYTHONDONTWRITEBYTECODE="True"
# - DJANGO_DEBUG=False # For testing deploys
1 change: 1 addition & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ python manage.py migrate --noinput # Apply database migrations
cat takwimu/sql/*.sql | psql # Upload tables / data
python manage.py compilescss # Compile SCSS (offline)
python manage.py collectstatic --noinput # Collect static files
python manage.py update_index # Update search index

# Prepare log files and start outputting logs to stdout
touch /srv/logs/gunicorn.log
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ git+https://github.com/CodeForAfricaLabs/django-fontawesome.git@master#egg=djang
Markdown==2.6.11

django-debug-toolbar

elasticsearch==5.5.3

requests-aws4auth==0.9

13 changes: 9 additions & 4 deletions takwimu/models/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class TopicPageDataIndicators(Orderable, TopicPageDataIndicator):


class TopicPage(Page):
'''
"""
Topic Editor
------------
All data indicators are made available to profiles + sections via topics.
This therefore serves as an editorial interface to create topics and link indicators to it.
'''
"""
description = models.TextField(blank=True)
icon = IconField() #adding icon property that uses the djano-fontawesome app structure

Expand All @@ -64,7 +64,6 @@ class TopicPage(Page):
# Search index configuration

search_fields = Page.search_fields + [
index.SearchField('title'),
index.SearchField('description')
]

Expand Down Expand Up @@ -361,13 +360,19 @@ class ExplainerSteps(Page):
StreamFieldPanel('steps'),
]

class FAQ(models.Model):

class FAQ(index.Indexed, models.Model):
question = models.TextField()
answer = RichTextField()
cta_one_url = models.URLField("'Find Out More' button URL", default="https://takwimu.zendesk.com/")
cta_two_name = models.TextField("Second button Name (optional)", blank=True)
cta_two_url = models.URLField("Second button URL (optional)", blank=True)

search_fields = [
index.SearchField('question'),
index.SearchField('answer'),
]

def __str__(self):
return self.question.encode('ascii', 'ignore')

Expand Down
48 changes: 48 additions & 0 deletions takwimu/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# coding=utf-8
import os

from elasticsearch import RequestsHttpConnection
from requests_aws4auth import AWS4Auth

from hurumap.settings import * # noqa

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Expand Down Expand Up @@ -110,3 +113,48 @@
ZENDESK_API = 'https://takwimu.zendesk.com/api/v2/requests.json'

ZENDESK_API_TOKEN = os.environ.get('ZENDESK_API_TOKEN')

# -------------------------------------------------------------------------------------
# WAGTAIL Search
# -------------------------------------------------------------------------------------


TAKWIMU_ES_INDEX = os.environ.get('TAKWIMU_ES_INDEX', 'takwimu')
TAKWIMU_ES_TIMEOUT = int(os.environ.get('TAKWIMU_ES_TIMEOUT', '5'))
TAKWIMU_ES_URL = os.environ.get('TAKWIMU_ES_URL', 'http://localhost:9200')

# Support for AWS ElasticSearch service. If HOST_TYPE is anything other than
# 'AWS', the default configuration will be used.
TAKWIMU_ES_HOST_TYPE = os.environ.get('TAKWIMU_ES_HOST_TYPE', '')
if TAKWIMU_ES_HOST_TYPE.lower() == 'aws':
TAKWIMU_ES_AWS_ACCESS_KEY = os.environ.get('TAKWIMU_ES_AWS_ACCESS_KEY', '')
TAKWIMU_ES_AWS_SECRET_KEY = os.environ.get('TAKWIMU_ES_AWS_SECRET_KEY', '')
TAKWIMU_ES_AWS_REGION = os.environ.get('TAKWIMU_ES_AWS_REGION', '')
WAGTAILSEARCH_BACKENDS = {
'default': {
'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch5',
'INDEX': TAKWIMU_ES_INDEX,
'TIMEOUT': TAKWIMU_ES_TIMEOUT,
'HOSTS': [{
'host': TAKWIMU_ES_URL,
'port': 443,
'use_ssl': True,
'verify_certs': True,
'http_auth': AWS4Auth(TAKWIMU_ES_AWS_ACCESS_KEY, TAKWIMU_ES_AWS_SECRET_KEY, TAKWIMU_ES_AWS_REGION, 'es'),
}],
'OPTIONS': {
'connection_class': RequestsHttpConnection,
},
}
}
else:
WAGTAILSEARCH_BACKENDS = {
'default': {
'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch5',
'INDEX': TAKWIMU_ES_INDEX,
'TIMEOUT': TAKWIMU_ES_TIMEOUT,
'URLS': [TAKWIMU_ES_URL],
'OPTIONS': {},
'INDEX_SETTINGS': {},
}
}
35 changes: 35 additions & 0 deletions takwimu/templates/search_results.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends 'takwimu/_layouts/page.html' %}
{% load wagtailcore_tags %}
{% load staticfiles %}

{% block content %}

{% include 'takwimu/_includes/homepage/hero.html' %}

<div class="container">
<div class="m-2">
<form action="{% url 'search' %}" method="get">
<input type="text" name="q" value="{{ search_query }}" class="form-control">
<input type="submit" value="Search" class="btn btn-primary">
</form>
</div>

{% 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|safe }}
{% endif %}
</li>
{% endfor %}
</ul>
{% elif search_query %}
No results found
{% else %}
Please type something into the search box
{% endif %}
</div>

{% endblock %}
3 changes: 2 additions & 1 deletion takwimu/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.views.generic import RedirectView

from takwimu import settings
from takwimu.views import HomePageView, SupportServicesIndexView, AboutUsView, LegalView, TopicView
from takwimu.views import HomePageView, SupportServicesIndexView, AboutUsView, LegalView, TopicView, SearchView
from takwimu.views import handler404, handler500
from wazimap.views import HomepageView as ProfileView
from takwimu.feed import CountryProfileFeed
Expand All @@ -20,6 +20,7 @@
url(r'^profiles/$', ProfileView.as_view(), name='profiles'),
url(r'^topics/$', TopicView.as_view(), name='topics'),
url(r'^feed/$', CountryProfileFeed(), name='rss_feed'),
url(r'^search/$', SearchView.as_view(), name='search'),
] + \
hurumap_urlpatterns

Expand Down
32 changes: 30 additions & 2 deletions takwimu/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import requests

from django.conf import settings
from django.shortcuts import render_to_response
from django.shortcuts import render_to_response, render
from django.template import RequestContext
from django.views.generic import TemplateView, FormView
from django.views.generic import TemplateView, FormView, View
from django.views.generic.base import TemplateView
from wagtail.wagtailcore.models import Page
from wagtail.wagtailsearch.models import Query

from takwimu.models.dashboard import ExplainerSteps, FAQ, Testimonial
from forms import SupportServicesContactForm
Expand Down Expand Up @@ -42,6 +45,7 @@ class LegalView(TemplateView):
"""
template_name = 'takwimu/about/legal.html'


class TopicView(TemplateView):
"""
Topic View:
Expand Down Expand Up @@ -133,3 +137,27 @@ def form_invalid(self, form):
print('\n\n\n\n\n\n')
print form.data
return super(SupportServicesIndexView, self).form_invalid(form)


class SearchView(TemplateView):
"""
Search View
-----------
Displays search results.
"""
template_name = 'search_results.html'

def get(self, request, *args, **kwargs):
search_results = Page.objects.none()
search_query = request.GET.get('q', '')
if search_query:
search_results = Page.objects.live().search(search_query)

# Log the query so Wagtail can suggest promoted results
Query.get(search_query).add_hit()

return render(request, self.template_name, {
'search_query': search_query,
'search_results': search_results,
})

0 comments on commit dc66680

Please sign in to comment.