一、权限分配
最终生成页面:
视图函数views.py
主要内容:从数据库中查到相应数据,构建层级结构。
- 一级菜单
- 二级菜单
- 归属二级菜单的子权限
- 二级菜单
def distribute_permissions(request): """ 分配权限 :param request: :return: """ # 用户id uid = request.GET.get('uid') # 角色id rid = request.GET.get('rid') if request.method == 'POST' and request.POST.get('postType') == 'role': # 角色分配 user = User.objects.filter(id=uid).first() if not user: return HttpResponse('用户不存在') # 为用户分配角色 user.roles.set(request.POST.getlist('roles')) if request.method == 'POST' and request.POST.get('postType') == 'permission' and rid: # 为角色分配权限,点中角色才能分配权限 role = Role.objects.filter(id=rid).first() if not role: return HttpResponse('角色不存在') # 为角色分配权限 role.permissions.set(request.POST.getlist('permissions')) # 获取所有的用户 user_list = User.objects.all() # 当前选中用户所拥有的角色 user_has_roles = User.objects.filter(id=uid).values('id', 'roles') user_has_roles_dict = {item['roles']: None for item in user_has_roles} """ user_has_roles_dict = { 角色的id:None } """ # 所有的角色 role_list = Role.objects.all() if rid: # 查出当前角色所拥有的权限 role_has_permissions = Role.objects.filter(id=rid).values('id', 'permissions') elif uid and not rid: user = User.objects.filter(id=uid).first() if not user: return HttpResponse('用户不存在') # 查出前用户所拥有的角色所对应的权限 role_has_permissions = user.roles.values('id', 'permissions') else: role_has_permissions = [] role_has_permissions_dict = {item['permissions']: None for item in role_has_permissions} """" role_has_permissions_dict = { 权限的id :None } """ all_menu_list = [] """ all_menu_list = [ { 'id', 'title', ‘children' : [ { 'id', 'title', 'menu_id' , 'children': [ {'id', 'title', 'parent_id'} ] } ] }, {'id': None, 'title': '其他', 'children': [ {'id', 'title', 'parent_id'} ]} ] """ queryset = Menu.objects.values('id', 'title') menu_dict = {} """ 需要构成的分级数据结构 menu_dict = { 一级菜单的id : { 'id', 'title', ‘children' : [ { '二级菜单id', 'title', 'menu_id' , 'children': [ {'id', 'title', 'parent_id'} ] } ] }, # 其他 None : {'id': None, 'title': '其他', 'children': [ {'id', 'title', 'parent_id'} ]} } """ for item in queryset: item['children'] = [] # 放二级菜单 父权限 menu_dict[item['id']] = item all_menu_list.append(item) other = { 'id': None, 'title': '其他', 'children': []} all_menu_list.append(other) menu_dict[None] = other # 二级菜单 父权限 root_permission = Permission.objects.filter(menu__isnull=False).values('id', 'title', 'menu_id') # 构建二级菜单权限数据结构 root_permission_dict = {} """ root_permission_dict = { 父权限的id: { 'id', 'title', 'menu_id' , 'children': [ {'id', 'title', 'parent_id'} ] } } """ for per in root_permission: per['children'] = [] # 放子权限 nid = per['id'] menu_id = per['menu_id'] root_permission_dict[nid] = per menu_dict[menu_id]['children'].append(per) # 除了父权限的其他的权限 node_permission = Permission.objects.filter(menu__isnull=True).values('id', 'title', 'parent_id') # 构建其他权限的数据结构 for per in node_permission: pid = per['parent_id'] if not pid: menu_dict[None]['children'].append(per) continue root_permission_dict[pid]['children'].append(per) return render( request, 'rbac/distribute_permissions.html', { 'user_list': user_list, 'role_list': role_list, 'user_has_roles_dict': user_has_roles_dict, 'role_has_permissions_dict': role_has_permissions_dict, 'all_menu_list': all_menu_list, 'uid': uid, 'rid': rid } )
模板rbac/distribute_permissions.html
主要有三块结构,注意数据的转义。数字key变为了字符串类型。
{% extends 'layout.html' %}{% load rbac %}{% block css %} {% endblock %}{% block content %}{% endblock %}{% block js %} {% endblock %}用户信息{% for user in user_list %}
- { { user.name }}
{% endfor %}
templatetages.rbac.py中相关的simple_tag
# 获取当前角色id,然后给url添加一个rid参数@register.simple_tagdef gen_role_url(request, rid): params = request.GET.copy() params['rid'] = rid return params.urlencode()