rbac组件之菜单操作(三)
菜单包括菜单列表,菜单列表不仅将菜单列出来,而且将每个菜单下的权限也列出来、菜单的添加、删除、修改。
urls.py
... re_path(r'^menus/list/$', MenuView.as_view(), name='menus_list'), re_path(r'^menus/add/$', MenuAddView.as_view(), name='menus_add'), re_path(r'^menus/edit/(?P<mid>\d+)/$', MenuEditView.as_view(), name='menus_edit'), re_path(r'^menus/dell/(?P<mid>\d+)/$', MenuDelView.as_view(), name='menus_del'), ...
1、后台处理菜单列表页
class MenuView(View): def get(self,request): menu_queryset=Menu.objects.all() pid=request.GET.get('pid') if pid: root_permission_queryset = Permission.objects.filter(menu_id=pid).values('id', 'title', 'url', 'name', 'parent_id','menu__title') else: root_permission_queryset = Permission.objects.filter(menu_id__isnull=False).values('id', 'title', 'url', 'name', 'parent_id','menu__title') # print('permission_queryset',root_permission_queryset) all_root_permission_queryset=Permission.objects.all().values('id', 'title', 'url', 'name', 'parent_id') root_permission_dict={} for row in root_permission_queryset: if not row['parent_id']: root_permission_dict[row['id']]={ 'id':row['id'], 'title':row['title'], 'name':row['name'], 'url':row['url'], 'menu_title':row['menu__title'], 'children':[] } for row in all_root_permission_queryset: parent_id=row['parent_id'] if parent_id in root_permission_dict: root_permission_dict[parent_id]['children'].append( { 'id':row['id'], 'title':row['title'], 'name':row['name'], 'url':row['url'] } ) print(root_permission_dict) return render(request,'rbac/menu_list.html',{'menu_queryset':menu_queryset, 'root_permission_dict':root_permission_dict, 'pid':pid })
将每一个菜单及其权限取出来,组成相应的数据结构
{ 64: { 'id': 64, 'name': 'rbac:multi_permissions', 'menu_title': '权限管理', 'title': '批量添加权限', 'children': [], 'url': '/rbac/multi/permissions/' }, 1: { 'id': 1, 'name': 'stark:crm_userinfo_changelist', 'menu_title': '权限管理', 'title': '用户列表', 'children': [{ 'id': 2, 'name': 'stark:crm_userinfo_add', 'title': '添加用户', 'url': '/stark/crm/userinfo/add/' }, { 'id': 3, 'name': 'stark:crm_userinfo_change', 'title': '修改用户', 'url': '/stark/crm/userinfo/(?P<pk>\\d+)/change/' }, { 'id': 4, 'name': 'stark:crm_userinfo_del', 'title': '删除用户', 'url': '/stark/crm/userinfo/(?P<pk>\\d+)/del/' }, { 'id': 78, 'name': 'stark:crm_userinfo_reset_pwd', 'title': '重置密码', 'url': '/stark/crm/userinfo/reset/password/(?P<pk>\\d+)/' }], 'url': '/stark/crm/userinfo/list/' }, 66: { 'id': 66, 'name': 'login', 'menu_title': '其它', 'title': '登陆', 'children': [], 'url': '/login/' }, 99: { 'id': 99, 'name': 'stark:crm_order_customer_order', 'menu_title': '客户管理', 'title': '个人订单列表', 'children': [{ 'id': 100, 'name': 'stark:crm_paymentrecord_changelist', 'title': '个人账单列表', 'url': '/stark/crm/paymentrecord/list/(?P<order_id>\\d+)/' }, { 'id': 102, 'name': 'stark:crm_paymentrecord_change', 'title': '修改个人账单', 'url': '/stark/crm/paymentrecord/(?P<order_id>\\d+)/(?P<pk>\\d+)/change/' }, { 'id': 103, 'name': 'stark:crm_paymentrecord_del', 'title': '删除个人账单', 'url': '/stark/crm/paymentrecord/(?P<order_id>\\d+)/(?P<pk>\\d+)/del/' }, { 'id': 105, 'name': 'stark:crm_paymentrecord_add', 'title': '添加个人账单', 'url': '/stark/crm/paymentrecord/add/(?P<order_id>\\d+)/' }], 'url': '/stark/crm/order/customer/order/list/' }, 37: { 'id': 37, 'name': 'stark:crm_productparameter_changelist', 'menu_title': '产品管理', 'title': '产品参数列表', 'children': [{ 'id': 38, 'name': 'stark:crm_productparameter_add', 'title': '添加产品参数', 'url': '/stark/crm/productparameter/add/' }, { 'id': 39, 'name': 'stark:crm_productparameter_change', 'title': '修改产品参数', 'url': '/stark/crm/productparameter/(?P<pk>\\d+)/change/' }, { 'id': 40, 'name': 'stark:crm_productparameter_del', 'title': '删除产品参数', 'url': '/stark/crm/productparameter/(?P<pk>\\d+)/del/' }], 'url': '/stark/crm/productparameter/list/' }, 65: { 'id': 65, 'name': 'rbac:distribute_permissions', 'menu_title': '权限管理', 'title': '分配权限', 'children': [], 'url': '/rbac/distribute/permissions/' }, 49: { 'id': 49, 'name': 'stark:crm_productaudit_changelist', 'menu_title': '产品管理', 'title': '产品审核列表', 'children': [], 'url': '/stark/crm/productaudit/list/' }, 72: { 'id': 72, 'name': 'index', 'menu_title': '其它', 'title': '主页', 'children': [], 'url': '/index/' }, 9: { 'id': 9, 'name': 'stark:crm_customer_per_changelist', 'menu_title': '客户管理', 'title': '个人客户列表', 'children': [{ 'id': 10, 'name': 'stark:crm_customer_per_add', 'title': '个人客户添加', 'url': '/stark/crm/customer/per/add/' }, { 'id': 11, 'name': 'stark:crm_customer_per_change', 'title': '个人客户修改', 'url': '/stark/crm/customer/per/(?P<pk>\\d+)/change/' }, { 'id': 12, 'name': 'stark:crm_customer_per_del', 'title': '个人客户删除', 'url': '/stark/crm/customer/per/(?P<pk>\\d+)/del/' }, { 'id': 84, 'name': 'stark:crm_order_add', 'title': '添加客户订单', 'url': '/stark/crm/order/add/(?P<customer_id>\\d+)/' }, { 'id': 85, 'name': 'stark:crm_order_change', 'title': '修改客户订单', 'url': '/stark/crm/order/(?P<customer_id>\\d+)/(?P<pk>\\d+)/change/' }, { 'id': 86, 'name': 'stark:crm_order_del', 'title': '删除客户订单', 'url': '/stark/crm/order/(?P<customer_id>\\d+)/(?P<pk>\\d+)/del/' }, { 'id': 88, 'name': 'stark:crm_consultantrecord_per_add', 'title': '添加客户咨询记录', 'url': '/stark/crm/consultantrecord/per/add/(?P<customer_id>\\d+)/' }, { 'id': 89, 'name': 'stark:crm_consultantrecord_per_change', 'title': '修改客户咨询记录', 'url': '/stark/crm/consultantrecord/per/(?P<customer_id>\\d+)/(?P<pk>\\d+)/change/' }, { 'id': 90, 'name': 'stark:crm_consultantrecord_per_del', 'title': '删除客户咨询记录', 'url': '/stark/crm/consultantrecord/per/(?P<customer_id>\\d+)/(?P<pk>\\d+)/del/' }, { 'id': 91, 'name': 'stark:crm_consultantrecord_per_changelist', 'title': '客户咨询列表', 'url': '/stark/crm/consultantrecord/per/constant/list/(?P<customer_id>\\d+)/' }, { 'id': 92, 'name': 'stark:crm_order_changelist', 'title': '客户订单列表', 'url': '/stark/crm/order/customer/order/(?P<customer_id>\\d+)/' }], 'url': '/stark/crm/customer/per/list/' }, 45: { 'id': 45, 'name': 'stark:crm_workshop_changelist', 'menu_title': '生产流程管理', 'title': '生产车间列表', 'children': [{ 'id': 46, 'name': 'stark:crm_workshop_add', 'title': '添加生产车间', 'url': '/stark/crm/workshop/add/' }, { 'id': 47, 'name': 'stark:crm_workshop_change', 'title': '修改生产车间', 'url': '/stark/crm/workshop/(?P<pk>\\d+)/change/' }, { 'id': 48, 'name': 'stark:crm_workshop_del', 'title': '删除生产车间', 'url': '/stark/crm/workshop/(?P<pk>\\d+)/del/' }], 'url': '/stark/crm/workshop/list/' }, 79: { 'id': 79, 'name': 'stark:crm_paymentrecord_audit_list', 'menu_title': '费用管理', 'title': '账单审核', 'children': [], 'url': '/stark/crm/paymentrecord/audit/' }, 80: { 'id': 80, 'name': 'logout', 'menu_title': '其它', 'title': '登出', 'children': [], 'url': '/logout/' }, 17: { 'id': 17, 'name': 'stark:crm_product_changelist', 'menu_title': '产品管理', 'title': '产品列表', 'children': [{ 'id': 18, 'name': 'stark:crm_product_add', 'title': '产品增加', 'url': '/stark/crm/product/add/' }, { 'id': 19, 'name': 'stark:crm_product_change', 'title': '产品修改', 'url': '/stark/crm/product/(?P<pk>\\d+)/change/' }, { 'id': 20, 'name': 'stark:crm_product_del', 'title': '产品删除', 'url': '/stark/crm/product/(?P<pk>\\d+)/del/' }], 'url': '/stark/crm/product/list/' }, 82: { 'id': 82, 'name': 'stark:crm_order_check_order', 'menu_title': '订单管理', 'title': '订单审核', 'children': [], 'url': '/stark/crm/order/check_list/' }, 53: { 'id': 53, 'name': 'rbac:roles_list', 'menu_title': '权限管理', 'title': '角色列表', 'children': [{ 'id': 54, 'name': 'rbac:roles_add', 'title': '添加角色', 'url': '/rbac/roles/add/' }, { 'id': 55, 'name': 'rbac:roles_edit', 'title': '修改角色', 'url': '/rbac/roles/edit/(?P<rid>\\d+)/' }, { 'id': 56, 'name': 'rbac:roles_del', 'title': '删除角色', 'url': '/rbac/roles/dell/(?P<rid>\\d+)/' }, { 'id': 60, 'name': 'rbac:menus_del', 'title': '删除菜单', 'url': '/rbac/menus/dell/(?P<mid>\\d+)/' }], 'url': '/rbac/roles/list/' }, 41: { 'id': 41, 'name': 'stark:crm_procedure_changelist', 'menu_title': '生产流程管理', 'title': '生产流程列表', 'children': [{ 'id': 42, 'name': 'stark:crm_procedure_add', 'title': '添加生产流程', 'url': '/stark/crm/procedure/add/' }, { 'id': 43, 'name': 'stark:crm_procedure_change', 'title': '修改生产流程', 'url': '/stark/crm/procedure/(?P<pk>\\d+)/change/' }, { 'id': 44, 'name': 'stark:crm_procedure_del', 'title': '删除生产流程', 'url': '/stark/crm/procedure/(?P<pk>\\d+)/del/' }], 'url': '/stark/crm/procedure/list/' }, 57: { 'id': 57, 'name': 'rbac:menus_list', 'menu_title': '权限管理', 'title': '菜单列表', 'children': [{ 'id': 58, 'name': 'rbac:menus_add', 'title': '添加菜单', 'url': '/rbac/menus/add/' }, { 'id': 59, 'name': 'rbac:menus_edit', 'title': '修改菜单', 'url': '/rbac/menus/edit/(?P<mid>\\d+)/' }, { 'id': 61, 'name': 'rbac:permissions_add', 'title': '添加权限', 'url': '/rbac/permissions/add/' }, { 'id': 62, 'name': 'rbac:permissions_edit', 'title': '修改权限', 'url': '/rbac/permissions/edit/(?P<pid>\\d+)/' }, { 'id': 63, 'name': 'rbac:permissions_del', 'title': '删除权限', 'url': '/rbac/permissions/dell/(?P<pid>\\d+)/' }], 'url': '/rbac/menus/list/' }, 5: { 'id': 5, 'name': 'stark:crm_department_changelist', 'menu_title': '权限管理', 'title': '部门列表', 'children': [{ 'id': 6, 'name': 'stark:crm_department_add', 'title': '添加部门', 'url': '/stark/crm/department/add/' }, { 'id': 7, 'name': 'stark:crm_department_change', 'title': '修改部门', 'url': '/stark/crm/department/(?P<pk>\\d+)/change/' }, { 'id': 8, 'name': 'stark:crm_department_del', 'title': '删除部门', 'url': '/stark/crm/department/(?P<pk>\\d+)/del/' }], 'url': '/stark/crm/department/list/' } }
2、处理添加菜单
添加以及修改使用的都是modelform
from django import forms from rbac import models from django.utils.safestring import mark_safe ICON_LIST = [ ['fa fa-address-book', '<i aria-hidden="true" class="fa fa-address-book"></i>'], ['fa fa-address-book-o', '<i aria-hidden="true" class="fa fa-address-book-o"></i>'], ['fa fa-address-card', '<i aria-hidden="true" class="fa fa-address-card"></i>'], ['fa fa-address-card-o', '<i aria-hidden="true" class="fa fa-address-card-o"></i>'], ['fa fa-adjust', '<i aria-hidden="true" class="fa fa-adjust"></i>'], ['fa fa-american-sign-language-interpreting','<i aria-hidden="true" class="fa fa-american-sign-language-interpreting"></i>'], ['fa fa-stack-overflow', '<i class="fa fa-stack-overflow" aria-hidden="true"></i>'], ['fa fa-clipboard', '<i class="fa fa-clipboard"></i>'], ] for item in ICON_LIST: item[1]=mark_safe(item[1]) class MenuModelForm(forms.ModelForm): class Meta: model=models.Menu fields=['title','icon'] widgets = { 'title': forms.TextInput(attrs={'placeholder': '请输入菜单名称', 'class': 'form-control'}), 'icon':forms.RadioSelect(choices=ICON_LIST) }
class MenuAddView(View): def get(self,request): form = MenuModelForm() return render(request,'rbac/menu_add.html',{'form':form}) def post(self,request): form=MenuModelForm(data=request.POST) if form.is_valid(): form.save() return redirect(reverse('rbac:menus_list')) return render(request,'rbac/menu_add.html',{'form':form})
3、处理修改菜单
class MenuEditView(View): def get(self,request,mid): menu_obj=Menu.objects.filter(id=mid).first() if not menu_obj: return HttpResponse('该菜单不存在') form=MenuModelForm(instance=menu_obj) return render(request,'rbac/menu_edit.html',{'form':form}) def post(self,request,mid): menu_obj=Menu.objects.filter(id=mid).first() form=MenuModelForm(data=request.POST,instance=menu_obj) if form.is_valid(): form.save() return redirect(reverse('rbac:menus_list')) return render(request, 'rbac/menu_edit.html', {'form': form})
4、处理删除菜单
class MenuDelView(View): def get(self,request,mid): Menu.objects.filter(id=mid).first().delete() return redirect(reverse('rbac:menus_list'))
作者:iveBoy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。