<commit_before>from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.dispatch import receiver
from django.contrib.auth.signals import user_logged_in


class Structure(models.Model):
    name = models.CharField(max_length=256)

    def __unicode__(self):
        return self.name


def default_structure():
    return Structure.objects.get_or_create(name=settings.DEFAULT_STRUCTURE_NAME)[0]


class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True)

    language = models.CharField(_('Language'), max_length=10, choices=settings.LANGUAGES,
                                default=settings.LANGUAGE_CODE)
    structure = models.ForeignKey(Structure, default=default_structure)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])


@receiver(user_logged_in)
def lang(sender, **kwargs):
    """ Set user's language in session when he logs in. """
    lang_code = kwargs['user'].profile.language
    kwargs['request'].session['django_language'] = lang_code


class StructureRelatedManager(models.Manager):
    def for_user(self, user):
        qs = super(StructureRelatedManager, self).get_query_set()
        return qs.filter(structure=user.profile.structure)


class StructureRelated(models.Model):
    """A mixin used for any entities that belong to a structure"""

    structure = models.ForeignKey(Structure, default=default_structure)

    objects = StructureRelatedManager()

    class Meta:
        abstract = True<commit_msg>Structure related class (ref #15)<commit_after>from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.dispatch import receiver
from django.contrib.auth.signals import user_logged_in


class Structure(models.Model):
    """
    Represents an organisational structure, to which users are related.
    """
    name = models.CharField(max_length=256)

    def __unicode__(self):
        return self.name


def default_structure():
    return Structure.objects.get_or_create(name=settings.DEFAULT_STRUCTURE_NAME)[0]


class StructureRelated(models.Model):
    """
    A mixin used for any entities that belong to a structure
    """
    structure = models.ForeignKey(Structure, default=default_structure)

    @classmethod
    def forUser(cls, user):
        qs = cls.objects.get_query_set()
        return qs.filter(structure=user.profile.structure)

    class Meta:
        abstract = True


class UserProfile(StructureRelated):
    user = models.OneToOneField(User, unique=True)

    language = models.CharField(_('Language'), max_length=10,
                                choices=settings.LANGUAGES,
                                default=settings.LANGUAGE_CODE)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])


@receiver(user_logged_in)
def lang(sender, **kwargs):
    """ Set user's language in session when he logs in. """
    lang_code = kwargs['user'].profile.language
    kwargs['request'].session['django_language'] = lang_code
{'fraction_non_alphanumeric': 0.09768315591734503, 'fraction_numerical': 0.005009392611145898, 'mean_word_length': 4.031496062992126, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5287188', 'n_tokens_mistral': 925, 'n_tokens_neox': 877, 'n_words': 211}