Skip to content

Commit

Permalink
chore: refactor search settings for reusability
Browse files Browse the repository at this point in the history
Moving settings from cms/envs/common.py makes it possible to reuse
Meilisearch settings in the LMS without duplicating the settings (see
OEP-45).
  • Loading branch information
regisb committed Oct 16, 2024
1 parent f41798a commit 8b58f95
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
15 changes: 0 additions & 15 deletions cms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 13 additions & 14 deletions openedx/core/djangoapps/content/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
24 changes: 24 additions & 0 deletions openedx/core/djangoapps/content/search/settings.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 8b58f95

Please sign in to comment.