默认展开非菜单URL

表结构

from django.db import models


class Menu(models.Model):
    """
    菜单
    """
    title = models.CharField(verbose_name=‘菜单‘, max_length=32)
    icon = models.CharField(verbose_name=‘图标‘, max_length=32)

    def __str__(self):
        return self.title


class Permission(models.Model):
    """
    权限表
    """
    title = models.CharField(verbose_name=‘标题‘, max_length=32)
    url = models.CharField(verbose_name=‘含正则的URL‘, max_length=128)
      
    # 自关联
    pid = models.ForeignKey(verbose_name=‘默认选中权限‘, to=‘Permission‘, related_name=‘ps‘, null=True, blank=True,
                            help_text="对于无法作为菜单的URL,可以为其选择一个可以作为菜单的权限,那么访问时,则默认选中此权限",
                            limit_choices_to={‘menu__isnull‘: False})
    
    menu = models.ForeignKey(verbose_name=‘菜单‘, to=‘Menu‘, null=True, blank=True, help_text=‘null表示非菜单‘)

    def __str__(self):
        return self.title


class Role(models.Model):
    """
    角色
    """
    title = models.CharField(verbose_name=‘角色名称‘, max_length=32)
    permissions = models.ManyToManyField(verbose_name=‘拥有的所有权限‘, to=‘Permission‘, blank=True)

    def __str__(self):
        return self.title


class UserInfo(models.Model):
    """
    用户表
    """
    name = models.CharField(verbose_name=‘用户名‘, max_length=32)
    password = models.CharField(verbose_name=‘密码‘, max_length=64)
    email = models.CharField(verbose_name=‘邮箱‘, max_length=32)
    roles = models.ManyToManyField(verbose_name=‘拥有的所有角色‘, to=‘Role‘, blank=True)

    def __str__(self):
        return self.name

获取权限信息

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.conf import settings


def init_permission(current_user, request):
    """
    用户权限的初始化
    :param current_user: 当前用户对象
    :param request: 请求相关所有数据
    :return:
    """
    # 2. 权限信息初始化
    # 根据当前用户信息获取此用户所拥有的所有权限,并放入session。
    # 当前用户所有权限
    permission_queryset = current_user.roles.filter(permissions__isnull=False).values("permissions__id",
                                                                                      "permissions__title",
                                                                                      "permissions__url",
                                                                                      "permissions__pid_id",
                                                                                      "permissions__menu_id",
                                                                                      "permissions__menu__title",
                                                                                      "permissions__menu__icon"
                                                                                      ).distinct()

    # 3. 获取权限+菜单信息
    permission_list = []

    menu_dict = {}

    for item in permission_queryset:

        permission_list.append(
            {‘id‘: item[‘permissions__id‘], ‘url‘: item[‘permissions__url‘], ‘pid‘: item[‘permissions__pid_id‘]})

        menu_id = item[‘permissions__menu_id‘]
        if not menu_id:
            continue
        node = {‘id‘: item[‘permissions__id‘], ‘title‘: item[‘permissions__title‘], ‘url‘: item[‘permissions__url‘]}

        if menu_id in menu_dict:
            menu_dict[menu_id][‘children‘].append(node)
        else:
            menu_dict[menu_id] = {
                ‘title‘: item[‘permissions__menu__title‘],
                ‘icon‘: item[‘permissions__menu__icon‘],
                ‘children‘: [node, ]
            }

    request.session[settings.PERMISSION_SESSION_KEY] = permission_list
    request.session[settings.MENU_SESSION_KEY] = menu_dict

默认展开非菜单URL

上一篇:WEB前端第五课——HTML表单


下一篇:在MySQL中如果发现乱码的情况该如何判断原因及应对?