Skip to content

Commit

Permalink
feat: add PreserveUserLanguageCookieMiddleware
Browse files Browse the repository at this point in the history
This ensure to use the cookie language if the user send it.
  • Loading branch information
johanseto committed Feb 29, 2024
1 parent bcb95dd commit 6129772
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions eox_nelp/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 6129772

Please sign in to comment.