从数据库获取数据后,对数据进行清洗
目标:
数据1,存放至session 中的数据
数据2,显示至前端的菜单数据
清洗数据:
1.session存放的数据:(menu_leaf_dict)
2.前端菜单数据(rasult)
from app01 import models # 取用户角色 uname juese data = models.Juese.objects.filter(utoj__u__uname='xguo') # 获取当前用户角色的权限 uname_juese quanxian_zsgc # 数据去重 (.values(字段).distinct()) 排除不必要的数据 .exclude(qz__q__menu__isnull=True) data2 = models.JtoQtoZ.objects.filter(j__in=data).exclude(qz__q__menu__isnull=True).values('qz__q__qxname','qz__q__url','qz__q__menu').distinct() # 获得菜单数据 data3 = models.Menu.objects.values("id", 'Mname', 'zgl').all() # for i in data2: # print(i) # {'qz__q__qxname': '行政管理', 'qz__q__url': '/xingzheng', 'qz__q__menu': 4} # {'qz__q__qxname': '用户管理', 'qz__q__url': '/yonghu', 'qz__q__menu': 6} # for i in data3: # print(i) # -------------------------------------------------------------------------------------- # 处理用户角色权限数据 # 格式: # id list(dict) # 4 [{'name': '行政管理', 'url': '/xingzheng', 'gid': 4, 'status': True, 'child': []}] menu_leaf_dict = {} for i in data2: i = { 'Mname': i['qz__q__qxname'], 'url': i['qz__q__url'], 'gid': i['qz__q__menu'], 'status': True, 'child': [] } if i['gid'] in menu_leaf_dict: menu_leaf_dict[i['gid']].append(i) else: menu_leaf_dict[i['gid']] = [i, ] # for i,j in menu_leaf_dict.items(): # print(i,j) # # 4[{'name': '行政管理', 'url': '/xingzheng', 'gid': 4, 'status': True, 'child': []}] # # 6[{'name': '用户管理', 'url': '/yonghu', 'gid': 6, 'status': True, 'child': []}] # -------------------------------------------------------------------------------------- # 处理菜单数据 # 格式: # ID dict 形式 # 1 {'id': 1, 'Mname': '菜单1', 'zgl': None, 'child': [], 'status': False} menu_dict = {} for i in data3: i['child'] = [] menu_dict[i['id']] = i i['status'] = False # for i,j in menu_dict.items(): # print(i,j) # -------------------------------------------------------------------------------------- # 将用户角色权限数据 挂到 菜单数据中,并将被挂的菜单设置为 True for i, j in menu_leaf_dict.items(): menu_dict[i]['child'] = j # menu_dict[i]['status'] = True zgl_id = i # print(zgl_id) while zgl_id: menu_dict[zgl_id]['status'] = True zgl_id = menu_dict[zgl_id]['zgl'] # for i,j in menu_dict.items(): # print(i, j) ''' 1 {'id': 1, 'Mname': '菜单1', 'zgl': None, 'child': [], 'status': False} 2 {'id': 2, 'Mname': '菜单2', 'zgl': None, 'child': [], 'status': False} 3 {'id': 3, 'Mname': '菜单3', 'zgl': None, 'child': [], 'status': False} 4 {'id': 4, 'Mname': '菜单1.1', 'zgl': 1, 'child': [{'name': '行政管理', 'url': '/xingzheng', 'gid': 4, 'status': True, 'child': []}], 'status': True} 5 {'id': 5, 'Mname': '菜单1.2', 'zgl': 1, 'child': [], 'status': False} 6 {'id': 6, 'Mname': '菜单1.1.1', 'zgl': 4, 'child': [{'name': '用户管理', 'url': '/yonghu', 'gid': 6, 'status': True, 'child': []}], 'status': True} ''' # -------------------------------------------------------------------------------------- # 前端的菜单数据: rasult = [] for i in menu_dict.values(): if i['zgl'] == None: # if i['child'] != []: rasult.append(i) else: menu_dict[i['zgl']]['child'].append(i) # for i in rasult: # print(i) # --------------------------------------------------------------------------------------
rasult数据 清洗成前端可以显示的数据:
# # 递归函数(为递归显示菜单做准备) def test(data_list): response = '' tpl = ''' <div class = 'item'> <div class='title'>%s</div> <div class='content'>%s</div> </div> ''' for i in data_list: if not i['status']: continue elif 'url' in i: response += "<a href='%s'>%s</a>" % (i['url'], i['Mname']) else: title = i['Mname'] i2 = test(i['child']) response += tpl % (title, i2) return response # 执行函数获取数据,利用递归函数 response = '' tpl = ''' <div class = 'item'> <div class='title'>%s</div> <div class='content'>%s</div> </div> ''' for i in rasult: if not i['status']: continue title = i['Mname'] co = test(i['child']) response += tpl % (title, co) # 最终菜单数据为 : response 数据
封装成class:
传递值:
user_data 用户数据
privilege_data 用户权限数据
menue_date 菜单数据
返回值:
return (menu_leaf_dict, response)
注释:return(用户权限的url,菜单显示的html)
class Privilege(): def __init__(self, user_data, privilege_data, menue_date): self.data = user_data self.data2 = privilege_data self.data3 = menue_date def data_clear(self): menu_leaf_dict = {} for i in self.data2: i = { 'Mname': i['qz__q__qxname'], 'url': i['qz__q__url'], 'gid': i['qz__q__menu'], 'status': True, 'child': [] } if i['gid'] in menu_leaf_dict: menu_leaf_dict[i['gid']].append(i) else: menu_leaf_dict[i['gid']] = [i, ] menu_dict = {} for i in self.data3: i['child'] = [] menu_dict[i['id']] = i i['status'] = False # 将用户角色权限数据 挂到 菜单数据中,并将被挂的菜单设置为 True for i, j in menu_leaf_dict.items(): menu_dict[i]['child'] = j menu_dict[i]['status'] = True zgl_id = i while zgl_id: menu_dict[zgl_id]['status'] = True zgl_id = menu_dict[zgl_id]['zgl'] rasult = [] for i in menu_dict.values(): if i['zgl'] == None: rasult.append(i) else: menu_dict[i['zgl']]['child'].append(i) # # 递归函数 def test(data_list): response = '' tpl = ''' <div class = 'item'> <div class='title'>%s</div> <div class='content'>%s</div> </div> ''' for i in data_list: if not i['status']: continue elif 'url' in i: response += "<a href='%s'>%s</a>" % (i['url'], i['Mname']) else: title = i['Mname'] i2 = test(i['child']) response += tpl % (title, i2) return response # 执行函数获取数据 response = '' tpl = ''' <div class = 'item'> <div class='title'>%s</div> <div class='content'>%s</div> </div> ''' for i in rasult: if not i['status']: continue title = i['Mname'] co = test(i['child']) response += tpl % (title, co) return (menu_leaf_dict, response)
在views中应用:
from django.shortcuts import render,HttpResponse from app01 import models from static.privilage import Privilege # Create your views here. def index(requset): # 取用户角色 uname juese data = models.Juese.objects.filter(utoj__u__uname='xmei') # 获取当前用户角色的权限 uname_juese quanxian_zsgc # 数据去重 (.values(字段).distinct()) 排除不必要的数据 .exclude(qz__q__menu__isnull=True) data2 = models.JtoQtoZ.objects.filter(j__in=data).exclude(qz__q__menu__isnull=True).values('qz__q__qxname', 'qz__q__url','qz__q__menu').distinct() # 获得菜单数据 data3 = models.Menu.objects.values("id", 'Mname', 'zgl').all() test = Privilege(data,data2,data3) a,b = test.data_clear() return render(requset, 'index.html', {'re': b})