xmpp.chapril.org-conversejs/accounts/models.py

34 lines
1.2 KiB
Python
Raw Normal View History

2012-03-02 14:24:39 +01:00
from django.db import models
2012-03-02 14:43:41 +01:00
from django.dispatch import receiver
2012-03-02 14:24:39 +01:00
from django.contrib.auth.models import User
2012-03-02 14:43:41 +01:00
from django.conf import settings
2012-03-02 14:45:01 +01:00
from django.contrib.auth.signals import user_logged_in
from django.db.models.signals import post_save
2012-03-02 14:43:41 +01:00
from django.utils.translation import ugettext_lazy as _
2012-03-02 14:24:39 +01:00
2012-03-02 14:43:41 +01:00
from lang.models import Language
2012-03-02 14:24:39 +01:00
class Profile(models.Model):
user = models.ForeignKey(User, unique = True)
2012-03-02 14:43:41 +01:00
language = models.CharField(verbose_name=_(u"Interface Language"),
max_length=10, choices=settings.LANGUAGES)
languages = models.ManyToManyField(Language)
secondary_languages = models.ManyToManyField(Language, related_name = 'secondary_profile_set')
@receiver(user_logged_in)
def set_lang(sender, **kwargs):
lang_code = kwargs['user'].get_profile().language
kwargs['request'].session['django_language'] = lang_code
2012-03-02 14:24:39 +01:00
def create_profile_callback(sender, **kwargs):
'''
Automatically create profile when creating new user.
'''
if kwargs['created']:
profile, newprofile = Profile.objects.get_or_create(user = kwargs['instance'])
if newprofile:
profile.save
post_save.connect(create_profile_callback, sender = User)