Skip to content

Commit

Permalink
feat: add user lang cookie preference
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed May 7, 2024
1 parent 92bd3d9 commit d2607eb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
9 changes: 9 additions & 0 deletions eox_core/edxapp_wrapper/backends/lang_pref_middleware_p_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
LanguagePreferenceMiddleware Backend.
"""
from openedx.core.djangoapps.lang_pref.middleware import LanguagePreferenceMiddleware # pylint: disable=import-error


def get_language_preference_middleware():
"""Backend to get the LanguagePreferenceMiddleware from openedx."""
return LanguagePreferenceMiddleware
11 changes: 11 additions & 0 deletions eox_core/edxapp_wrapper/language_preference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
""" Backend abstraction. """
from importlib import import_module

from django.conf import settings


def get_language_preference_middleware(*args, **kwargs):
""" Get LanguagePreferenceMiddleware. """
backend_function = settings.EOX_CORE_LANG_PREF_BACKEND
backend = import_module(backend_function)
return backend.get_language_preference_middleware(*args, **kwargs)
23 changes: 23 additions & 0 deletions eox_core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import re
from urllib.parse import urlparse
from django.http import parse_cookie

import six
from django.conf import settings
Expand All @@ -25,6 +26,7 @@

from eox_core.edxapp_wrapper.configuration_helpers import get_configuration_helper
from eox_core.edxapp_wrapper.third_party_auth import get_tpa_exception_middleware
from eox_core.edxapp_wrapper.language_preference import get_language_preference_middleware
from eox_core.models import Redirection
from eox_core.utils import cache, fasthash

Expand All @@ -38,6 +40,7 @@ class EoxTenantAuthException:

configuration_helper = get_configuration_helper() # pylint: disable=invalid-name
ExceptionMiddleware = get_tpa_exception_middleware()
LanguagePreferenceMiddleware = get_language_preference_middleware()

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -282,3 +285,23 @@ def process_exception(self, request, exception):
return super().process_exception(request, new_exception)

return super().process_exception(request, exception)

class UserLanguagePreferenceMiddleware(LanguagePreferenceMiddleware):
"""This Middleware allows the user set the language preference for the site, avoiding the default LANGUAGE_CODE.
The previous behavior was modified here
https://github.com/openedx/edx-platform/blob/open-release/palm.master/openedx/core/djangoapps/lang_pref/middleware.py#L61-L62
"""
def process_request(self, request):
"""
If a user's UserPreference contains a language preference, use the user's preference.
Save the current language preference cookie as the user's preferred language.
"""
original_user_language_cookie = parse_cookie(request.META.get("HTTP_COOKIE", "")).get(
settings.LANGUAGE_COOKIE_NAME
)

if original_user_language_cookie:
request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = original_user_language_cookie

return self.get_response(request)
1 change: 1 addition & 0 deletions eox_core/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def plugin_settings(settings):
settings.EOX_CORE_BEARER_AUTHENTICATION = 'eox_core.edxapp_wrapper.backends.bearer_authentication_j_v1'
settings.EOX_CORE_ASYNC_TASKS = []
settings.EOX_CORE_THIRD_PARTY_AUTH_BACKEND = 'eox_core.edxapp_wrapper.backends.third_party_auth_l_v1'
settings.EOX_CORE_LANG_PREF_BACKEND = 'eox_core.edxapp_wrapper.backends.lang_pref_middleware_p_v1'

if settings.EOX_CORE_USER_ENABLE_MULTI_TENANCY:
settings.EOX_CORE_USER_ORIGIN_SITE_SOURCES = [
Expand Down
7 changes: 6 additions & 1 deletion eox_core/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ def plugin_settings(settings): # pylint: disable=function-redefined
'eox_core.middleware.RedirectionsMiddleware',
'eox_core.middleware.TPAExceptionMiddleware'
]


settings.MIDDLEWARE.insert(
settings.MIDDLEWARE.index("openedx.core.djangoapps.lang_pref.middleware.LanguagePreferenceMiddleware") + 1,
"eox_core.middleware.UserLanguagePreferenceMiddleware",
)

# Sentry Integration
sentry_integration_dsn = getattr(settings, 'ENV_TOKENS', {}).get(
'EOX_CORE_SENTRY_INTEGRATION_DSN',
Expand Down

0 comments on commit d2607eb

Please sign in to comment.