Skip to content

Commit 1f30eeb

Browse files
authored
Merge pull request #9094 from cfpb/fix/global-search
Update global search for API changes
2 parents 8fa3d5a + 8157181 commit 1f30eeb

7 files changed

Lines changed: 21 additions & 65 deletions

File tree

cfgov/searchgov/jinja2/searchgov/recommended.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div class="block block--flush-top u-mb45 recommended">
77
<h2 class="h5 u-mt45">{{rec_hed}}</h2>
88
{% for result in recommended %}
9-
{{ render_result(result, domain) }}
9+
{{ render_result(result, domain, key="description") }}
1010
{% endfor %}
1111
</div>
1212
{% endif %}

cfgov/searchgov/jinja2/searchgov/result.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{{url.replace("https://www.consumerfinance.gov", domain)}}
1515
{%- endmacro -%}
1616

17-
{%- macro render(result, domain, key="description") -%}
17+
{%- macro render(result, domain, key="snippet") -%}
1818
{%- set url = replace_url(result.url, domain) %}
1919
<div class="search-result">
2020
<article>

cfgov/searchgov/tests/test_views.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55

66
from searchgov.views import (
77
API_ENDPOINT,
8-
decode_meta,
98
encode_url,
109
get_affiliate,
1110
get_api_key,
12-
recreate_unencoded,
11+
strip_title_suffix,
1312
)
1413

1514

@@ -41,19 +40,12 @@ def test_encodes_non_url_safe_chars(self):
4140
)
4241

4342

44-
class RecreateUnencodedTestCase(TestCase):
45-
def test_combines_lowercased_split(self):
46-
self.assertEqual(recreate_unencoded(["abc", "def"]), "Abc, def")
47-
48-
49-
class DecodeMetaTestCase(TestCase):
50-
def test_decodes_encoded(self):
51-
self.assertEqual(decode_meta("mfrggzdf"), "abcde")
52-
53-
def test_decodes_and_unescapes(self):
54-
self.assertEqual(
55-
decode_meta("MFRCM4LVN52DWYZGOF2W65B3MQ======"), 'ab"c"d'
43+
class StripTitleSuffixTestCase(TestCase):
44+
def test_strip_suffix(self):
45+
cleaned_title = strip_title_suffix(
46+
"Page Title | Consumer Financial Protection Bureau"
5647
)
48+
self.assertEqual(cleaned_title, "Page Title")
5749

5850

5951
class JsonTestCase(TestCase):

cfgov/searchgov/views.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import math
2-
from base64 import b32decode
3-
from binascii import Error
4-
from html import unescape
2+
import re
53
from urllib.parse import urlencode
64

75
from django.conf import settings
@@ -40,12 +38,15 @@ def encode_url(params):
4038
return API_ENDPOINT.format(urlencode(params))
4139

4240

43-
def recreate_unencoded(parts):
44-
return ", ".join(parts).capitalize()
45-
46-
47-
def decode_meta(encoded):
48-
return unescape(b32decode(encoded.upper()).decode("utf-8"))
41+
def strip_title_suffix(title):
42+
"""Remove the ' | Consumer Financial Protection Bureau' from the end
43+
of search result titles. Becuase one or more words may be enclosed by
44+
\ue000 and \ue001 characters if they match a search term, we have to be
45+
a little flexible with the regex.
46+
"""
47+
suffix_regex = r" \| .*"
48+
cleaned_title = re.sub(suffix_regex, "", title)
49+
return cleaned_title
4950

5051

5152
class SearchView(TranslatedTemplateView):
@@ -72,7 +73,6 @@ def get(self, request, **kwargs):
7273
response = requests.get(
7374
encode_url(
7475
{
75-
"include_facets": "true",
7676
"affiliate": affiliate,
7777
"access_key": api_key,
7878
"limit": RESULTS_PER_PAGE,
@@ -105,21 +105,8 @@ def get(self, request, **kwargs):
105105

106106
# Post proprocess results
107107
for res in results:
108-
# Strip | CFPB suffix
109-
encoded_list = res.get("searchgov_custom2")
110-
if encoded_list:
111-
# Hack due to search.gov caching old data
112-
try:
113-
res["description"] = decode_meta(
114-
encoded_list[0]
115-
)
116-
# binascii Error points to likely unencoded data
117-
except Error:
118-
res["description"] = recreate_unencoded(
119-
encoded_list
120-
)
121-
else:
122-
res["description"] = res["snippet"]
108+
# Strip " | CFPB" suffix
109+
res["title"] = strip_title_suffix(res["title"])
123110

124111
else:
125112
count = 0

cfgov/v1/jinja2/v1/layouts/base.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@
5959
{{- meta_description if page else '' -}}
6060
{%- endblock -%}
6161
">
62-
<meta name="searchgov_custom1" content="{{ language }}">
63-
{# Base32 encoding used to avoid an issue with Search.gov custom fields losing capitalization. -#}
64-
<meta name="searchgov_custom2" content="{{ encode_b32_string(self.desc()) }}">
65-
62+
6663
{# Always open preview panel links in a new tab. #}
6764
{% if request.in_preview_panel %}
6865
<base target="_blank">

cfgov/v1/jinja2tags/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from base64 import b32encode
2-
31
from jinja2 import pass_context
42
from jinja2.ext import Extension
53

@@ -53,10 +51,6 @@ def unique_id_in_context(context):
5351
return get_unique_id()
5452

5553

56-
def encode_b32_string(s):
57-
return b32encode(s.encode("utf-8")).decode("utf-8")
58-
59-
6054
class V1Extension(Extension):
6155
def __init__(self, environment):
6256
super().__init__(environment)
@@ -69,7 +63,6 @@ def __init__(self, environment):
6963
"get_unique_id": get_unique_id,
7064
"is_filter_selected": pass_context(is_filter_selected),
7165
"unique_id_in_context": pass_context(unique_id_in_context),
72-
"encode_b32_string": encode_b32_string,
7366
"app_url": app_url,
7467
"app_page_url": app_page_url,
7568
}

cfgov/v1/tests/jinja2tags/test_jinja2tags.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,3 @@ def test_nonexistent_category_name_returns_none(self):
9898
self.checkRender(
9999
"{{ get_category_icon('Invalid category name') }}", "None"
100100
)
101-
102-
103-
class TestEncodeB32String(SimpleTestCase):
104-
def setUp(self):
105-
self.jinja_engine = engines["wagtail-env"]
106-
107-
def test_encode_b32_string(self):
108-
s = (
109-
"{%- block desc -%}cfpb{%- endblock -%}"
110-
"{{encode_b32_string(self.desc())}}"
111-
)
112-
template = self.jinja_engine.from_string(s)
113-
self.assertEqual(template.render(), "cfpbMNTHAYQ=")

0 commit comments

Comments
 (0)