-
Notifications
You must be signed in to change notification settings - Fork 4.3k
LMS: Adds Language Selection Menu #2419
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
895bb5e
LMS: adding language selector to user dashboard (WIP)
talbs cb6b52f
wiring for language selector
a520770
Added dark lang database fixture to bok choy tests
d867e87
Acceptance test for language selector
c41d814
LMS: simplifying language settings modal controls UI
talbs 7d81a51
Moved UserPreferences to common
5525726
Added middleware for handling language changes
77cce3a
Added acceptance test for language persistence
be189e6
Store user preferences for languages
dianakhuang a6f147a
LMS: revising modal UI to support translators
talbs e94b1a5
If we run into issues with languages, use default
dianakhuang 59aa06f
Add back in the fake2 language
dianakhuang a07d243
Clean up the display and dialog to use user preferences
dianakhuang a51ab4f
LMS: adding iconography to language pref on dashboard UI
talbs d00a0f5
LMS: adding in supplemental 'volunteer to be a translator' action to …
talbs cb209fa
LMS: revising color of language icon
talbs 4868679
Clean up tests.
dianakhuang 7e38c3a
Use a given default for getting a preference
dianakhuang f6e14e6
Change setlang url
dianakhuang afdd8d6
Better dashboard display behavior
dianakhuang c235ec3
Open up translation docs in separate tab.
dianakhuang efe7bda
Fixed broken acceptance tests
b61649f
Refactor out new lang_pref djangoapp
dianakhuang 0553754
Fix acceptance tests
ff0a6fb
Update cms urls for language preferences
dianakhuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| """ | ||
| Useful information for setting the language preference | ||
| """ | ||
|
|
||
| # this is the UserPreference key for the user's preferred language | ||
| LANGUAGE_KEY = 'pref-lang' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| """ | ||
| Middleware for Language Preferences | ||
| """ | ||
|
|
||
| from user_api.models import UserPreference | ||
| from lang_pref import LANGUAGE_KEY | ||
|
|
||
|
|
||
| class LanguagePreferenceMiddleware(object): | ||
| """ | ||
| Middleware for user preferences. | ||
|
|
||
| Ensures that, once set, a user's preferences are reflected in the page | ||
| whenever they are logged in. | ||
| """ | ||
|
|
||
| def process_request(self, request): | ||
| """ | ||
| If a user's UserPreference contains a language preference and there is | ||
| no language set on the session (i.e. from dark language overrides), use the user's preference. | ||
| """ | ||
| if request.user.is_authenticated() and 'django_language' not in request.session: | ||
| user_pref = UserPreference.get_preference(request.user, LANGUAGE_KEY) | ||
| if user_pref: | ||
| request.session['django_language'] = user_pref |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| from django.test import TestCase | ||
| from django.test.client import RequestFactory | ||
| from django.contrib.sessions.middleware import SessionMiddleware | ||
|
|
||
| from lang_pref.middleware import LanguagePreferenceMiddleware | ||
| from user_api.models import UserPreference | ||
| from lang_pref import LANGUAGE_KEY | ||
| from student.tests.factories import UserFactory | ||
|
|
||
|
|
||
| class TestUserPreferenceMiddleware(TestCase): | ||
| """ | ||
| Tests to make sure user preferences are getting properly set in the middleware | ||
| """ | ||
|
|
||
| def setUp(self): | ||
| self.middleware = LanguagePreferenceMiddleware() | ||
| self.session_middleware = SessionMiddleware() | ||
| self.user = UserFactory.create() | ||
| self.request = RequestFactory().get('/somewhere') | ||
| self.request.user = self.user | ||
| self.session_middleware.process_request(self.request) | ||
|
|
||
| def test_no_language_set_in_session_or_prefs(self): | ||
| # nothing set in the session or the prefs | ||
| self.middleware.process_request(self.request) | ||
| self.assertNotIn('django_language', self.request.session) | ||
|
|
||
| def test_language_in_user_prefs(self): | ||
| # language set in the user preferences and not the session | ||
| UserPreference.set_preference(self.user, LANGUAGE_KEY, 'eo') | ||
| self.middleware.process_request(self.request) | ||
| self.assertEquals(self.request.session['django_language'], 'eo') | ||
|
|
||
| def test_language_in_session(self): | ||
| # language set in both the user preferences and session, | ||
| # session should get precedence | ||
| self.request.session['django_language'] = 'en' | ||
| UserPreference.set_preference(self.user, LANGUAGE_KEY, 'eo') | ||
| self.middleware.process_request(self.request) | ||
|
|
||
| self.assertEquals(self.request.session['django_language'], 'en') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| """ | ||
| Tests for the language setting view | ||
| """ | ||
| from django.core.urlresolvers import reverse | ||
| from django.test import TestCase | ||
| from student.tests.factories import UserFactory | ||
| from user_api.models import UserPreference | ||
| from lang_pref import LANGUAGE_KEY | ||
|
|
||
|
|
||
| class TestLanguageSetting(TestCase): | ||
| """ | ||
| Test setting languages | ||
| """ | ||
| def test_set_preference_happy(self): | ||
| user = UserFactory.create() | ||
| self.client.login(username=user.username, password='test') | ||
|
|
||
| lang = 'en' | ||
| response = self.client.post(reverse('lang_pref_set_language'), {'language': lang}) | ||
|
|
||
| self.assertEquals(response.status_code, 200) | ||
| user_pref = UserPreference.get_preference(user, LANGUAGE_KEY) | ||
| self.assertEqual(user_pref, lang) | ||
|
|
||
| def test_set_preference_missing_lang(self): | ||
| user = UserFactory.create() | ||
| self.client.login(username=user.username, password='test') | ||
|
|
||
| response = self.client.post(reverse('lang_pref_set_language')) | ||
|
|
||
| self.assertEquals(response.status_code, 400) | ||
|
|
||
| self.assertIsNone(UserPreference.get_preference(user, LANGUAGE_KEY)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| """ | ||
| Urls for managing language preferences | ||
| """ | ||
|
|
||
| from django.conf.urls import patterns, url | ||
|
|
||
| urlpatterns = patterns( | ||
| '', | ||
| url(r'^setlang/', 'lang_pref.views.set_language', name='lang_pref_set_language') | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| """ | ||
| Views for accessing language preferences | ||
| """ | ||
| from django.contrib.auth.decorators import login_required | ||
| from django.http import HttpResponse, HttpResponseBadRequest | ||
|
|
||
| from user_api.models import UserPreference | ||
| from lang_pref import LANGUAGE_KEY | ||
|
|
||
|
|
||
| @login_required | ||
| def set_language(request): | ||
| """ | ||
| This view is called when the user would like to set a language preference | ||
| """ | ||
| user = request.user | ||
| lang_pref = request.POST.get('language', None) | ||
|
|
||
| if lang_pref: | ||
| UserPreference.set_preference(user, LANGUAGE_KEY, lang_pref) | ||
| return HttpResponse('{"success": true}') | ||
|
|
||
| return HttpResponseBadRequest('no language provided') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from django.contrib.auth.models import User | ||
| from django.db import models | ||
|
|
||
|
|
||
| class UserPreference(models.Model): | ||
| """A user's preference, stored as generic text to be processed by client""" | ||
| user = models.ForeignKey(User, db_index=True, related_name="+") | ||
| key = models.CharField(max_length=255, db_index=True) | ||
| value = models.TextField() | ||
|
|
||
| class Meta: | ||
| unique_together = ("user", "key") | ||
|
|
||
| @classmethod | ||
| def set_preference(cls, user, preference_key, preference_value): | ||
| """ | ||
| Sets the user preference for a given key | ||
| """ | ||
| user_pref, _ = cls.objects.get_or_create(user=user, key=preference_key) | ||
| user_pref.value = preference_value | ||
| user_pref.save() | ||
|
|
||
| @classmethod | ||
| def get_preference(cls, user, preference_key, default=None): | ||
| """ | ||
| Gets the user preference value for a given key | ||
|
|
||
| Returns the given default if there isn't a preference for the given key | ||
| """ | ||
|
|
||
| try: | ||
| user_pref = cls.objects.get(user=user, key=preference_key) | ||
| return user_pref.value | ||
| except cls.DoesNotExist: | ||
| return default |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's not a whole lot of English here to explain what you're testing and why