<commit_before>from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from dry_rest_permissions.generics import allow_staff_or_superuser

from sigma_core.models.group import Group

class UserManager(BaseUserManager):
    # TODO: Determine whether 'memberships' fields needs to be retrieved every time or not...
    # def get_queryset(self):
    #     return super(UserManager, self).get_queryset().prefetch_related('memberships', 'invited_to_groups')

    def create_user(self, email, lastname, firstname, password=None):
        """
        Creates and saves a User with the given email, lastname, firstname and password
        TODO - Generate a unique username.
        """
        if not email:
            raise ValueError('Users must have an email address')

        # username = email # TODO - Generate unique username

        user = self.model(
            email=self.normalize_email(email),
            lastname=lastname,
            firstname=firstname
        )

        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, lastname, firstname, password):
        """
        Creates and saves a superuser with the given email, lastname, firstname and password.
        """
        user = self.create_user(email, lastname, firstname, password)
        user.is_superuser = True
        user.is_staff = True
        user.save()
        return user


class User(AbstractBaseUser):
    """
    User are identified by their email. Lastname and firstname are required.
    """
    email = models.EmailField(max_length=254, unique=True)
    lastname = models.CharField(max_length=255)
    firstname = models.CharField(max_length=128)
    # username = models.CharField(max_length=128, unique=True) # TODO - Add unique username for frontend URLs
    phone = models.CharField(max_length=20, blank=True)

    is_active = models.BooleanField(default=True)
    last_modified = models.DateTimeField(auto_now=True)
    join_date = models.DateTimeField(auto_now_add=True)

    is_superuser = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    objects = UserManager()

    invited_to_groups = models.ManyToManyField(Group, blank=True, related_name="invited_users");

    # Related fields:
    #   - memberships (model UserGroup)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['lastname', 'firstname']

    def __str__(self):
        return self.email

    def get_full_name(self):
        return "{} {}".format(self.lastname, self.firstname)

    def get_short_name(self):
        return self.email

    def is_sigma_admin(self):
        return self.is_staff or self.is_superuser

    def is_group_member(self, g):
        try:
            mem = self.memberships.get(group=g)
        except GroupMember.DoesNotExist:
            return False
        return mem.is_accepted()

    def can_invite(self, group):
        try:
            mem = self.memberships.get(group=group)
        except GroupMember.DoesNotExist:
            return False
        return mem.perm_rank >= group.req_rank_invite

    def can_accept_join_requests(self, group):
        try:
            mem = self.memberships.get(group=group)
        except GroupMember.DoesNotExist:
            return False
        return mem.perm_rank >= group.req_rank_accept_join_requests

    def can_modify_group_infos(self, group):
        try:
            mem = self.memberships.get(group=group)
        except GroupMember.DoesNotExist:
            return False
        return mem.perm_rank >= group.req_rank_modify_group_infos


    # Perms for admin site
    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    # Permissions
    @staticmethod
    def has_read_permission(request):
        """
        Only authenticated users can retrieve an users list.
        """
        return True

    def has_object_read_permission(self, request):
        """
        Only authenticated users can retrieve an user.
        """
        return True

    @staticmethod
    def has_write_permission(request):<commit_msg>Bugfix : GroupMember import was missing<commit_after>from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from dry_rest_permissions.generics import allow_staff_or_superuser

from sigma_core.models.group import Group

class UserManager(BaseUserManager):
    # TODO: Determine whether 'memberships' fields needs to be retrieved every time or not...
    # def get_queryset(self):
    #     return super(UserManager, self).get_queryset().prefetch_related('memberships', 'invited_to_groups')

    def create_user(self, email, lastname, firstname, password=None):
        """
        Creates and saves a User with the given email, lastname, firstname and password
        TODO - Generate a unique username.
        """
        if not email:
            raise ValueError('Users must have an email address')

        # username = email # TODO - Generate unique username

        user = self.model(
            email=self.normalize_email(email),
            lastname=lastname,
            firstname=firstname
        )

        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, lastname, firstname, password):
        """
        Creates and saves a superuser with the given email, lastname, firstname and password.
        """
        user = self.create_user(email, lastname, firstname, password)
        user.is_superuser = True
        user.is_staff = True
        user.save()
        return user


class User(AbstractBaseUser):
    """
    User are identified by their email. Lastname and firstname are required.
    """
    email = models.EmailField(max_length=254, unique=True)
    lastname = models.CharField(max_length=255)
    firstname = models.CharField(max_length=128)
    # username = models.CharField(max_length=128, unique=True) # TODO - Add unique username for frontend URLs
    phone = models.CharField(max_length=20, blank=True)

    is_active = models.BooleanField(default=True)
    last_modified = models.DateTimeField(auto_now=True)
    join_date = models.DateTimeField(auto_now_add=True)

    is_superuser = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    objects = UserManager()

    invited_to_groups = models.ManyToManyField(Group, blank=True, related_name="invited_users");

    # Related fields:
    #   - memberships (model UserGroup)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['lastname', 'firstname']

    def __str__(self):
        return self.email

    def get_full_name(self):
        return "{} {}".format(self.lastname, self.firstname)

    def get_short_name(self):
        return self.email

    def is_sigma_admin(self):
        return self.is_staff or self.is_superuser

    def is_group_member(self, g):
        from sigma_core.models.group_member import GroupMember
        try:
            mem = self.memberships.get(group=g)
        except GroupMember.DoesNotExist:
            return False
        return mem.is_accepted()

    def can_invite(self, group):
        from sigma_core.models.group_member import GroupMember
        try:
            mem = self.memberships.get(group=group)
        except GroupMember.DoesNotExist:
            return False
        return mem.perm_rank >= group.req_rank_invite

    def can_accept_join_requests(self, group):
        from sigma_core.models.group_member import GroupMember
        try:
            mem = self.memberships.get(group=group)
        except GroupMember.DoesNotExist:
            return False
        return mem.perm_rank >= group.req_rank_accept_join_requests

    def can_modify_group_infos(self, group):
        from sigma_core.models.group_member import GroupMember
        try:
            mem = self.memberships.get(group=group)
        except GroupMember.DoesNotExist:
            return False
        return mem.perm_rank >= group.req_rank_modify_group_infos


    # Perms for admin site
    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    # Permissions
    @staticmethod
    def has_read_permission(request):
        """
        Only authenticated users can retrieve an users list.
        """
        return True

    def has_object_read_permission(self, request):
        """
        Only authenticated users can retrieve an user.
        """
        return True

    @staticmethod
    def has_write_permission(request):
{'fraction_non_alphanumeric': 0.08188556868195507, 'fraction_numerical': 0.0032429928190873293, 'mean_word_length': 2.7740384615384617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1174537', 'n_tokens_mistral': 2503, 'n_tokens_neox': 2409, 'n_words': 601}