<commit_before>from functools import update_wrapper

import django
from django.contrib import messages
from django.contrib.admin.options import csrf_protect_m
from django.contrib.admin.utils import model_ngettext, unquote
from django.contrib.auth.models import Group
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.forms import formset_factory
from django.http import Http404
from django.template.response import TemplateResponse
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.translation import ugettext as _
from guardian.shortcuts import assign_perm, get_groups_with_perms, get_perms_for_model, remove_perm

from .actions import update_permissions
from .forms import get_group_permission_form


PERM_PREFIX = 'perm_'


class GrootAdminMixin(object):
    actions = [update_permissions]

    def get_urls(self):
        from django.conf.urls import url

        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            wrapper.model_admin = self
            return update_wrapper(wrapper, view)

        info = self.model._meta.app_label, self.model._meta.model_name

        return [
            url(r'^(.+)/groot/$', wrap(self.groot_view), name='%s_%s_groot' % info),
        ] + super(GrootAdminMixin, self).get_urls()

    @csrf_protect_m
    @transaction.atomic
    def groot_view(self, request, object_id):
        # Only allow superusers to edit permissions
        if not request.user.is_superuser:
            raise PermissionDenied

        model = self.model
        opts = model._meta

        obj = self.get_object(request, unquote(object_id))

        if obj is None:
            raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {
                'name': force_text(opts.verbose_name),
                'key': escape(object_id),
            })

        opts = self.model._meta
        app_label = opts.app_label

        group_list = Group.objects.all()
        group_count = group_list.count()

        GroupPermissionForm = get_group_permission_form(model_perms=get_perms_for_model(model))
        GroupPermissionFormSet = formset_factory(
            GroupPermissionForm, extra=0, min_num=group_count, validate_min=True,
            max_num=group_count)

        obj_group_perms = get_groups_with_perms(obj=obj, attach_perms=True)
        initial_data = []

        for group in group_list:
            try:
                group_perms = obj_group_perms[group]
            except KeyError:
                group_perms = []

            group_initial = {
                'group': group,
            }

            for perm in group_perms:
                field_name = '%s%s' % (PERM_PREFIX, perm)
                group_initial[field_name] = True

            initial_data.append(group_initial)

        formset = GroupPermissionFormSet(request.POST or None, initial=initial_data)

        if formset.is_valid():
            # The user has confirmed the update
            group_count = 0

            for form in formset.forms:
                # Only act on changed data
                if form.has_changed():
                    group_count += 1

                    for field in form.changed_data:
                        group = form.cleaned_data['group']
                        changed_perm = field.replace(PERM_PREFIX, '', 1)
                        add_perm = form.cleaned_data[field]

                        # Change perm action accordingly
                        if add_perm:
                            update_perm = assign_perm
                        else:
                            update_perm = remove_perm

                        update_perm(perm=changed_perm, user_or_group=group, obj=obj)

            if group_count:
                self.message_user(request, _((
                    'Successfully updated permissions for %(count)d %(groups)s.'
                )) % {
                    'count': group_count,
                    'groups': model_ngettext(Group, n=group_count),
                }, messages.SUCCESS)
            else:
                self.message_user(request, _('No permissions were updated.'), messages.INFO)

        if django.VERSION >= (1, 8):
            context = self.admin_site.each_context(request)
        else:
            context = self.admin_site.each_context()

        context.update({
            'title': _('Group permissions: %s') % force_text(obj),
            'object': obj,
            'opts': opts,
            'formset': formset,
            'group_formsets': zip(group_list, formset.forms),
        })

        request.current_app = self.admin_site.name<commit_msg>Redirect back on successful form submission<commit_after>from functools import update_wrapper

import django
from django.contrib import messages
from django.contrib.admin.options import csrf_protect_m
from django.contrib.admin.utils import model_ngettext, unquote
from django.contrib.auth.models import Group
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.forms import formset_factory
from django.http import Http404, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.translation import ugettext as _
from guardian.shortcuts import assign_perm, get_groups_with_perms, get_perms_for_model, remove_perm

from .actions import update_permissions
from .forms import get_group_permission_form


PERM_PREFIX = 'perm_'


class GrootAdminMixin(object):
    actions = [update_permissions]

    def get_urls(self):
        from django.conf.urls import url

        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            wrapper.model_admin = self
            return update_wrapper(wrapper, view)

        info = self.model._meta.app_label, self.model._meta.model_name

        return [
            url(r'^(.+)/groot/$', wrap(self.groot_view), name='%s_%s_groot' % info),
        ] + super(GrootAdminMixin, self).get_urls()

    @csrf_protect_m
    @transaction.atomic
    def groot_view(self, request, object_id):
        # Only allow superusers to edit permissions
        if not request.user.is_superuser:
            raise PermissionDenied

        model = self.model
        opts = model._meta

        obj = self.get_object(request, unquote(object_id))

        if obj is None:
            raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {
                'name': force_text(opts.verbose_name),
                'key': escape(object_id),
            })

        opts = self.model._meta
        app_label = opts.app_label

        group_list = Group.objects.all()
        group_count = group_list.count()

        GroupPermissionForm = get_group_permission_form(model_perms=get_perms_for_model(model))
        GroupPermissionFormSet = formset_factory(
            GroupPermissionForm, extra=0, min_num=group_count, validate_min=True,
            max_num=group_count)

        obj_group_perms = get_groups_with_perms(obj=obj, attach_perms=True)
        initial_data = []

        for group in group_list:
            try:
                group_perms = obj_group_perms[group]
            except KeyError:
                group_perms = []

            group_initial = {
                'group': group,
            }

            for perm in group_perms:
                field_name = '%s%s' % (PERM_PREFIX, perm)
                group_initial[field_name] = True

            initial_data.append(group_initial)

        formset = GroupPermissionFormSet(request.POST or None, initial=initial_data)

        if formset.is_valid():
            # The user has confirmed the update
            group_count = 0

            for form in formset.forms:
                # Only act on changed data
                if form.has_changed():
                    group_count += 1

                    for field in form.changed_data:
                        group = form.cleaned_data['group']
                        changed_perm = field.replace(PERM_PREFIX, '', 1)
                        add_perm = form.cleaned_data[field]

                        # Change perm action accordingly
                        if add_perm:
                            update_perm = assign_perm
                        else:
                            update_perm = remove_perm

                        update_perm(perm=changed_perm, user_or_group=group, obj=obj)

            if group_count:
                self.message_user(request, _((
                    'Successfully updated permissions for %(count)d %(groups)s.'
                )) % {
                    'count': group_count,
                    'groups': model_ngettext(Group, n=group_count),
                }, messages.SUCCESS)
            else:
                self.message_user(request, _('No permissions were updated.'), messages.INFO)

            return HttpResponseRedirect(request.path)

        if django.VERSION >= (1, 8):
            context = self.admin_site.each_context(request)
        else:
            context = self.admin_site.each_context()

        context.update({
            'title': _('Group permissions: %s') % force_text(obj),
            'object': obj,
            'opts': opts,
            'formset': formset,
            'group_formsets': zip(group_list, formset.forms),
        })

        request.current_app = self.admin_site.name
{'fraction_non_alphanumeric': 0.08306510227390718, 'fraction_numerical': 0.002491953068217215, 'mean_word_length': 2.0432859399684045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1249229', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2546, 'n_words': 595}