<commit_before>from django.http import HttpResponseRedirect
from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import user_passes_test
from django.db.models import Q, Max
from functools import wraps

from NearBeach.models import *


def check_user_permissions(min_permission_level, object_lookup=''):
    def decorator(func):
        @wraps(func)
        def inner(request, *args, **kwargs):
            # If user is admin - grant them all permissions
            if request.user.is_superuser:
                # Return the function with a user_level of 4
                return func(request, *args, **kwargs, user_level=4)

            # Default user level is 0
            user_group_results = user_group.objects.filter(
                is_deleted=False,
                username=request.user,
            )

            # If we are passing the project_id through, we will use a different function
            if len(kwargs) > 0:
                # Check user permissions AGAINST the project and it's groups
                # project_instance = project.objects.get(project_id=kwargs['project_id'])

                # Determine if there are any cross over with user groups and project groups
                group_results = group.objects.filter(
                    Q(
                        is_deleted=False,
                        # The project groups
                        group_id__in=object_assignment.objects.filter(
                            is_deleted=False,
                            **{object_lookup: kwargs[object_lookup]},
                        ).values('group_id'),
                    ) &
                    Q(
                        group_id__in=user_group_results.values('group_id')
                    )
                )

                # Check to make sure the user groups intersect
                if len(group_results) == 0:
                    # There are no matching groups - i.e. the user does not have any permission
                    raise PermissionDenied

            # Get the max permission value from user_group_results
            user_level = user_group_results.aggregate(
                Max('permission_set__project')
            )['permission_set__project__max']

            if user_level >= min_permission_level:
                # Everything is fine - continue on
                return func(request, *args, **kwargs, user_level=user_level)

            # Does not meet conditions
            raise PermissionDenied
        return inner
    return decorator<commit_msg>Fix to user permissions only focusing on projects<commit_after>from django.http import HttpResponseRedirect
from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import user_passes_test
from django.db.models import Q, Max
from functools import wraps

from NearBeach.models import *


def check_user_permissions(min_permission_level, object_lookup=''):
    def decorator(func):
        @wraps(func)
        def inner(request, *args, **kwargs):
            # If user is admin - grant them all permissions
            if request.user.is_superuser:
                # Return the function with a user_level of 4
                return func(request, *args, **kwargs, user_level=4)

            # Default user level is 0
            user_group_results = user_group.objects.filter(
                is_deleted=False,
                username=request.user,
            )

            # If we are passing the object_lookup through, we will use a different function
            if len(kwargs) > 0:
                # Determine if there are any cross over with user groups and project groups
                group_results = group.objects.filter(
                    Q(
                        is_deleted=False,
                        # The project groups
                        group_id__in=object_assignment.objects.filter(
                            is_deleted=False,
                            **{object_lookup: kwargs[object_lookup]},
                        ).values('group_id'),
                    ) &
                    Q(
                        group_id__in=user_group_results.values('group_id')
                    )
                )

                # Check to make sure the user groups intersect
                if len(group_results) == 0:
                    # There are no matching groups - i.e. the user does not have any permission
                    raise PermissionDenied

            # Get the max permission value from user_group_results
            user_level = user_group_results.aggregate(
                Max('permission_set__%s' % object_lookup.replace('_id',''))
            )['permission_set__%s__max' % object_lookup.replace('_id','')]


            if user_level >= min_permission_level:
                # Everything is fine - continue on
                return func(request, *args, **kwargs, user_level=user_level)

            # Does not meet conditions
            raise PermissionDenied
        return inner
    return decorator
{'fraction_non_alphanumeric': 0.06440745144669045, 'fraction_numerical': 0.0019817677368212444, 'mean_word_length': 1.693169690501601, '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': '3305020', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1174, 'n_words': 357}