def multi_permissions(request): """ 批量操作权限 :param request: :return: """ post_type = request.GET.get('type') # 更新和编辑用的 FormSet = modelformset_factory(models.Permission, MultiPermissionForm, extra=0) # 增加用的 AddFormSet = formset_factory(MultiPermissionForm, extra=0) permissions = models.Permission.objects.all() # 获取路由系统中所有URL router_dict = get_all_url_dict(ignore_namespace_list=['admin']) # 数据库中的所有权限的别名 permissions_name_set = set([i.name for i in permissions]) # 路由系统中的所有权限的别名 router_name_set = set(router_dict.keys()) if request.method == 'POST' and post_type == 'add': add_formset = AddFormSet(request.POST) if add_formset.is_valid(): print(add_formset.cleaned_data) permission_obj_list = [models.Permission(**i) for i in add_formset.cleaned_data] query_list = models.Permission.objects.bulk_create(permission_obj_list) for i in query_list: permissions_name_set.add(i.name) add_name_set = router_name_set - permissions_name_set add_formset = AddFormSet(initial=[row for name, row in router_dict.items() if name in add_name_set]) del_name_set = permissions_name_set - router_name_set del_formset = FormSet(queryset=models.Permission.objects.filter(name__in=del_name_set)) update_name_set = permissions_name_set & router_name_set update_formset = FormSet(queryset=models.Permission.objects.filter(name__in=update_name_set)) if request.method == 'POST' and post_type == 'update': update_formset = FormSet(request.POST) if update_formset.is_valid(): update_formset.save() update_formset = FormSet(queryset=models.Permission.objects.filter(name__in=update_name_set)) return render( request, 'rbac/multi_permissions.html', { 'del_formset': del_formset, 'update_formset': update_formset, 'add_formset': add_formset, } )
def multi_power(request): """ 批量操作权限 :param request: :return: """ post_type = request.GET.get('type') # 删除和编辑使用modelformset FormSet = modelformset_factory(models.Permission, MultiPermissionForm, extra=0) # 添加使用的formset AddFormSet = formset_factory(MultiPermissionForm, extra=0) # 获取数据中所有的权限 permissions = models.Permission.objects.all() # 获取到路由系统所有的url(权限) router_dict = get_all_url_dict(ignore_namespace_list=['admin']) # 数据库中权限别名的集合 permissions_name_set = set([i.url_name for i in permissions]) # 路由系统中权限别名的集合 router_name_set = set(router_dict.keys()) # 新增权限的别名的集合 add_name_set = router_name_set - permissions_name_set add_formset = AddFormSet(initial=[ row for name, row in router_dict.items() if name in add_name_set ]) # print(router_dict) if request.method == 'POST' and post_type == 'add': add_formset = AddFormSet(request.POST) if add_formset.is_valid(): print('pass') permission_obj_list = [ models.Permission(**i) for i in add_formset.cleaned_data ] query_list = models.Permission.objects.bulk_create( permission_obj_list) add_formset = AddFormSet() for i in query_list: permissions_name_set.add(i.url_name) print('no pass') # 删除权限的别名的集合 del_name_set = permissions_name_set - router_name_set del_formset = FormSet(queryset=models.Permission.objects.filter( url_name__in=del_name_set)) # 更新权限的别名的集合 update_name_set = permissions_name_set & router_name_set update_formset = FormSet(queryset=models.Permission.objects.filter( url_name__in=update_name_set)) if request.method == 'POST' and post_type == 'update': update_formset = FormSet(request.POST) if update_formset.is_valid(): update_formset.save() update_formset = FormSet(queryset=models.Permission.objects.filter( url_name__in=update_name_set)) return render( request, 'multi_powers.html', { 'del_formset': del_formset, 'update_formset': update_formset, 'add_formset': add_formset, })