1.如何生成二级菜单栏
在 rbca表中生成一张表 menu , 表示可能需要给客户展示的一级菜单栏
在permission表中 ,创建字段,通过多对一外键关系,关联到menu表,表示哪些permission要在二级菜单栏显示,显示到哪个一级菜单栏下面
在initial_session 权限注入的时候,注入一个字典 permission_menu_dict > 格式如下:
'''目标数据结构 { 1:{ # 1--menu_id 'title':'..', 'icon':'xx', children:[ {'title':'二级','url':'xx'}, {'title':'二级','url':'xx'}, ] }, 2:{ # 2--menu_id 'title':'..', 'icon':'xx', children:[ {'title':'二级','url':'xx'}, {'title':'二级','url':'xx'}, ] } } '''
在自定义标签中循环该字典生成标签
from django import template from django.urls import reverse register = template.Library() from django.conf import settings import re @register.inclusion_tag('rbac/menu.html') def menu(request): # menu_list = request.session.get('permisson_menu_list') menu_dict= request.session.get('permisson_menu_dict') for key,item in menu_dict.items(): item['class'] = 'hide' for child in item['children']: # /customer/add/ # 'url': '/customer/list/' # 'url': '/payment/list/' # # if re.match('^{}$'.format(child['url']), request.path): if request.show_id == child['pk']: # null == child['pk'] 1 item['class'] = '' child['class'] = 'active' break return {"menu_dict": menu_dict} @register.filter def haspermission(base_url,request): for item in request.session['permisson_list']: #item = {'url','','title':'','name':''} reg = '^%s$' % item['url'] #/customer/edit/(d+)/ --- "/customer/edit/ # /" ret = re.search(reg, base_url) # if item['name'] == base_url: if ret: return True return False
from django.db import models class Menu(models.Model): title = models.CharField(max_length=32, verbose_name='一级菜单', null=True, blank=True) icon = models.CharField(max_length=32, verbose_name='一级菜单图标', null=True, blank=True) class Permission(models.Model): """ 权限表 """ title = models.CharField(max_length=32, verbose_name='标题') url = models.CharField(max_length=32, verbose_name='权限') # is_menu = models.BooleanField(default=False, verbose_name='是否是菜单') menu = models.ForeignKey('Menu',null=True) pid = models.ForeignKey('self',null=True) class Meta: verbose_name_plural = '权限表' verbose_name = '权限表' def __str__(self): return self.title class Role(models.Model): name = models.CharField(max_length=32, verbose_name='角色名称') permissions = models.ManyToManyField(to='Permission', verbose_name='角色所拥有的权限', blank=True) def __str__(self): return self.name class User(models.Model): """ 用户表 """ name = models.CharField(max_length=32, verbose_name='用户名') password = models.CharField(max_length=32, verbose_name='密码') roles = models.ManyToManyField(to='Role', verbose_name='用户所拥有的角色', blank=True) def __str__(self): return self.name
from rbac import models def initial_session(request,user_obj): permission_obj = models.Permission.objects.filter(role__user=user_obj).values('title','url','pk','menu_id','menu__title','menu__icon').distinct() # print(permission_obj) permission_list = [] permission_menu_dict = {} for item in permission_obj: permission_list.append(item['url']) if item['menu_id']: if item['menu_id'] not in permission_menu_dict: permission_menu_dict[item['menu_id']] = {'title':item['menu__title'],'icon':item['menu__icon'],'children':[{'title':item['title'],'url':item['url']}]} else: permission_menu_dict[item.menu_id]['children'].append({'title':item['title'],'url':item['url']}) # permission_list = [i.url for i in permission_obj] request.session['permission_list'] = permission_list request.session['permission_menu_dict'] = permission_menu_dict print(permission_menu_dict) ''' 1. 循环生成2级菜单 生成一个字典 permission_menu_dict = { 1:{} } '''
<div class="static-menu"> {# {% for item in menu_list %}#} {# {% if item.active %}#} {# <a href="{{ item.url }}" class="{{ item.class }} active">#} {# <span class="icon-wrap"><i class="fa {{ item.icon }}"></i></span> {{ item.title }}</a>#} {# {% else %}#} {# <a href="{{ item.url }}" class="{{ item.class }} ">#} {# <span class="icon-wrap"><i class="fa {{ item.icon }}"></i></span> {{ item.title }}</a>#} {# {% endif %}#} {##} {##} {# {% endfor %}#} {% for item in menu_list.values %} <div class="item"> <div class="title"> <i class="{{ item.icon }}"></i>{{ item.title }} </div> {% for child in item.children %} <div class="body {{ item.class }}"> <a href="{{ child.url }}" class="{{ child.class }}">{{ child.title }}</a> </div> {% endfor %} </div> {% endfor %} </div>
<script> $('.item .title').click(function () { $(this).next().toggleClass('hide'); $(this).parent().siblings().children(".body").addClass("hide") }); </script>