<commit_before>import uuid
from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin, _user_has_perm
from django.utils.translation import ugettext_lazy as _


class RoleManager(BaseUserManager):
    pass


class Role(PermissionsMixin, AbstractBaseUser):
    PERSON_ROLE = 'P'
    CLIENT_ROLE = 'C'

    ROLE_TYPE = [
        (PERSON_ROLE, _('Personne')),
        (CLIENT_ROLE, _('Client'))
    ]

    objects = RoleManager()

    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    type = models.CharField(_('type de rôle'), max_length=1, choices=ROLE_TYPE, editable=False, blank=False, null=False)

    USERNAME_FIELD = 'id'

    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this admin site.'),
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )

    def __str__(self):
        if self.type == self.CLIENT_ROLE:
            return 'Client role -> %r' % self.client
        elif self.type == self.PERSON_ROLE:
            return 'Person role -> %r' % self.person
        else:
            return 'Unknown role %s' % self.pk

    def has_perm(self, perm, obj=None):
        """
        Override PermissionsMixin has_perm to deactivate is_superuser if
        authenticated through a token
        """

        # Active superusers have all permissions.
        if self.is_active and self.is_superuser and not hasattr(self, 'token'):
            return True

        # Otherwise we need to check the backends.
        return _user_has_perm(self, perm, obj)

    def get_short_name(self):
        if self.type == self.PERSON_ROLE:
            return self.person.get_short_name()
        else:
            return self.client.get_short_name()

    def get_full_name(self):
        if self.type == self.PERSON_ROLE:
            return self.person.get_full_name()
        else:
            return self.client.get_full_name()
<commit_msg>Fix bug with roles when person has been deleted<commit_after>import uuid
from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin, _user_has_perm
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist


class RoleManager(BaseUserManager):
    pass


class Role(PermissionsMixin, AbstractBaseUser):
    PERSON_ROLE = 'P'
    CLIENT_ROLE = 'C'

    ROLE_TYPE = [
        (PERSON_ROLE, _('Personne')),
        (CLIENT_ROLE, _('Client'))
    ]

    objects = RoleManager()

    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    type = models.CharField(_('type de rôle'), max_length=1, choices=ROLE_TYPE, editable=False, blank=False, null=False)

    USERNAME_FIELD = 'id'

    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this admin site.'),
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )

    def __str__(self):
        if self.type == self.CLIENT_ROLE:
            try:
                return 'Client role -> %r' % self.client
            except ObjectDoesNotExist:
                return 'Unknown Client role %s' % self.pk
        elif self.type == self.PERSON_ROLE:
            try:
                return 'Person role -> %r' % self.person
            except ObjectDoesNotExist:
                return 'Unknown Client role %s' % self.pk
        else:
            return 'Unknown role %s' % self.pk

    def has_perm(self, perm, obj=None):
        """
        Override PermissionsMixin has_perm to deactivate is_superuser if
        authenticated through a token
        """

        # Active superusers have all permissions.
        if self.is_active and self.is_superuser and not hasattr(self, 'token'):
            return True

        # Otherwise we need to check the backends.
        return _user_has_perm(self, perm, obj)

    def get_short_name(self):
        if self.type == self.PERSON_ROLE:
            return self.person.get_short_name()
        else:
            return self.client.get_short_name()

    def get_full_name(self):
        if self.type == self.PERSON_ROLE:
            return self.person.get_full_name()
        else:
            return self.client.get_full_name()

{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.0008302200083022001, 'mean_word_length': 2.4396859386152747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1755513', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1400, 'n_words': 374}