Thomas Waldmann aa425c3402 add UserProfiles with additional metadata (for now: language)
as the migrations depend on settings.LANGUAGES, I put a complete list there with what we have .po files for.
2014-11-25 14:08:48 +01:00

45 lines
1.5 KiB
Python

"""
models for account-related stuff
"""
from django.db import models
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.signals import user_logged_in
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import LANGUAGE_SESSION_KEY
class UserProfile(models.Model):
"""
stuff we need additionally to what Django stores in User model
"""
user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True, related_name='profile',
verbose_name=_('user'))
language = models.CharField(max_length=10, choices=settings.LANGUAGES,
default='', blank=True, null=True,
verbose_name=_('language'))
def __unicode__(self):
return u"profile for %s" % self.user.__unicode__()
class Meta:
verbose_name = _('user profile')
verbose_name_plural = _('user profiles')
def create_user_profile(sender, instance=None, created=False, **kwargs):
# if a new user is created, create the UserProfile also:
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=settings.AUTH_USER_MODEL)
@receiver(user_logged_in)
def lang(sender, user=None, request=None, **kwargs):
# if a user logs in, activate language preference from profile
request.session[LANGUAGE_SESSION_KEY] = user.profile.language