diff --git a/cfgov/searchgov/jinja2/searchgov/recommended.html b/cfgov/searchgov/jinja2/searchgov/recommended.html index b6b375180b8..76a53c0047d 100644 --- a/cfgov/searchgov/jinja2/searchgov/recommended.html +++ b/cfgov/searchgov/jinja2/searchgov/recommended.html @@ -6,7 +6,7 @@ {% endif %} diff --git a/cfgov/searchgov/jinja2/searchgov/result.html b/cfgov/searchgov/jinja2/searchgov/result.html index d4ed8e84935..d1f3b26e8a5 100644 --- a/cfgov/searchgov/jinja2/searchgov/result.html +++ b/cfgov/searchgov/jinja2/searchgov/result.html @@ -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) %}
diff --git a/cfgov/searchgov/tests/test_views.py b/cfgov/searchgov/tests/test_views.py index 6777fc69278..51f1e175b02 100644 --- a/cfgov/searchgov/tests/test_views.py +++ b/cfgov/searchgov/tests/test_views.py @@ -5,11 +5,10 @@ from searchgov.views import ( API_ENDPOINT, - decode_meta, encode_url, get_affiliate, get_api_key, - recreate_unencoded, + strip_title_suffix, ) @@ -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): diff --git a/cfgov/searchgov/views.py b/cfgov/searchgov/views.py index a7543a44d1a..a8615596d0f 100644 --- a/cfgov/searchgov/views.py +++ b/cfgov/searchgov/views.py @@ -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 @@ -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): @@ -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, @@ -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 diff --git a/cfgov/v1/jinja2/v1/layouts/base.html b/cfgov/v1/jinja2/v1/layouts/base.html index 32dabe8093d..84831c48966 100644 --- a/cfgov/v1/jinja2/v1/layouts/base.html +++ b/cfgov/v1/jinja2/v1/layouts/base.html @@ -59,10 +59,7 @@ {{- meta_description if page else '' -}} {%- endblock -%} "> - - {# Base32 encoding used to avoid an issue with Search.gov custom fields losing capitalization. -#} - - + {# Always open preview panel links in a new tab. #} {% if request.in_preview_panel %} diff --git a/cfgov/v1/jinja2tags/__init__.py b/cfgov/v1/jinja2tags/__init__.py index 084cf0580b9..424461aa60c 100644 --- a/cfgov/v1/jinja2tags/__init__.py +++ b/cfgov/v1/jinja2tags/__init__.py @@ -1,5 +1,3 @@ -from base64 import b32encode - from jinja2 import pass_context from jinja2.ext import Extension @@ -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) @@ -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, } diff --git a/cfgov/v1/tests/jinja2tags/test_jinja2tags.py b/cfgov/v1/tests/jinja2tags/test_jinja2tags.py index 2b5f85c5342..340966f3494 100644 --- a/cfgov/v1/tests/jinja2tags/test_jinja2tags.py +++ b/cfgov/v1/tests/jinja2tags/test_jinja2tags.py @@ -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=")