From 8b58f9559c8f331df2b2c85283317e751ae48a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Wed, 16 Oct 2024 15:40:35 +0200 Subject: [PATCH] chore: refactor search settings for reusability Moving settings from cms/envs/common.py makes it possible to reuse Meilisearch settings in the LMS without duplicating the settings (see OEP-45). --- cms/envs/common.py | 15 ----------- openedx/core/djangoapps/content/search/api.py | 27 +++++++++---------- .../djangoapps/content/search/settings.py | 24 +++++++++++++++++ 3 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 openedx/core/djangoapps/content/search/settings.py diff --git a/cms/envs/common.py b/cms/envs/common.py index 63221ee0b0a4..ed2f16b5a634 100644 --- a/cms/envs/common.py +++ b/cms/envs/common.py @@ -2906,21 +2906,6 @@ def _should_send_learning_badge_events(settings): BEAMER_PRODUCT_ID = "" -################### Studio Search (beta), using Meilisearch ################### - -# Enable Studio search features (powered by Meilisearch) (beta, off by default) -MEILISEARCH_ENABLED = False -# Meilisearch URL that the python backend can use. Often points to another docker container or k8s service. -MEILISEARCH_URL = "http://meilisearch" -# URL that browsers (end users) can use to reach Meilisearch. Should be HTTPS in production. -MEILISEARCH_PUBLIC_URL = "http://meilisearch.example.com" -# To support multi-tenancy, you can prefix all indexes with a common key like "sandbox7-" -# and use a restricted tenant token in place of an API key, so that this Open edX instance -# can only use the index(es) that start with this prefix. -# See https://www.meilisearch.com/docs/learn/security/tenant_tokens -MEILISEARCH_INDEX_PREFIX = "" -MEILISEARCH_API_KEY = "devkey" - # .. setting_name: DISABLED_COUNTRIES # .. setting_default: [] # .. setting_description: List of country codes that should be disabled diff --git a/openedx/core/djangoapps/content/search/api.py b/openedx/core/djangoapps/content/search/api.py index 17338f20ab83..80da89fb2734 100644 --- a/openedx/core/djangoapps/content/search/api.py +++ b/openedx/core/djangoapps/content/search/api.py @@ -10,7 +10,6 @@ from functools import wraps from typing import Callable, Generator -from django.conf import settings from django.contrib.auth import get_user_model from django.core.cache import cache from django.core.paginator import Paginator @@ -39,18 +38,21 @@ searchable_doc_tags, searchable_doc_tags_for_collection, ) +from .settings import ( + MEILISEARCH_ENABLED, + MEILISEARCH_INDEX_PREFIX, + MEILISEARCH_API_KEY, + MEILISEARCH_URL, + MEILISEARCH_PUBLIC_URL, +) log = logging.getLogger(__name__) User = get_user_model() -STUDIO_INDEX_SUFFIX = "studio_content" - -if hasattr(settings, "MEILISEARCH_INDEX_PREFIX"): - STUDIO_INDEX_NAME = settings.MEILISEARCH_INDEX_PREFIX + STUDIO_INDEX_SUFFIX -else: - STUDIO_INDEX_NAME = STUDIO_INDEX_SUFFIX +STUDIO_INDEX_SUFFIX = "studio_content" +STUDIO_INDEX_NAME = MEILISEARCH_INDEX_PREFIX + STUDIO_INDEX_SUFFIX _MEILI_CLIENT = None _MEILI_API_KEY_UID = None @@ -104,7 +106,7 @@ def _get_meilisearch_client(): if _MEILI_CLIENT is not None: return _MEILI_CLIENT - _MEILI_CLIENT = MeilisearchClient(settings.MEILISEARCH_URL, settings.MEILISEARCH_API_KEY) + _MEILI_CLIENT = MeilisearchClient(MEILISEARCH_URL, MEILISEARCH_API_KEY) try: _MEILI_CLIENT.health() except MeilisearchError as err: @@ -125,7 +127,7 @@ def _get_meili_api_key_uid(): """ global _MEILI_API_KEY_UID # pylint: disable=global-statement if _MEILI_API_KEY_UID is None: - _MEILI_API_KEY_UID = _get_meilisearch_client().get_key(settings.MEILISEARCH_API_KEY).uid + _MEILI_API_KEY_UID = _get_meilisearch_client().get_key(MEILISEARCH_API_KEY).uid return _MEILI_API_KEY_UID @@ -273,10 +275,7 @@ def is_meilisearch_enabled() -> bool: """ Returns whether Meilisearch is enabled """ - if hasattr(settings, "MEILISEARCH_ENABLED"): - return settings.MEILISEARCH_ENABLED - - return False + return MEILISEARCH_ENABLED # pylint: disable=too-many-statements @@ -757,7 +756,7 @@ def generate_user_token_for_studio_search(request): ) return { - "url": settings.MEILISEARCH_PUBLIC_URL, + "url": MEILISEARCH_PUBLIC_URL, "index_name": STUDIO_INDEX_NAME, "api_key": restricted_api_key, } diff --git a/openedx/core/djangoapps/content/search/settings.py b/openedx/core/djangoapps/content/search/settings.py new file mode 100644 index 000000000000..2de36b9acba1 --- /dev/null +++ b/openedx/core/djangoapps/content/search/settings.py @@ -0,0 +1,24 @@ +""" +Expose settings for this application. + +Instead of calling settings.MEILISEARCH_*, developers are encouraged to import settings +from here. +""" + +from django.conf import settings + +# Enable Studio search features (powered by Meilisearch) (beta, off by default) +MEILISEARCH_ENABLED = getattr(settings, "MEILISEARCH_ENABLED", False) +# Meilisearch URL that the python backend can use. Often points to another docker container or k8s service. +MEILISEARCH_URL = getattr(settings, "MEILISEARCH_URL", "http://meilisearch") +# URL that browsers (end users) can use to reach Meilisearch. Should be HTTPS in production. +MEILISEARCH_PUBLIC_URL = getattr(settings, "MEILISEARCH_PUBLIC_URL", "http://meilisearch.example.com") +# To support multi-tenancy, you can prefix all indexes with a common key like "sandbox7-" +# and use a restricted tenant token in place of an API key, so that this Open edX instance +# can only use the index(es) that start with this prefix. +# See https://www.meilisearch.com/docs/learn/security/tenant_tokens +MEILISEARCH_INDEX_PREFIX = getattr(settings, "MEILISEARCH_INDEX_PREFIX", "") +# Access key: note that there should be no need to use a master key. Instead, good +# practices dictate to create an API key that can only access indices with the +# MEILISEARCH_INDEX_PREFIX. +MEILISEARCH_API_KEY = getattr(settings, "MEILISEARCH_API_KEY", "devkey")