Implement GUI language and timezone as user preferences, issue #114

This commit is contained in:
Roberto Rosario
2015-01-11 17:38:47 -04:00
parent 305c1aaa6e
commit 090302676d
11 changed files with 311 additions and 18 deletions
+25 -3
View File
@@ -3,27 +3,33 @@ from __future__ import absolute_import
import logging
import tempfile
from django.conf import settings
from django.contrib.auth import models as auth_models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.signals import user_logged_in
from south.signals import post_migrate
from navigation.api import register_links, register_top_menu
from common import settings as common_settings
from .links import (link_about, link_current_user_details,
link_current_user_edit, link_license,
link_current_user_edit,
link_current_user_locale_profile_details,
link_current_user_locale_profile_edit, link_license,
link_password_change)
from .models import AnonymousUserSingleton, AutoAdminSingleton
from .models import (AnonymousUserSingleton, AutoAdminSingleton,
UserLocaleProfile)
from .settings import (AUTO_ADMIN_USERNAME, AUTO_ADMIN_PASSWORD,
AUTO_CREATE_ADMIN, TEMPORARY_DIRECTORY)
from .utils import validate_path
logger = logging.getLogger(__name__)
register_links(['common:current_user_details', 'common:current_user_edit', 'common:password_change_view'], [link_current_user_details, link_current_user_edit, link_password_change], menu_name='secondary_menu')
register_links(['common:current_user_details', 'common:current_user_edit', 'common:current_user_locale_profile_details', 'common:current_user_locale_profile_edit', 'common:password_change_view'], [link_current_user_details, link_current_user_edit, link_current_user_locale_profile_details, link_current_user_locale_profile_edit, link_password_change], menu_name='secondary_menu')
register_links(['common:about_view', 'common:license_view', 'registration:form_view'], [link_about, link_license], menu_name='secondary_menu')
register_top_menu('about', link_about, position=-1)
@@ -73,5 +79,21 @@ def auto_admin_account_passwd_change(sender, instance, **kwargs):
auto_admin_properties.save()
@receiver(user_logged_in, dispatch_uid='user_locale_profile_session_config', sender=User)
def user_locale_profile_session_config(sender, request, user, **kwargs):
if hasattr(request, 'session'):
user_locale_profile, created = UserLocaleProfile.objects.get_or_create(user=user)
request.session['django_language'] = user_locale_profile.language
request.session['django_timezone'] = user_locale_profile.timezone
else:
request.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_locale_profile.language)
@receiver(post_save, dispatch_uid='user_locale_profile_create', sender=User)
def user_locale_profile_create(sender, instance, created, **kwargs):
if created:
UserLocaleProfile.objects.create(user=instance)
if (not validate_path(TEMPORARY_DIRECTORY)) or (not TEMPORARY_DIRECTORY):
setattr(common_settings, 'TEMPORARY_DIRECTORY', tempfile.mkdtemp())