Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jlc/palm mig/language middleware #127

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions eox_nelp/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
classes:
ExtendedProfileFieldsMiddleware: Set extended_profile_fields in registration form.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add the new middleware here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
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
Expand Down Expand Up @@ -133,3 +135,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)
Loading