Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 0 additions & 17 deletions cfgov/searchgov/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

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


Expand Down Expand Up @@ -41,21 +39,6 @@ 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 JsonTestCase(TestCase):
def test_json_response(self):
response = self.client.get("/search/?q=mortgage&format=json")
Expand Down
30 changes: 3 additions & 27 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,14 +38,6 @@ 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"))


class SearchView(TranslatedTemplateView):
template_name = "searchgov/index.html"

Expand All @@ -72,7 +62,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 +94,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"] = re.sub(r" \| .*", "", res["title"])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was trickier than I thought to strip the | Consumer Financial Protection Bureau from the end of search result titles. We used to do it like res["title"] = res["title"][:-39] (see #8993). That mostly worked, but I realized it didn't cover if someone searches for, say, "bureau", which would come back in the API results as | Consumer Financial Protection \ue000Bureau\ue001 and thus leave | in the result title. This regex seems to work fine unless we for some reason happen to have pages with a | somewhere else in their title. We shouldn't have any page titles like that, but if there's a way to improve this regex, I'm all for it. I thought about using a negative lookahead to only capture the last instance, but I wasn't sure if the extra complexity was worth it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me! Definitely better than the previous way.


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