Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Fix dynamically excluded providers (#3401)
Olga Bulatfd73fcca0a78
diff --git a/api/api/controllers/search_controller.py b/api/api/controllers/search_controller.pyindex 3a6200815..1a5a4eb62 100644--- a/api/api/controllers/search_controller.py+++ b/api/api/controllers/search_controller.py@@ -50,6 +50,8 @@ module_logger = logging.getLogger(__name__)NESTING_THRESHOLD = config("POST_PROCESS_NESTING_THRESHOLD", cast=int, default=5)SOURCE_CACHE_TIMEOUT = 60 * 60 * 4 # 4 hoursFILTER_CACHE_TIMEOUT = 30+FILTERED_PROVIDERS_CACHE_KEY = "filtered_providers"+FILTERED_PROVIDERS_CACHE_VERSION = 2THUMBNAIL = "thumbnail"URL = "url"PROVIDER = "provider"@@ -170,19 +172,27 @@ def get_excluded_providers_query() -> Q | None:Hide data sources from the catalog dynamically.To exclude a provider, set ``filter_content`` to ``True`` in the``ContentProvider`` model in Django admin.+ The list of ``provider_identifier``s is cached in Redis with+ `:FILTERED_PROVIDERS_CACHE_VERSION:FILTERED_PROVIDERS_CACHE_KEY` key."""- filter_cache_key = "filtered_providers"- filtered_providers = cache.get(key=filter_cache_key)+ filtered_providers = cache.get(+ key=FILTERED_PROVIDERS_CACHE_KEY, version=FILTERED_PROVIDERS_CACHE_VERSION+ )if not filtered_providers:- filtered_providers = models.ContentProvider.objects.filter(- filter_content=True- ).values("provider_identifier")+ filtered_providers = list(+ models.ContentProvider.objects.filter(filter_content=True).values_list(+ "provider_identifier", flat=True+ )+ )cache.set(- key=filter_cache_key, timeout=FILTER_CACHE_TIMEOUT, value=filtered_providers+ key=FILTERED_PROVIDERS_CACHE_KEY,+ version=FILTERED_PROVIDERS_CACHE_VERSION,+ timeout=FILTER_CACHE_TIMEOUT,+ value=filtered_providers,)- if provider_list := [f["provider_identifier"] for f in filtered_providers]:- return Q("terms", provider=provider_list)+ if filtered_providers:+ return Q("terms", provider=filtered_providers)return Nonediff --git a/api/test/unit/controllers/elasticsearch/test_related.py b/api/test/unit/controllers/elasticsearch/test_related.pyindex abbb0bfa9..b05066c80 100644--- a/api/test/unit/controllers/elasticsearch/test_related.py+++ b/api/test/unit/controllers/elasticsearch/test_related.py@@ -12,6 +12,10 @@ import pookimport pytestfrom api.controllers.elasticsearch import related+from api.controllers.search_controller import (+ FILTERED_PROVIDERS_CACHE_KEY,+ FILTERED_PROVIDERS_CACHE_VERSION,+)pytestmark = pytest.mark.django_db@@ -19,14 +23,18 @@ pytestmark = pytest.mark.django_db@pytest.fixturedef excluded_providers_cache():- cache_key = "filtered_providers"excluded_provider = "excluded_provider"- cache_value = [{"provider_identifier": excluded_provider}]- cache.set(cache_key, cache_value, timeout=1)+ cache_value = [excluded_provider]+ cache.set(+ key=FILTERED_PROVIDERS_CACHE_KEY,+ version=FILTERED_PROVIDERS_CACHE_VERSION,+ value=cache_value,+ timeout=1,+ )yield excluded_provider- cache.delete(cache_key)+ cache.delete(FILTERED_PROVIDERS_CACHE_KEY, version=FILTERED_PROVIDERS_CACHE_VERSION)@mock.patch(diff --git a/api/test/unit/controllers/test_search_controller.py b/api/test/unit/controllers/test_search_controller.pyindex 764fffca2..6d87311c6 100644--- a/api/test/unit/controllers/test_search_controller.py+++ b/api/test/unit/controllers/test_search_controller.py@@ -11,13 +11,17 @@ from test.factory.es_http import (from unittest import mockfrom uuid import uuid4+from django.core.cache import cache+import pookimport pytestfrom django_redis import get_redis_connectionfrom elasticsearch_dsl import Search+from elasticsearch_dsl.query import Termsfrom api.controllers import search_controllerfrom api.controllers.elasticsearch import helpers as es_helpers+from api.controllers.search_controller import FILTERED_PROVIDERS_CACHE_KEYfrom api.utils import talliesfrom api.utils.dead_link_mask import get_query_hash, save_query_maskfrom api.utils.search_context import SearchContext@@ -26,6 +30,19 @@ from api.utils.search_context import SearchContextpytestmark = pytest.mark.django_db+def cache_setter():+ keys = []++ def _cache_setter(key, value, version=1):+ keys.append((key, version))+ cache.set(key, value=value, version=version, timeout=1)++ yield _cache_setter+ for key, version in keys:+ cache.delete(key, version=version)++@pytest.mark.parametrize("total_hits, real_result_count, page_size, page, expected",[@@ -807,3 +824,14 @@ def test_excessive_recursion_in_post_process(filter_dead=True,)assert "Nesting threshold breached" in caplog.text+++def test_get_excluded_providers_query_returns_None_when_no_provider_is_excluded():+ assert search_controller.get_excluded_providers_query() is None+++def test_get_excluded_providers_query_returns_when_cache_is_set(cache_setter):+ cache_setter(FILTERED_PROVIDERS_CACHE_KEY, ["provider1", "provider2"], version=2)+ assert search_controller.get_excluded_providers_query() == Terms(+ provider=["provider1", "provider2"]+ )diff --git a/api/test/unit/controllers/test_search_controller_search_query.py b/api/test/unit/controllers/test_search_controller_search_query.pyindex bd024337d..c327a8223 100644--- a/api/test/unit/controllers/test_search_controller_search_query.py+++ b/api/test/unit/controllers/test_search_controller_search_query.py@@ -4,6 +4,10 @@ import pytestfrom elasticsearch_dsl import Qfrom api.controllers import search_controller+from api.controllers.search_controller import (+ FILTERED_PROVIDERS_CACHE_KEY,+ FILTERED_PROVIDERS_CACHE_VERSION,+)pytestmark = pytest.mark.django_db@@ -11,14 +15,18 @@ pytestmark = pytest.mark.django_db@pytest.fixturedef excluded_providers_cache():- cache_key = "filtered_providers"excluded_provider = "excluded_provider"- cache_value = [{"provider_identifier": excluded_provider}]- cache.set(cache_key, cache_value, timeout=1)+ cache_value = [excluded_provider]+ cache.set(+ key=FILTERED_PROVIDERS_CACHE_KEY,+ version=FILTERED_PROVIDERS_CACHE_VERSION,+ value=cache_value,+ timeout=1,+ )yield excluded_provider- cache.delete(cache_key)+ cache.delete(FILTERED_PROVIDERS_CACHE_KEY, version=FILTERED_PROVIDERS_CACHE_VERSION)def test_create_search_query_empty(media_type_config):