Skip to content

Commit

Permalink
Merge pull request #592 from freedomofpress/569-search-incidents-only
Browse files Browse the repository at this point in the history
569 search incidents only
  • Loading branch information
melinath committed Feb 15, 2018
2 parents f36f667 + 6e093de commit d249f48
Show file tree
Hide file tree
Showing 25 changed files with 202 additions and 138 deletions.
11 changes: 11 additions & 0 deletions README.rst
Expand Up @@ -183,3 +183,14 @@ Licenses for `Source Serif Pro <https://github.com/adobe-fonts/source-serif-pro>

- `common/static/fonts/LICENSE.SourceSansPro.txt`
- `common/static/fonts/LICENSE.SourceSerifPro.txt`

Design decision notes
+++++++++++++++++++++

Search
------

The search bar on the site is a shortcut to using incident search.
This is because the site is primarily incident-related, and using incident search provides more powerful filtering as well as enhanced previews.
As a result, there is no generic wagtail search view which includes other content such as blog posts.
See https://github.com/freedomofpress/pressfreedom/pull/592.
6 changes: 4 additions & 2 deletions common/management/commands/createdevdata.py
Expand Up @@ -13,7 +13,7 @@
from common.models import (
CategoryPage, TaxonomyCategoryPage, TaxonomySettings,
PersonPage, SimplePage, SimplePageWithSidebar,
FooterSettings, CustomImage,
FooterSettings, CustomImage, SearchSettings,
)
from forms.models import FormPage
from home.models import HomePage, HomePageIncidents
Expand Down Expand Up @@ -249,12 +249,14 @@ def handle(self, *args, **options):
blog_index_page.add_child(instance=page)

# INCIDENT RELATED PAGES
search_settings = SearchSettings.for_site(site)
incident_index_page = IncidentIndexPage(
title='All Incidents',
slug='all-incidents'
)
home_page.add_child(instance=incident_index_page)
home_page.incident_index_page = incident_index_page
search_settings.search_page = incident_index_page
search_settings.save()

for x in range(0, 100):
page = IncidentPage(
Expand Down
28 changes: 28 additions & 0 deletions common/migrations/0030_searchsettings.py
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2018-02-05 23:56
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('wagtailcore', '0039_collectionviewrestriction'),
('common', '0029_auto_20180205_2333'),
]

operations = [
migrations.CreateModel(
name='SearchSettings',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('search_page', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailcore.Page')),
('site', models.OneToOneField(editable=False, on_delete=django.db.models.deletion.CASCADE, to='wagtailcore.Site')),
],
options={
'verbose_name': 'Search',
},
),
]
25 changes: 25 additions & 0 deletions common/migrations/0031_auto_20180206_0046.py
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2018-02-06 00:46
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('common', '0030_searchsettings'),
]

operations = [
migrations.AlterModelOptions(
name='searchsettings',
options={'verbose_name': 'Incident search'},
),
migrations.AlterField(
model_name='searchsettings',
name='search_page',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='incident.IncidentIndexPage'),
),
]
21 changes: 21 additions & 0 deletions common/migrations/0032_auto_20180206_0048.py
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2018-02-06 00:48
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('common', '0031_auto_20180206_0046'),
]

operations = [
migrations.AlterField(
model_name='searchsettings',
name='search_page',
field=models.ForeignKey(blank=True, help_text='Incident index page to use for search and for "Recent incidents" feeds', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='incident.IncidentIndexPage', verbose_name='Incident search page'),
),
]
10 changes: 5 additions & 5 deletions common/models/pages.py
Expand Up @@ -224,17 +224,17 @@ def get_context(self, request):
# placed here to avoid circular dependency
from incident.utils.incident_filter import IncidentFilter
from incident.models.choices import get_filter_choices
from home.models import HomePage
from common.models.settings import SearchSettings

context = super(CategoryPage, self).get_context(request)

incident_filter = IncidentFilter.from_request(request)
incident_filter.categories = str(self.page_ptr_id)
context['category_options'] = incident_filter.get_category_options()
try:
context['export_path'] = HomePage.objects.live()[0].incident_index_page.url
except Exception:
context['export_path'] = None

search_page = SearchSettings.for_site(request.site).search_page
context['export_path'] = getattr(search_page, 'url', None)

context['filter_choices'] = get_filter_choices()
summary, entry_qs = incident_filter.fetch()

Expand Down
20 changes: 20 additions & 0 deletions common/models/settings.py
Expand Up @@ -7,6 +7,26 @@
from modelcluster.models import ClusterableModel


@register_setting(icon='search')
class SearchSettings(BaseSetting):
search_page = models.ForeignKey(
'incident.IncidentIndexPage',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
help_text='Incident index page to use for search and for "Recent incidents" feeds',
verbose_name='Incident search page',
)

panels = [
PageChooserPanel('search_page'),
]

class Meta:
verbose_name = 'Incident search'


@register_setting
class FooterSettings(BaseSetting):
body = RichTextField(blank=True, null=True)
Expand Down
10 changes: 8 additions & 2 deletions common/templates/common/_search_bar.html
@@ -1,11 +1,15 @@
<form action="{% url 'search' %}" class="search-bar {{ class|default:'' }}" method="get">
{% load wagtailcore_tags %}

{% with search_page=settings.common.SearchSettings.search_page %}
{% if search_page %}
<form action="{% pageurl search_page %}" class="search-bar {{ class|default:'' }}" method="get">
{% include "common/_search_icon.html" with class="search-bar__icon" %}

<input
type="text"
class="search-bar__input"
{% if search_query %} value="{{ search_query }}"{% endif %}
name="query"
name="search"
/>

<button
Expand All @@ -16,3 +20,5 @@
<span class="sr-only">Search</span>
</button>
</form>
{% endif %}
{% endwith %}
3 changes: 0 additions & 3 deletions common/templates/common/category_block.html
@@ -1,6 +1,3 @@
{% load wagtailsettings_tags %}
{% get_settings %}

<div class="category-block">
<h2 class="category-block__header">
All Categories
Expand Down
3 changes: 0 additions & 3 deletions common/templates/common/emails_signup.html
@@ -1,6 +1,3 @@
{% load wagtailsettings_tags %}
{% get_settings %}

{% with email_settings=settings.emails.EmailSettings %}
<div class="emails-signup" id="js-emails-signup-target"></div>
<script type="text/javascript">
Expand Down
3 changes: 1 addition & 2 deletions common/templates/common/info_footer.html
@@ -1,5 +1,4 @@
{% load wagtailcore_tags wagtailimages_tags wagtailsettings_tags static typogrify_tags %}
{% get_settings %}
{% load wagtailcore_tags wagtailimages_tags static typogrify_tags %}

{% with footer=settings.common.FooterSettings %}
<div class="info-footer">
Expand Down
2 changes: 2 additions & 0 deletions dashboard/templates/dashboard/shortcuts_panel.html
Expand Up @@ -10,6 +10,7 @@
</a>
</li>

{% if incident_page %}
<li>
<a
href="{% url 'wagtailadmin_pages:add_subpage' incident_page.pk %}"
Expand All @@ -19,5 +20,6 @@
Add a new incident
</a>
</li>
{% endif %}
</ul>
</section>
7 changes: 4 additions & 3 deletions dashboard/wagtail_hooks.py
@@ -1,6 +1,7 @@
from django.template.loader import render_to_string
from wagtail.wagtailcore import hooks
from wagtail.wagtailcore.models import Site

from common.models.settings import SearchSettings


class ShortcutsPanel:
Expand All @@ -12,9 +13,9 @@ def __init__(self, request):
self.request = request

def render(self):
site = Site.find_for_request(self.request)
search_page = SearchSettings.for_site(self.request.site).search_page
return render_to_string('dashboard/shortcuts_panel.html', dict(
incident_page=site.root_page.specific.incident_index_page,
incident_page=search_page,
))


Expand Down
33 changes: 33 additions & 0 deletions home/migrations/0019_move_all_incidents_to_setting.py
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2018-02-06 00:04
from __future__ import unicode_literals

from django.db import migrations

from common.models.settings import SearchSettings
from wagtail.wagtailcore.models import Site


def move_all_incidents_to_setting(apps, schema_editor):
HomePage = apps.get_model('home', 'HomePage')
site = Site.objects.get(is_default_site=True)
search_settings = SearchSettings.for_site(site)
try:
homepage = HomePage.objects.get()
except HomePage.DoesNotExist:
pass
else:
search_settings.search_page_id = homepage.incident_index_page_id
search_settings.save()


class Migration(migrations.Migration):

dependencies = [
('home', '0018_auto_20180205_2333'),
('common', '0030_searchsettings'),
]

operations = [
migrations.RunPython(move_all_incidents_to_setting, migrations.RunPython.noop, elidable=True),
]
19 changes: 19 additions & 0 deletions home/migrations/0020_remove_homepage_incident_index_page.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2018-02-06 00:46
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('home', '0019_move_all_incidents_to_setting'),
]

operations = [
migrations.RemoveField(
model_name='homepage',
name='incident_index_page',
),
]
16 changes: 5 additions & 11 deletions home/models.py
Expand Up @@ -14,6 +14,7 @@

from common.choices import CATEGORY_COLOR_CHOICES
from common.models import MetadataPageMixin
from common.models.settings import SearchSettings
from incident.models.choices import get_filter_choices
from incident.utils.incident_filter import IncidentFilter

Expand Down Expand Up @@ -42,15 +43,6 @@ class HomePage(MetadataPageMixin, Page):
help_text='Recent blog posts will automatically be pulled from this page'
)

incident_index_page = models.ForeignKey(
'incident.IncidentIndexPage',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
help_text='Recent incidents will automatically be pulled from this page'
)

statboxes_label = models.CharField(
default='Quick Stats',
max_length=255,
Expand Down Expand Up @@ -132,7 +124,6 @@ class HomePage(MetadataPageMixin, Page):

MultiFieldPanel([
FieldPanel('recent_incidents_label'),
PageChooserPanel('incident_index_page', 'incident.IncidentIndexPage'),
], 'Recent Incidents'),

MultiFieldPanel([
Expand All @@ -151,7 +142,10 @@ def get_context(self, request):

incident_filter = IncidentFilter.from_request(request)
context['category_options'] = incident_filter.get_category_options()
context['export_path'] = self.incident_index_page.url

search_page = SearchSettings.for_site(request.site).search_page
context['export_path'] = getattr(search_page, 'url', None)

context['filter_choices'] = get_filter_choices()
context['incidents'] = self.incidents.all().prefetch_related('incident__categories__category')

Expand Down
8 changes: 5 additions & 3 deletions home/templates/home/_featured_incidents.html
@@ -1,10 +1,11 @@
{% load wagtailcore_tags typogrify_tags %}

{% if page.incident_index_page %}
{% with search_page=settings.common.SearchSettings.search_page %}
{% if search_page %}

{% with incidents=incidents %}
{% include "common/_section_heading.html" with label=label %}
{% include "incident/_filters.html" with apply_externally=True external=page.incident_index_page change_filters_message=page.change_filters_message %}
{% include "incident/_filters.html" with apply_externally=True external=search_page change_filters_message=page.change_filters_message %}

<div class="grid-50">
{% for related in incidents|slice:":2" %}
Expand All @@ -24,9 +25,10 @@
{% endwith %}
<a
class="button button--outline button--center"
href="{% pageurl page.incident_index_page %}"
href="{% pageurl search_page %}"
>
{{ link_label }}
</a>

{% endif %}
{% endwith %}

0 comments on commit d249f48

Please sign in to comment.