Skip to content

Commit

Permalink
Implement Search
Browse files Browse the repository at this point in the history
  • Loading branch information
Merlin Buczek committed Jun 4, 2019
1 parent 70bf6ea commit 01b51b9
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 21 deletions.
5 changes: 4 additions & 1 deletion mpicms/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class CategoryPage(NewsMixin, Page):
panels=None),
]

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

parent_page_types = ['HomePage', 'CategoryPage']

@property
Expand All @@ -113,7 +117,6 @@ class WikiPage(CategoryMixin, Page):

search_fields = Page.search_fields + [
index.SearchField('body'),
index.FilterField('date'),
]

content_panels = Page.content_panels + [
Expand Down
27 changes: 27 additions & 0 deletions mpicms/base/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.shortcuts import render

from wagtail.core.models import Page
from django.core.paginator import Paginator
from wagtail.search.models import Query


def search(request):
search_query = request.GET.get('query', None)
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()
else:
search_results = Page.objects.none()

paginator = Paginator(search_results, 8)

page = request.GET.get('page')
search_results = paginator.get_page(page)

return render(request, 'base/search_results.html', {
'paginator': paginator,
'search_query': search_query,
'search_results': search_results,
})
9 changes: 8 additions & 1 deletion mpicms/news/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.search import index

from mpicms.base.models import CategoryMixin

Expand All @@ -18,7 +19,7 @@ class NewsPage(CategoryMixin, Page):
parent_page_types = ['base.CategoryPage', 'base.HomePage']
subpage_types = ['NewsEntry']
show_in_menus_default = False
paginated_by = 2
paginated_by = 8

def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
Expand Down Expand Up @@ -59,6 +60,12 @@ class NewsEntry(CategoryMixin, Page):
FieldPanel('body', classname="full"),
]

search_fields = Page.search_fields + [
index.SearchField('body'),
index.SearchField('preview'),
index.FilterField('date')
]

parent_page_types = ['NewsPage']

class Meta: # noqa
Expand Down
23 changes: 23 additions & 0 deletions mpicms/static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ select, input {
border-radius: 0 !important;
}

.content .fa-chevron-right {
padding-left: 1em;
}

.title {
font-family: "Merriweather", serif;
font-weight: 400;
Expand Down Expand Up @@ -231,6 +235,20 @@ select, input {
float: right;
}

.card-head {
/* font-color: gray !important; */
padding: 0 !important;
margin: 0 0 0.8em 0 !important;
}

.card-head a {
color: #4a4a4a;
}

.card-head a:hover {
color: black;
}

.section-button {
display: flex;
align-items: center;
Expand All @@ -250,6 +268,11 @@ select, input {
border-color: #006c66;
}

.is-important .card-content{
/* background-color: #eeeeee */
border: 1px solid grey;
}

/* Full Calendar */
.fc-button {
border-radius: 0 !important;
Expand Down
16 changes: 3 additions & 13 deletions mpicms/templates/base/components/pagination.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% load url_tags %}

{% if paginator.num_pages > 1 %}

{% firstof items.number '1' as current %}

<nav class="pagination is-centered" role="navigation" aria-label="pagination">
Expand Down Expand Up @@ -46,16 +48,4 @@
<a class="pagination-next"><i class="fas fa-chevron-right"></i></a>
</nav>

<!-- <nav class="pagination is-centered" role="navigation" aria-label="pagination">
<a class="pagination-previous">Previous</a>
<a class="pagination-next">Next page</a>
<ul class="pagination-list">
<li><a class="pagination-link" aria-label="Goto page 1">1</a></li>
<li><span class="pagination-ellipsis">&hellip;</span></li>
<li><a class="pagination-link" aria-label="Goto page 45">45</a></li>
<li><a class="pagination-link is-current" aria-label="Page 46" aria-current="page">46</a></li>
<li><a class="pagination-link" aria-label="Goto page 47">47</a></li>
<li><span class="pagination-ellipsis">&hellip;</span></li>
<li><a class="pagination-link" aria-label="Goto page 86">86</a></li>
</ul>
</nav> -->
{% endif %}
70 changes: 70 additions & 0 deletions mpicms/templates/base/search_results.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{% extends "base.html" %}
{% load wagtailcore_tags wagtailsearchpromotions_tags %}

{% block title %}Search{% endblock %}

{% block content %}
<div class="section">

<h2 class="title is-2">Search: {{ search_query }}</h2>

{% get_search_promotions search_query as search_promotions %}
{% firstof search_results.number '1' as current %}

{% if search_promotions and current|add:0 == 1 %}
{% for search_promotion in search_promotions %}
<div class="card is-important">
<div class="card-content">
{% if result.specific.category %}
<a href="{% pageurl search_promotion.page.specific.category %}" class="category-label">{{ search_promotion.page.specific.category }}</a>
{% endif %}
<a href="{% pageurl search_promotion.page %}" class="title is-4">{{ search_promotion.page.title }}</a>
{% if search_promotion.description %}
<div class="content">

{{ search_promotion.description }}
</div>

{% endif %}
</div>
</div>
{% endfor %}
<br>
{% endif %}

{% for result in search_results %}
<div class="card">
<div class="card-content">
<nav class="breadcrumb card-head">
<ul>
{% for ancestor in result.get_ancestors|slice:"2:" %}
<li><a href="{% pageurl ancestor %}">{{ ancestor }}</a></li>
{% endfor %}
</ul>
</nav>
<a href="{% pageurl result %}" class="title is-4">{{ result.title }}</a>
{% if result.search_description %}
<div class="content">

{{ result.search_description|safe }}
</div>

{% endif %}
</div>
</div>
{% endfor %}

{% if not search_promotion and not search_results %}
{% if search_query %}
<p>No results found</p>
{% else %}
<p>Please type something into the search box</p>
{% endif %}
{% endif %}

</div>

<div class="section">
{% include 'base/components/pagination.html' with items=search_results %}
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion mpicms/templates/events/event_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block content %}
<div class="section">

<h2 class="title is-2">{{ page.title }}</h1>
<h2 class="title is-2">{{ page.title }}</h2>

<div id="calendar"></div>

Expand Down
10 changes: 5 additions & 5 deletions mpicms/templates/menus/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ <h1 class="title is-1">Intranet</h1>

<div class="navbar-end">
<div class="navbar-item">
<div class="field has-addons">
<form action="{% url 'search' %}" method="get" class="field has-addons">
<div class="control is-expanded">
<input type="text" class="input" placeholder="{% trans 'Search' %}" style="height: 100%">
<input type="text" name="query" value="{{ search_query }}" class="input" placeholder="{% trans 'Search' %}" style="height: 100%">
</div>
<div class="control">
<a class="button">
<button type="submit" class="button">
<span class="icon has-text-grey">
<i class="fas fa-search"></i>
</span>
</a>
</button>
</div>
</div>
</form>
</div>
</div>

Expand Down

0 comments on commit 01b51b9

Please sign in to comment.