diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b374108..d6d17e3 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,11 @@ + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -82,7 +143,7 @@ true DEFINITION_ORDER - + - - - - @@ -182,6 +234,10 @@ + + + + - + + + - - + - + - - - - - - - - - - - - - - @@ -299,13 +342,6 @@ - - - - - - - @@ -584,14 +620,6 @@ - - - - - - - - @@ -608,10 +636,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + @@ -629,94 +697,70 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/apps/__pycache__/custom.cpython-36.pyc b/apps/__pycache__/custom.cpython-36.pyc index 1a6857e..197c1ca 100644 Binary files a/apps/__pycache__/custom.cpython-36.pyc and b/apps/__pycache__/custom.cpython-36.pyc differ diff --git a/apps/custom.py b/apps/custom.py index fc91465..b7d92db 100644 --- a/apps/custom.py +++ b/apps/custom.py @@ -12,6 +12,7 @@ from system.mixin import LoginRequiredMixin class SandboxGetObjectMixin: + def get_object(self, queryset=None): if queryset is None: @@ -50,7 +51,7 @@ class SandboxCreateView(LoginRequiredMixin, SandboxEditViewMixin, CreateView): class SandboxUpdateView(LoginRequiredMixin, SandboxEditViewMixin, SandboxGetObjectMixin, UpdateView): - + """View for updating an object, with a response rendered by a template.""" def post(self, request, *args, **kwargs): self.object = self.get_object() - return super().post(request, *args, **kwargs) \ No newline at end of file + return super().post(request, *args, **kwargs) diff --git a/apps/system/__pycache__/urls.cpython-36.pyc b/apps/system/__pycache__/urls.cpython-36.pyc index 0eaf445..ba70b30 100644 Binary files a/apps/system/__pycache__/urls.cpython-36.pyc and b/apps/system/__pycache__/urls.cpython-36.pyc differ diff --git a/apps/system/__pycache__/views_role.cpython-36.pyc b/apps/system/__pycache__/views_role.cpython-36.pyc index 98edcd2..8ad075c 100644 Binary files a/apps/system/__pycache__/views_role.cpython-36.pyc and b/apps/system/__pycache__/views_role.cpython-36.pyc differ diff --git a/apps/system/urls.py b/apps/system/urls.py index c9f86b9..9537e25 100644 --- a/apps/system/urls.py +++ b/apps/system/urls.py @@ -31,4 +31,6 @@ urlpatterns = [ path('rbac/role/create/', views_role.RoleCreateView.as_view(), name='rbac-role-create'), path('rbac/role/list/', views_role.RoleListView.as_view(), name='rbac-role-list'), path('rbac/role/update/', views_role.RoleUpdateView.as_view(), name='rbac-role-update'), + path('rbac/role/delete/', views_role.RoleDeleteView.as_view(), name='rbac-role-delete'), + path('rbac/role/role2user$', views_role.Role2UserView.as_view(), name="rbac-role-role2user"), ] diff --git a/apps/system/views_role.py b/apps/system/views_role.py index 0abe1c9..b10f814 100644 --- a/apps/system/views_role.py +++ b/apps/system/views_role.py @@ -34,3 +34,50 @@ class RoleUpdateView(SandboxUpdateView): model = Role fields = '__all__' template_name_suffix = '_update' + + +class RoleDeleteView(LoginRequiredMixin, View): + + def post(self, request): + ret = dict(result=False) + if 'id' in request.POST and request.POST['id']: + id_list = map(int, request.POST['id'].split(',')) + Role.objects.filter(id__in=id_list).delete() + ret['result'] = True + return HttpResponse(json.dumps(ret), content_type='application/json') + + +from django.shortcuts import get_object_or_404 +from django.contrib.auth import get_user_model +from django.shortcuts import render + +User = get_user_model() + +class Role2UserView(LoginRequiredMixin, View): + """ + 角色关联用户 + """ + + def get(self, request): + if 'id' in request.GET and request.GET['id']: + role = get_object_or_404(Role, pk=int(request.GET.get('id'))) + added_users = role.userprofile_set.all() + all_users = User.objects.all() + un_add_users = set(all_users).difference(added_users) + ret = dict(role=role, added_users=added_users, un_add_users=list(un_add_users)) + return render(request, 'system/role_role2user.html', ret) + + def post(self, request): + res = dict(result=False) + id_list = None + role = get_object_or_404(Role, pk=int(request.POST.get('id'))) + if 'to' in request.POST and request.POST['to']: + id_list = map(int, request.POST.getlist('to', [])) + role.userprofile_set.clear() + if id_list: + for user in User.objects.filter(id__in=id_list): + role.userprofile_set.add(user) + res['result'] = True + return HttpResponse(json.dumps(res), content_type='application/json') + + diff --git a/db.sqlite3 b/db.sqlite3 index 2a8b9eb..5d4dbd9 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/templates/system/role.html b/templates/system/role.html index 529387d..accb09d 100644 --- a/templates/system/role.html +++ b/templates/system/role.html @@ -142,8 +142,101 @@ oDataTable.ajax.reload(); } }); + } -} + //checkbox全选 + $("#checkAll").on("click", function () { + if ($(this).prop("checked") === true) { + $("input[name='checkList']").prop("checked", $(this).prop("checked")); + $('#example tbody tr').addClass('selected'); + } else { + $("input[name='checkList']").prop("checked", false); + $('#example tbody tr').removeClass('selected'); + } + }); + + //批量删除 + $("#btnDelete").click(function () { + if ($("input[name='checkList']:checked").length == 0) { + layer.msg("请选择要删除的记录"); + return; + } + + var arrId = new Array(); + $("input[name='checkList']:checked").each(function () { + //alert($(this).val()); + arrId.push($(this).val()); + }); + + sId = arrId.join(','); + + layer.alert('确定删除吗?', { + title: '提示' + , icon: 3 //0:感叹号 1:对号 2:差号 3:问号 4:小锁 5:哭脸 6:笑脸 + , time: 0 //不自动关闭 + , btn: ['YES', 'NO'] + , yes: function (index) { + layer.close(index); + $.ajax({ + type: "POST", + url: "{% url 'system:rbac-role-delete' %}", + data: {"id": sId, csrfmiddlewaretoken: '{{ csrf_token }}'}, + cache: false, + success: function (msg) { + if (msg.result) { + layer.alert("操作成功", {icon: 1}); + oDataTable.ajax.reload(); + } else { + //alert(msg.message); + layer.alert("操作失败", {icon: 2}); + } + return; + } + }); + } + }); + }); + + //删除单个数据 + function doDelete(id) { + layer.alert('确定删除吗?', { + title: '提示' + , icon: 3 //0:感叹号 1:对号 2:差号 3:问号 4:小锁 5:哭脸 6:笑脸 + , time: 0 //不自动关闭 + , btn: ['YES', 'NO'] + , yes: function (index) { + layer.close(index); + $.ajax({ + type: "POST", + url: "{% url 'system:rbac-role-delete' %}", + data: {"id": id, csrfmiddlewaretoken: '{{ csrf_token }}'}, + cache: false, + success: function (msg) { + if (msg.result) { + layer.alert('删除成功', {icon: 1}); + oDataTable.ajax.reload(); + } else { + //alert(msg.message); + layer.alert('删除失败', {icon: 2}); + } + return; + } + }); + } + }); + } + + //关联用户 + function doUpdateUser(id) { + layer.open({ + type: 2, + title: '绑定用户', + shadeClose: false, + maxmin: true, + area: ['800px', '600px'], + content: ["{% url 'system:rbac-role-role2user' %}" + '?id=' + id, 'no'], + }); + } {% endblock %} \ No newline at end of file diff --git a/templates/system/role_role2user.html b/templates/system/role_role2user.html new file mode 100644 index 0000000..332f7e9 --- /dev/null +++ b/templates/system/role_role2user.html @@ -0,0 +1,105 @@ +{% extends 'base-layer.html' %} +{% load staticfiles %} + +{% block css %} + +{% endblock %} +{% block main %} +
+
+ {% csrf_token %} + +
+
+
+ + +
+
+





+ + + + +
+
+ + +
+
+
+
+

*注意:一个用户可以添加到多个角色中,继承多个角色的权限

+
+
+
+ +
+
+{% endblock %} + +{% block javascripts %} + + +{% endblock %}