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

46 lines
1.5 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):
2012-03-02 16:16:20 +01:00
user = models.ForeignKey(User, unique = True, editable = False)
2012-03-02 16:18:12 +01:00
language = models.CharField(
verbose_name = _(u"Interface Language"),
max_length = 10,
choices = settings.LANGUAGES
)
languages = models.ManyToManyField(
Language,
verbose_name = _('Languages')
)
secondary_languages = models.ManyToManyField(
Language,
verbose_name = _('Secondary languages'),
related_name = 'secondary_profile_set'
)
2012-03-05 09:52:24 +01:00
suggested = models.IntegerField(default = 0, db_index = True)
translated = models.IntegerField(default = 0, db_index = True)
2012-03-02 14:43:41 +01:00
@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)