<commit_before>from django.conf import settings
from django.contrib.auth.models import User

from object_permissions.registration import get_user_perms, user_has_perm

class ObjectPermBackend(object):
    supports_object_permissions = True
    supports_anonymous_user = True

    def __init__(self, *args, **kwargs):
        if hasattr(settings, 'ANONYMOUS_USER_ID'):
            id = settings.ANONYMOUS_USER_ID
            self.anonymous, new = User.objects.get_or_create(id=id, \
                                                        username='anonymous')
            if new:
                self.anonymous.save()
        else:
            self.anonymous = None

    def authenticate(self, username, password):
        """ Empty method, this backend does not authenticate users """
        return None

    def has_perm(self, user_obj, perm, obj=None):
        """
        Return whether the user has the given permission on the given object.
        """

        if not user_obj.is_authenticated():
            if self.anonymous:
                user_obj = self.anonymous
            else:
                return False

        if obj is None:
            return False

        return user_has_perm(user_obj, perm, obj, True)

    def get_all_permissions(self, user_obj, obj=None):
        """
        Get a list of all permissions for the user on the given object.
        """

        if not user_obj.is_authenticated():
            if self.anonymous:
                user_obj = self.anonymous
            else:
                return []

        if obj is None:
            return []

        return get_user_perms(user_obj, obj)<commit_msg>backend: Rewrite with querysets for accuracy.<commit_after>from django.conf import settings
from django.db import models
from django.contrib.auth.models import User

from object_permissions.registration import permission_map, user_has_perm

class ObjectPermBackend(object):
    supports_object_permissions = True
    supports_anonymous_user = True

    def __init__(self, *args, **kwargs):
        if hasattr(settings, 'ANONYMOUS_USER_ID'):
            id = settings.ANONYMOUS_USER_ID
            self.anonymous, new = User.objects.get_or_create(id=id, \
                                                        username='anonymous')
            if new:
                self.anonymous.save()
        else:
            self.anonymous = None

    def authenticate(self, username, password):
        """ Empty method, this backend does not authenticate users """
        return None

    def has_perm(self, user_obj, perm, obj=None):
        """
        Return whether the user has the given permission on the given object.
        """

        if not user_obj.is_authenticated():
            if self.anonymous:
                user_obj = self.anonymous
            else:
                return False

        if obj is None:
            return False

        return user_has_perm(user_obj, perm, obj, True)

    def get_all_permissions(self, user_obj, obj=None):
        """
        Get a list of all permissions for the user on the given object.

        This includes permissions given through groups.
        """

        if not user_obj.is_authenticated():
            if self.anonymous:
                user_obj = self.anonymous
            else:
                return []

        if obj is None:
            return []

        model = obj.__class__
        permissions = permission_map[model]

        q = permissions.objects.filter(
            models.Q(group__user=user_obj) | models.Q(user=user_obj),
            obj=obj)

        # Extract all set permissions from each row in the query.
        rv = set()
        for row in q:
            rv.update(field.name for field in row._meta.fields
                if isinstance(field, models.BooleanField)
                and getattr(row, field.name))

        return list(rv)

    def get_group_permissions(self, user_obj, obj=None):
        """
        Get a list of permissions for this user's groups on the given object.
        """

        if not user_obj.is_authenticated():
            if self.anonymous:
                user_obj = self.anonymous
            else:
                return []

        if obj is None:
            return []

        model = obj.__class__
        permissions = permission_map[model]

        q = permissions.objects.filter(groups__user=user_obj, obj=obj)

        # Extract all set permissions from each row in the query.
        rv = set()
        for row in q:
            rv.update(field.name for field in row._meta.fields
                if isinstance(field, models.BooleanField)
                and getattr(row, field.name))

        return list(rv)
{'fraction_non_alphanumeric': 0.07479224376731301, 'fraction_numerical': 0.0, 'mean_word_length': 1.8107784431137726, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5911729', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1205, 'n_words': 364}