Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cfgov/searchgov/jinja2/searchgov/recommended.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="block block--flush-top u-mb45 recommended">
<h2 class="h5 u-mt45">{{rec_hed}}</h2>
{% for result in recommended %}
{{ render_result(result, domain) }}
{{ render_result(result, domain, key="description") }}
{% endfor %}
</div>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion cfgov/searchgov/jinja2/searchgov/result.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{{url.replace("https://www.consumerfinance.gov", domain)}}
{%- endmacro -%}

{%- macro render(result, domain, key="description") -%}
{%- macro render(result, domain, key="snippet") -%}
{%- set url = replace_url(result.url, domain) %}
<div class="search-result">
<article>
Expand Down
20 changes: 6 additions & 14 deletions cfgov/searchgov/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

from searchgov.views import (
API_ENDPOINT,
decode_meta,
encode_url,
get_affiliate,
get_api_key,
recreate_unencoded,
strip_title_suffix,
)


Expand Down Expand Up @@ -41,19 +40,12 @@ def test_encodes_non_url_safe_chars(self):
)


class RecreateUnencodedTestCase(TestCase):
def test_combines_lowercased_split(self):
self.assertEqual(recreate_unencoded(["abc", "def"]), "Abc, def")


class DecodeMetaTestCase(TestCase):
def test_decodes_encoded(self):
self.assertEqual(decode_meta("mfrggzdf"), "abcde")

def test_decodes_and_unescapes(self):
self.assertEqual(
decode_meta("MFRCM4LVN52DWYZGOF2W65B3MQ======"), 'ab"c"d'
class StripTitleSuffixTestCase(TestCase):
def test_strip_suffix(self):
cleaned_title = strip_title_suffix(
"Page Title | Consumer Financial Protection Bureau"
)
self.assertEqual(cleaned_title, "Page Title")


class JsonTestCase(TestCase):
Expand Down
37 changes: 12 additions & 25 deletions cfgov/searchgov/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import math
from base64 import b32decode
from binascii import Error
from html import unescape
import re
from urllib.parse import urlencode

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


def recreate_unencoded(parts):
return ", ".join(parts).capitalize()


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


class SearchView(TranslatedTemplateView):
Expand All @@ -72,7 +73,6 @@ def get(self, request, **kwargs):
response = requests.get(
encode_url(
{
"include_facets": "true",
"affiliate": affiliate,
"access_key": api_key,
"limit": RESULTS_PER_PAGE,
Expand Down Expand Up @@ -105,21 +105,8 @@ def get(self, request, **kwargs):

# Post proprocess results
for res in results:
# Strip | CFPB suffix
encoded_list = res.get("searchgov_custom2")
if encoded_list:
# Hack due to search.gov caching old data
try:
res["description"] = decode_meta(
encoded_list[0]
)
# binascii Error points to likely unencoded data
except Error:
res["description"] = recreate_unencoded(
encoded_list
)
else:
res["description"] = res["snippet"]
# Strip " | CFPB" suffix
res["title"] = strip_title_suffix(res["title"])

else:
count = 0
Expand Down
5 changes: 1 addition & 4 deletions cfgov/v1/jinja2/v1/layouts/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@
{{- meta_description if page else '' -}}
{%- endblock -%}
">
<meta name="searchgov_custom1" content="{{ language }}">
{# Base32 encoding used to avoid an issue with Search.gov custom fields losing capitalization. -#}
<meta name="searchgov_custom2" content="{{ encode_b32_string(self.desc()) }}">


{# Always open preview panel links in a new tab. #}
{% if request.in_preview_panel %}
<base target="_blank">
Expand Down
7 changes: 0 additions & 7 deletions cfgov/v1/jinja2tags/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from base64 import b32encode

from jinja2 import pass_context
from jinja2.ext import Extension

Expand Down Expand Up @@ -53,10 +51,6 @@ def unique_id_in_context(context):
return get_unique_id()


def encode_b32_string(s):
return b32encode(s.encode("utf-8")).decode("utf-8")


class V1Extension(Extension):
def __init__(self, environment):
super().__init__(environment)
Expand All @@ -69,7 +63,6 @@ def __init__(self, environment):
"get_unique_id": get_unique_id,
"is_filter_selected": pass_context(is_filter_selected),
"unique_id_in_context": pass_context(unique_id_in_context),
"encode_b32_string": encode_b32_string,
"app_url": app_url,
"app_page_url": app_page_url,
}
Expand Down
13 changes: 0 additions & 13 deletions cfgov/v1/tests/jinja2tags/test_jinja2tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,3 @@ def test_nonexistent_category_name_returns_none(self):
self.checkRender(
"{{ get_category_icon('Invalid category name') }}", "None"
)


class TestEncodeB32String(SimpleTestCase):
def setUp(self):
self.jinja_engine = engines["wagtail-env"]

def test_encode_b32_string(self):
s = (
"{%- block desc -%}cfpb{%- endblock -%}"
"{{encode_b32_string(self.desc())}}"
)
template = self.jinja_engine.from_string(s)
self.assertEqual(template.render(), "cfpbMNTHAYQ=")
Loading