diff --git a/eox_nelp/middleware.py b/eox_nelp/middleware.py index 426fb38d..1642444c 100644 --- a/eox_nelp/middleware.py +++ b/eox_nelp/middleware.py @@ -4,7 +4,10 @@ classes: ExtendedProfileFieldsMiddleware: Set extended_profile_fields in registration form. + PreserveUserLanguageCookieMiddleware: Set the LANGUAGE cookie name with the original cookie sent by user. """ +from django.conf import settings +from django.http import parse_cookie from django.utils.translation import gettext_lazy as _ from eox_nelp.edxapp_wrapper.site_configuration import configuration_helpers @@ -133,3 +136,26 @@ def handler(form_instance, form_desc, required=True): # pylint: disable=protected-access form_instance._add_field_with_configurable_select_options(field, label, form_desc, required=required) return handler + + +class PreserveUserLanguageCookieMiddleware: + """This middleware ensure that in the COOKIES property the LANGUAGE_COOKIE_NAME + key has to be the cookie sent by the user and not other, + because it could be modified previously by other. + eg. + https://github.com/openedx/edx-platform/blob/open-release/palm.master/openedx/ + core/djangoapps/lang_pref/middleware.py#L61-L62 + """ + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + """Process the request to change the language cookie based in original cookie value.""" + 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)