码迷,mamicode.com
首页 > 其他好文 > 详细

Django-权限信息自定义标签

时间:2018-03-29 16:00:46      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:tin   imp   end   his   values   获取   isp   pat   lock   

自定义权限标签:

import re
from django.template import Library
from django.conf import settings
register = Library()

@register.inclusion_tag("rbac/xxxxx.html")
def menu_html(request):
    """
    去Session中获取菜单相关信息,匹配当前URL,生成菜单
    :param request:
    :return:
    """
    menu_list = request.session[settings.PERMISSION_MENU_KEY]
    current_url = request.path_info

    menu_dict = {}
    for item in menu_list:
        if not item[menu_gp_id]:
            menu_dict[item[id]] = item

    for item in menu_list:
        regex = "^{0}$".format(item[url])
        if re.match(regex,current_url):
            menu_gp_id = item[menu_gp_id]
            if menu_gp_id:
                menu_dict[menu_gp_id][active] = True
            else:
                menu_dict[item[id]][active] = True

    result = {}
    for item in menu_dict.values():
        active = item.get(active)
        menu_id = item[menu_id]
        if menu_id in result:
            result[menu_id][children].append({ title: item[title], url: item[url],active:active})
            if active:
                result[menu_id][active] = True
        else:
            result[menu_id] = {
                menu_id:item[menu_id],
                menu_title:item[menu_title],
                active:active,
                children:[
                    { title: item[title], url: item[url],active:active}
                ]
            }

    return {menu_dict:result}
rbac/xxxx.html

{% for k,item in menu_dict.items %}
    <div class="item">
        <div class="item-title">{{ item.menu_title }}</div>
        {% if item.active %}
            <div class="item-permission">
        {% else %}
            <div class="item-permission hide">
        {% endif %}
            {% for v in item.children %}

                {% if v.active %}
                    <a href="{{ v.url }}" class="active">{{ v.title }}</a>
                {% else %}
                    <a href="{{ v.url }}">{{ v.title }}</a>
                {% endif %}

            {% endfor %}
        </div>
    </div>
{% endfor %}

############样式相关

# css
.item-permission{
    padding: 3px 10px;
}
.item-permission a{
    display: block;
}
.item-permission a.active{
    color: red;
}
.hide{
    display: none;
}
# js
$(function () {
$(.item .item-title).click(function () {
    if($(this).next().hasClass(hide)){
        $(this).next().removeClass(hide)
    }else{
        $(this).next().addClass(hide)
    }
})
});

 

Django-权限信息自定义标签

标签:tin   imp   end   his   values   获取   isp   pat   lock   

原文地址:https://www.cnblogs.com/52-qq/p/8669816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!