<commit_before>from django.views.generic import ListView,View,TemplateView
from django.contrib.auth.models import Group, User, Permission, ContentType
from django.http import JsonResponse
from django.db import IntegrityError
from django.http import QueryDict
from django.shortcuts import redirect
import traceback

from django.contrib.auth.mixins import LoginRequiredMixin
from permissions.mixins import MyPermissionRequiredMixin
from permissions.mixins import my_permission_required
from django.utils.decorators import method_decorator

class GroupListView(LoginRequiredMixin, MyPermissionRequiredMixin, ListView):
    template_name = "group/grouplist.html"
    model = Group
    #指定权限
    permission_required = "auth.view_group"

class ModifyGroupView(View):

    #添加组
    def post(self, request):
        response = {}
        if request.user.has_perm('auth.add_group'):
            group_name = request.POST.get('name', None)
            if not group_name:
                response['status'] = 1
                response['errmsg'] = '不能为空'
                return JsonResponse(response)
            try:
                group = Group(name=group_name)
                group.save()
                response['status'] = 0
                response['group_id'] = group.id
                response['group_name'] = group.name
            except IntegrityError:
                response['status'] =1
                response['errmsg'] = '用户组以存在'
            except:
                response['status'] = 1
                response['errmsg'] = '添加用户组失败'
            return JsonResponse(response)
        else:
            response['status'] = 1
            response['errmsg'] = '没有添加用户组权限'
            return JsonResponse(response)

    def delete(self, request):
        response = {'status': 0}
        if request.user.has_perm('auth.delete_group'):
            data = QueryDict(request.body)
            gid = data.get('gid', None)
            if not gid:
                response['status'] = 1
                response['errmsg'] = 'gid不能为空'
                return JsonResponse(response)
            group_obj = Group.objects.get(id=gid)
            if group_obj.user_set.all().count() != 0:
                response['status'] = 1
                response['errmsg'] = '不能删除，组内还有成员'
                return JsonResponse(response)
            try:
                group_obj.delete()
                response['status'] == 0
                return JsonResponse(response)
            except:
                print(traceback.format_exc())
                response['status'] == 1
                response['errmsg'] == '删除组出错'
                return JsonResponse(response)
        else:
            response['status'] = 1
            response['errmsg'] = '没有删除用户组权限'<commit_msg>修改删除用户组，组内有权限不能删<commit_after>from django.views.generic import ListView,View,TemplateView
from django.contrib.auth.models import Group, User, Permission, ContentType
from django.http import JsonResponse
from django.db import IntegrityError
from django.http import QueryDict
from django.shortcuts import redirect
import traceback

from django.contrib.auth.mixins import LoginRequiredMixin
from permissions.mixins import MyPermissionRequiredMixin
from permissions.mixins import my_permission_required
from django.utils.decorators import method_decorator

class GroupListView(LoginRequiredMixin, MyPermissionRequiredMixin, ListView):
    template_name = "group/grouplist.html"
    model = Group
    #指定权限
    permission_required = "auth.view_group"

class ModifyGroupView(View):

    #添加组
    def post(self, request):
        response = {}
        if request.user.has_perm('auth.add_group'):
            group_name = request.POST.get('name', None)
            if not group_name:
                response['status'] = 1
                response['errmsg'] = '不能为空'
                return JsonResponse(response)
            try:
                group = Group(name=group_name)
                group.save()
                response['status'] = 0
                response['group_id'] = group.id
                response['group_name'] = group.name
            except IntegrityError:
                response['status'] =1
                response['errmsg'] = '用户组以存在'
            except:
                response['status'] = 1
                response['errmsg'] = '添加用户组失败'
            return JsonResponse(response)
        else:
            response['status'] = 1
            response['errmsg'] = '没有添加用户组权限'
            return JsonResponse(response)

    def delete(self, request):
        response = {'status': 0}
        if request.user.has_perm('auth.delete_group'):
            data = QueryDict(request.body)
            gid = data.get('gid', None)
            if not gid:
                response['status'] = 1
                response['errmsg'] = 'gid不能为空'
                return JsonResponse(response)
            group_obj = Group.objects.get(id=gid)
            if group_obj.user_set.all().count() > 0:
                response['status'] = 1
                response['errmsg'] = '不能删除，组内还有成员'
                return JsonResponse(response)
            if group_obj.permissions.all().count() > 0:
                response['status'] = 1
                response['errmsg'] = '不能删除，组内有赋权'
                return JsonResponse(response)
            try:
                group_obj.delete()
                response['status'] == 0
                return JsonResponse(response)
            except:
                print(traceback.format_exc())
                response['status'] == 1
                response['errmsg'] == '删除组出错'
                return JsonResponse(response)
        else:
            response['status'] = 1
            response['errmsg'] = '没有删除用户组权限'
{'fraction_non_alphanumeric': 0.09924864581513193, 'fraction_numerical': 0.004543071815481391, 'mean_word_length': 1.97196261682243, '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': '6238035', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1551, 'n_words': 326}