<commit_before># -*- coding: utf-8 -*-

"""
    Models to manage users and profiles
"""
from django.db import models
from django.contrib.auth.models import User, Group
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, verbose_name=_(u"Nom"))

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = _(u"Structure")
        verbose_name_plural = _(u"Structures")


def default_structure():
    """ Create default structure if necessary """
    return Structure.objects.get_or_create(name=settings.DEFAULT_STRUCTURE_NAME)[0]


class StructureRelatedManager(models.Manager):
    """ A simple manager to manage structure related objects"""
    def byUser(self, user):
        """ Filter by user's structure """
        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, verbose_name=_(u"Related structure"))

    objects = models.Manager()
    in_structure = StructureRelatedManager()

    @classmethod
    def forUser(cls, user):
        """ Shortcut to manager's filter by user """
        return cls.in_structure.byUser(user)

    class Meta:
        abstract = True
        verbose_name = _(u"Related structures")
        verbose_name_plural = _(u"Related structure")


class UserProfile(StructureRelated):
    """
    A custom user profile
    """
    user = models.OneToOneField(User, unique=True)

    language = models.CharField(_(u"Language"), max_length=10,
                                choices=settings.LANGUAGES,
                                default=settings.LANGUAGE_CODE)
    class Meta:
        verbose_name = _(u"User's profile")
        verbose_name_plural = _(u"User's profiles")

    def __unicode__(self):
        return _("Profile for %s") % self.user

    def has_group(self, g):
        return self.user.groups.filter(pk=g.pk).exists()

    def is_path_manager(self):
        g = Group.objects.get(name='Référents sentiers')
        return self.has_group(g)

    def is_comm_manager(self):
        g = Group.objects.get(name='Référents communication')
        return self.has_group(g)

    def is_editor(self):
        g = Group.objects.get(name='Rédacteurs')
        return self.has_group(g)

    def is_administrator(self):
        g = Group.objects.get(name='Administrateurs')
        return self.has_group(g)

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
<commit_msg>Minor change: use constant instead of raw strings<commit_after># -*- coding: utf-8 -*-

"""
    Models to manage users and profiles
"""
from django.db import models
from django.contrib.auth.models import User, Group
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

GROUP_PATH_MANAGER = 'Référents sentiers'
GROUP_COMM_MANAGER = 'Référents communication'
GROUP_EDITOR = 'Rédacteurs'
GROUP_ADMINISTRATOR = 'Administrateurs'


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

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = _(u"Structure")
        verbose_name_plural = _(u"Structures")


def default_structure():
    """ Create default structure if necessary """
    return Structure.objects.get_or_create(name=settings.DEFAULT_STRUCTURE_NAME)[0]


class StructureRelatedManager(models.Manager):
    """ A simple manager to manage structure related objects"""
    def byUser(self, user):
        """ Filter by user's structure """
        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, verbose_name=_(u"Related structure"))

    objects = models.Manager()
    in_structure = StructureRelatedManager()

    @classmethod
    def forUser(cls, user):
        """ Shortcut to manager's filter by user """
        return cls.in_structure.byUser(user)

    class Meta:
        abstract = True
        verbose_name = _(u"Related structures")
        verbose_name_plural = _(u"Related structure")


class UserProfile(StructureRelated):
    """
    A custom user profile
    """
    user = models.OneToOneField(User, unique=True)

    language = models.CharField(_(u"Language"), max_length=10,
                                choices=settings.LANGUAGES,
                                default=settings.LANGUAGE_CODE)
    class Meta:
        verbose_name = _(u"User's profile")
        verbose_name_plural = _(u"User's profiles")

    def __unicode__(self):
        return _("Profile for %s") % self.user

    def has_group(self, g):
        return self.user.groups.filter(pk=g.pk).exists()

    def is_path_manager(self):
        g = Group.objects.get(name=GROUP_PATH_MANAGER)
        return self.has_group(g)

    def is_comm_manager(self):
        g = Group.objects.get(name=GROUP_COMM_MANAGER)
        return self.has_group(g)

    def is_editor(self):
        g = Group.objects.get(name=GROUP_EDITOR)
        return self.has_group(g)

    def is_administrator(self):
        g = Group.objects.get(name=GROUP_ADMINISTRATOR)
        return self.has_group(g)

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.10767804727328904, 'fraction_numerical': 0.0024718059632318863, 'mean_word_length': 3.3537323470073974, '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': '2327436', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1860, 'n_words': 445}