Skip to content

Commit

Permalink
Merge pull request #72 from NatLibFi/simplye-388/ignore-language-head…
Browse files Browse the repository at this point in the history
…ers-in-search

Ignore Accept-Language header in search results
  • Loading branch information
ttuovinen authored May 7, 2024
2 parents 6fb4872 + e69131e commit 1144ea5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
13 changes: 4 additions & 9 deletions core/lane.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
from core.model.listeners import site_configuration_has_changed
from core.problem_details import *
from core.util import LanguageCodes
from core.util.accept_language import parse_accept_language
from core.util.datetime_helpers import utc_now
from core.util.opds_writer import OPDSFeed
from core.util.problem_detail import ProblemDetail
Expand Down Expand Up @@ -1080,16 +1079,12 @@ def from_request(
# Searches against a WorkList will use the union of the
# languages allowed by the WorkList and the languages found in
# the client's Accept-Language header.
language_header = get_header("Accept-Language")

# Finland: Accept-Language header usage removed from here
# in favor of the added language facet

languages = get_argument("language") or None
extra["language_from_query"] = languages is not None
if not languages:
if language_header:
languages = parse_accept_language(language_header)
languages = [l[0] for l in languages]
languages = list(map(LanguageCodes.iso_639_2_for_locale, languages))
languages = [l for l in languages if l]
languages = languages or None
extra["languages"] = languages

# The client can request a minimum score for search results.
Expand Down
9 changes: 5 additions & 4 deletions tests/core/test_app_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,13 @@ def test_load_facets_from_request(
assert default_entrypoint == facets.entrypoint
assert facets.entrypoint_is_default is True

# Load a SearchFacets object that pulls information from an
# HTTP header.
with fixture.app.test_request_context("/", headers={"Accept-Language": "ja"}):
# Finland: Load a SearchFacets object with language as facet and ignore Accept-Language header.
with fixture.app.test_request_context(
"/?language=eng", headers={"Accept-Language": "ja"}
):
flask.request.library = data.default_library() # type: ignore[attr-defined]
facets = load_facets_from_request(base_class=SearchFacets)
assert ["jpn"] == facets.languages
assert ["eng"] == facets.languages

def test_load_facets_from_request_class_instantiation(
self, load_methods_fixture: LoadMethodsFixture
Expand Down
6 changes: 5 additions & 1 deletion tests/core/test_lane.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,11 @@ def from_request(**extra):

# The SearchFacets implementation turned the 'Accept-Language'
# header into a set of language codes.
assert ["dan", "eng"] == facets.languages

# Finland: Language-header usage removed in favor of added language facet
# Before: assert ["dan", "eng"] == facets.languages
assert None == facets.languages

assert False == facets._language_from_query

# Try again with bogus media, languages, and minimum score.
Expand Down

0 comments on commit 1144ea5

Please sign in to comment.