Added middleware for handling language changes

This commit is contained in:
Julia Hansbrough
2014-02-11 19:19:51 +00:00
parent 7d81a510bc
commit 552572694f
2 changed files with 28 additions and 0 deletions

View File

@@ -462,6 +462,7 @@ INSTALLED_APPS = (
# User preferences
'user_api',
'django_openid_auth',
)

View File

@@ -0,0 +1,27 @@
"""
Middleware for UserPreferences
"""
from django.utils.translation.trans_real import parse_accept_lang_header
from user_api.models import UserPreference, LANGUAGE_KEY
class UserPreferenceMiddleware(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,
stick that preference in the session.
"""
query = UserPreference.objects.filter(user=request.user, key=LANGUAGE_KEY)
if query.exists():
# there should only be one result for query
request.session['django_language'] = query[0].value