diff --git a/eox_nelp/middleware.py b/eox_nelp/middleware.py index 426fb38d..f336ac04 100644 --- a/eox_nelp/middleware.py +++ b/eox_nelp/middleware.py @@ -5,6 +5,9 @@ classes: ExtendedProfileFieldsMiddleware: Set extended_profile_fields in registration form. """ +from django.conf import settings +from django.http import parse_cookie +from django.utils.deprecation import MiddlewareMixin from django.utils.translation import gettext_lazy as _ from eox_nelp.edxapp_wrapper.site_configuration import configuration_helpers @@ -133,3 +136,21 @@ 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(MiddlewareMixin): + """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 process_request(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