def test_default_lang_pref_saved(self, lang): with mock.patch("django.conf.settings.LANGUAGE_CODE", lang): response = self.client.post(self.url, self.params) self.assertEqual(response.status_code, 200) user = User.objects.get(username=self.username) self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY), lang)
def test_header_lang_pref_saved(self, lang): response = self.client.post(self.url, self.params, HTTP_ACCEPT_LANGUAGE=lang) self.assertEqual(response.status_code, 200) user = User.objects.get(username=self.username) self.assertEqual(UserPreference.get_preference(user, LANGUAGE_KEY), lang)
def test_get_set_preference(self): # Checks that you can set a preference and get that preference later # Also, tests that no preference is returned for keys that are not set user = UserFactory.create() key = 'testkey' value = 'testvalue' # does a round trip UserPreference.set_preference(user, key, value) pref = UserPreference.get_preference(user, key) self.assertEqual(pref, value) # get preference for key that doesn't exist for user pref = UserPreference.get_preference(user, 'testkey_none') self.assertIsNone(pref)
def test_set_preference_missing_lang(self): user = UserFactory.create() self.client.login(username=user.username, password='******') response = self.client.post(reverse('lang_pref_set_language')) self.assertEquals(response.status_code, 400) self.assertIsNone(UserPreference.get_preference(user, LANGUAGE_KEY))
def claim_locale(self, data): """ Return the locale for the users based on their preferences. Does not return a value if the users have not set their locale preferences. """ language = UserPreference.get_preference(data['user'], LANGUAGE_KEY) return language
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
def test_set_preference_happy(self): user = UserFactory.create() self.client.login(username=user.username, password='******') 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 login(request): response = login_user(request, "") context = json.loads(response.content) if context.get("success", False): context.update({ "user_name": request.user.username, "user_full_name": request.user.profile.name, "language_code": UserPreference.get_preference(request.user, LANGUAGE_KEY), }) response.content = json.dumps(context, cls = DjangoJSONEncoder, indent = 2, ensure_ascii = False) return response
def claim_locale(self, data): """ Return the locale for the users based on their preferences. Does not return a value if the users have not set their locale preferences. """ language = UserPreference.get_preference(data['user'], LANGUAGE_KEY) # If the user has no language specified, return the default one. if not language: language = getattr(settings, 'LANGUAGE_CODE') return language