Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
c9f6872b
Commit
c9f6872b
authored
May 10, 2017
by
Calen Pennington
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Capture the language preference set by an anonymous user when the register or login
[LEARNER-168]
parent
956207b9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
20 deletions
+79
-20
common/djangoapps/student/tests/factories.py
+3
-1
openedx/core/djangoapps/lang_pref/middleware.py
+28
-19
openedx/core/djangoapps/lang_pref/tests/test_middleware.py
+48
-0
No files found.
common/djangoapps/student/tests/factories.py
View file @
c9f6872b
...
...
@@ -84,9 +84,11 @@ class UserFactory(DjangoModelFactory):
model
=
User
django_get_or_create
=
(
'email'
,
'username'
)
_DEFAULT_PASSWORD
=
'test'
username
=
factory
.
Sequence
(
u'robot{0}'
.
format
)
email
=
factory
.
Sequence
(
u'robot+test+{0}@edx.org'
.
format
)
password
=
factory
.
PostGenerationMethodCall
(
'set_password'
,
'test'
)
password
=
factory
.
PostGenerationMethodCall
(
'set_password'
,
_DEFAULT_PASSWORD
)
first_name
=
factory
.
Sequence
(
u'Robot{0}'
.
format
)
last_name
=
'Test'
is_staff
=
False
...
...
openedx/core/djangoapps/lang_pref/middleware.py
View file @
c9f6872b
...
...
@@ -32,6 +32,8 @@ class LanguagePreferenceMiddleware(object):
if
cookie_lang
:
if
request
.
user
.
is_authenticated
():
set_user_preference
(
request
.
user
,
LANGUAGE_KEY
,
cookie_lang
)
else
:
request
.
_anonymous_user_cookie_lang
=
cookie_lang
accept_header
=
request
.
META
.
get
(
LANGUAGE_HEADER
,
None
)
if
accept_header
:
...
...
@@ -51,25 +53,32 @@ class LanguagePreferenceMiddleware(object):
def
process_response
(
self
,
request
,
response
):
# If the user is logged in, check for their language preference
if
getattr
(
request
,
'user'
,
None
)
and
request
.
user
.
is_authenticated
():
# Get the user's language preference
try
:
user_pref
=
get_user_preference
(
request
.
user
,
LANGUAGE_KEY
)
except
(
UserAPIRequestError
,
UserAPIInternalError
):
# If we can't find the user preferences, then don't modify the cookie
pass
user_pref
=
None
anonymous_cookie_lang
=
getattr
(
request
,
'_anonymous_user_cookie_lang'
,
None
)
if
anonymous_cookie_lang
:
user_pref
=
anonymous_cookie_lang
set_user_preference
(
request
.
user
,
LANGUAGE_KEY
,
anonymous_cookie_lang
)
else
:
# Get the user's language preference
try
:
user_pref
=
get_user_preference
(
request
.
user
,
LANGUAGE_KEY
)
except
(
UserAPIRequestError
,
UserAPIInternalError
):
# If we can't find the user preferences, then don't modify the cookie
pass
# If set, set the user_pref in the LANGUAGE_COOKIE
if
user_pref
:
response
.
set_cookie
(
settings
.
LANGUAGE_COOKIE
,
value
=
user_pref
,
domain
=
settings
.
SESSION_COOKIE_DOMAIN
,
max_age
=
COOKIE_DURATION
,
)
else
:
# Set it in the LANGUAGE_COOKIE
if
user_pref
:
response
.
set_cookie
(
settings
.
LANGUAGE_COOKIE
,
value
=
user_pref
,
domain
=
settings
.
SESSION_COOKIE_DOMAIN
,
max_age
=
COOKIE_DURATION
,
)
else
:
response
.
delete_cookie
(
settings
.
LANGUAGE_COOKIE
,
domain
=
settings
.
SESSION_COOKIE_DOMAIN
)
response
.
delete_cookie
(
settings
.
LANGUAGE_COOKIE
,
domain
=
settings
.
SESSION_COOKIE_DOMAIN
)
return
response
openedx/core/djangoapps/lang_pref/tests/test_middleware.py
View file @
c9f6872b
...
...
@@ -168,6 +168,54 @@ class TestUserPreferenceMiddleware(TestCase):
else
:
self
.
assertNotIn
(
settings
.
LANGUAGE_COOKIE
,
self
.
client
.
cookies
)
@ddt.data
(
(
None
,
None
),
(
'es'
,
'es-419'
),
(
'en'
,
'en'
),
(
'es-419'
,
'es-419'
)
)
@ddt.unpack
def
test_login_captures_lang_pref
(
self
,
lang_cookie
,
expected_lang
):
if
lang_cookie
:
self
.
client
.
cookies
[
settings
.
LANGUAGE_COOKIE
]
=
lang_cookie
elif
settings
.
LANGUAGE_COOKIE
in
self
.
client
.
cookies
:
del
self
.
client
.
cookies
[
settings
.
LANGUAGE_COOKIE
]
# Use an actual call to the login endpoint, to validate that the middleware
# stack does the right thing
if
settings
.
FEATURES
.
get
(
'ENABLE_COMBINED_LOGIN_REGISTRATION'
):
response
=
self
.
client
.
post
(
reverse
(
'user_api_login_session'
),
data
=
{
'email'
:
self
.
user
.
email
,
'password'
:
UserFactory
.
_DEFAULT_PASSWORD
,
'remember'
:
True
,
}
)
else
:
response
=
self
.
client
.
post
(
reverse
(
'login_post'
),
data
=
{
'email'
:
self
.
user
.
email
,
'password'
:
UserFactory
.
_DEFAULT_PASSWORD
,
'honor_code'
:
True
,
}
)
self
.
assertEqual
(
response
.
status_code
,
200
)
if
lang_cookie
:
self
.
assertEqual
(
response
[
'Content-Language'
],
expected_lang
)
self
.
assertEqual
(
get_user_preference
(
self
.
user
,
LANGUAGE_KEY
),
lang_cookie
)
self
.
assertEqual
(
self
.
client
.
cookies
[
settings
.
LANGUAGE_COOKIE
]
.
value
,
lang_cookie
)
else
:
self
.
assertEqual
(
response
[
'Content-Language'
],
'en'
)
self
.
assertEqual
(
get_user_preference
(
self
.
user
,
LANGUAGE_KEY
),
None
)
self
.
assertEqual
(
self
.
client
.
cookies
[
settings
.
LANGUAGE_COOKIE
]
.
value
,
''
)
def
test_process_response_no_user_noop
(
self
):
del
self
.
request
.
user
response
=
mock
.
Mock
(
spec
=
HttpResponse
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment