Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #499 from eregs/475-better-search
Browse files Browse the repository at this point in the history
Improved search UI
  • Loading branch information
cmc333333 authored Aug 22, 2017
2 parents 36cc4a0 + 7e9aa8f commit 21f7d61
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ p.search-version {
margin-top: 2em;
}

h3 {
h3, h4, h5 {
@include sans-font-bold;
margin-bottom: 0;
}
Expand Down
4 changes: 3 additions & 1 deletion regulations/templates/regulations/search-results.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ <h2 class="search-term">Searching for “{{ q }}”</h2>
{% for result in results.results %}
<li>
<h3><a href="{{result.url}}" class="internal" data-linked-section="{{result.section_id}}" data-linked-version="{{version}}" data-linked-subsection="{{result.label|join:'-'}}">{{result.header}}</a></h3>
<p>{{result.text}}</p>
{% if result.subheader %}<h4>{{ result.subheader }}</h4>{% endif %}
{% if result.subsubheader %}<h5>{{ result.subsubheader }}</h5>{% endif %}
{% if result.text %}<p>{{result.text}}</p>{% endif %}
</li>
{% empty %}
<span class="no-results">Sorry, there are no results in the regulation effective {{version_by_date|date:"n/j/Y"}}.</span>
Expand Down
36 changes: 34 additions & 2 deletions regulations/tests/views_partial_search_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from datetime import date

from mock import Mock
Expand All @@ -13,7 +15,7 @@ def test_get(client, monkeypatch):
'total_hits': 3333,
'results': [
{'label': ['111', '22'], 'text': 'tttt', 'version': 'vvv',
'title':"consumer's"},
'title':"Consumer's"},
{'label': ['111', '24', 'a'], 'text': 'o', 'version': 'vvv'},
{'label': ['111', '25'], 'text': 'more', 'version': 'vvv'}
]
Expand All @@ -29,7 +31,6 @@ def test_get(client, monkeypatch):
]
response = client.get('/partial/search/111?version=vvv&q=none').content
assert b'111-22' in response
assert b'111.22' in response
assert b'111-24-a' in response
assert b'111.24(a)' in response
assert b'111-25' in response
Expand Down Expand Up @@ -188,3 +189,34 @@ def test_add_prev_next():
view.add_prev_next(7, context)
assert context['previous'] == {'page': 6, 'length': 10}
assert 'next' not in context


def test_add_cfr_headers_no_title():
"""If no title is present in the result data, we construct one based on
its label."""
result = partial_search.add_cfr_headers({'label': ['111', '22', 'c', '4']})
assert result['header'] == '111.22(c)(4)'


def test_add_cfr_headers_section():
"""If all of the titles are the same (as is the case when a section's
intro text matches our search), we don't need to repeat them."""
result = partial_search.add_cfr_headers({'title': 'Section 22',
'match_title': 'Section 22',
'paragraph_title': 'Section 22'})
assert result['header'] == 'Section 22'
assert 'subheader' not in result
assert 'subsubheader' not in result


def test_add_cfr_headers_three_levels():
"""If the three levels of title are different, we expect them in our
results."""
result = partial_search.add_cfr_headers({
'title': 'Section 22',
'match_title': 'A matching heading',
'paragraph_title': 'A title for text',
})
assert result['header'] == 'Section 22'
assert result['subheader'] == 'A matching heading'
assert result['subsubheader'] == 'A title for text'
23 changes: 18 additions & 5 deletions regulations/views/partial_search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from copy import deepcopy

from django.core.urlresolvers import reverse
from django.template.defaultfilters import title

from regulations.generator import api_reader, node_types
from regulations.generator.html_builder import PreambleHTMLBuilder
Expand Down Expand Up @@ -85,15 +84,29 @@ def get_context_data(self, doc_type, **kwargs):
return context


def add_cfr_headers(result):
"""We always want a title to click, even if the search result doesn't have
one. We also want to prevent duplication, so we'll only show additional
levels of headings if they differ."""
if result.get('title'):
result['header'] = result['title']
else:
result['header'] = node_types.label_to_text(result['label'])
if result.get('match_title') and result['match_title'] != result['title']:
result['subheader'] = result['match_title']
if (result.get('paragraph_title') and
result['paragraph_title'] != result['match_title']):
result['subsubheader'] = result['paragraph_title']
return result


def process_cfr_results(results, version):
"""Modify the results of a search over the CFR by adding a human-readable
label, appropriate links, and version information"""
section_url = SectionUrl()
results = deepcopy(results)
for result in results['results']:
result['header'] = node_types.label_to_text(result['label'])
if 'title' in result:
result['header'] += ' ' + title(result['title'])
add_cfr_headers(result)
result['section_id'] = section_url.view_label_id(
result['label'], version)
result['url'] = section_url.fetch(
Expand All @@ -108,7 +121,7 @@ def process_preamble_results(results):
for result in results['results']:
result['header'] = PreambleHTMLBuilder.human_label(result)
if 'title' in result:
result['header'] += ' ' + title(result['title'])
result['header'] += ' ' + result['title']
result['section_id'] = '-'.join(
[result['label'][0], 'preamble'] + result['label'])
result['url'] = '{}#{}'.format(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="regulations",
version="8.1.0",
version="8.2.0",
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down

0 comments on commit 21f7d61

Please sign in to comment.