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

权限控制到按钮

时间:2020-03-24 00:53:53      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:删除   distinct   来源   val   perm   center   opened   oct   ini   

技术图片
 1 from app01.models import Role
 2 
 3 def initial_session(user,request):
 4     permissions = Role.objects.filter(user=user).values(permissions__url,
 5                                                         permissions__title,
 6                                                         permissions__name,
 7                                                         permissions__menu__title,
 8                                                         permissions__menu__icon,
 9                                                         permissions__menu__pk,
10                                                         ).distinct()
11     permission_list = []
12     permission_names = []
13     permission_menu_dict = {}
14     for item in permissions:
15         permission_list.append(item[permissions__url])
16         permission_names.append(item[permissions__name])
17         menu_pk = item[permissions__menu__pk]
18         if menu_pk:
19             if menu_pk not in permission_menu_dict:
20                 permission_menu_dict[menu_pk] = {
21                     "menu_title": item[permissions__menu__title],
22                     "menu_icon": item[permissions__menu__icon],
23                     "children": [
24                         {
25                             "title": item[permissions__title],
26                             "url": item[permissions__url],
27                         }
28                     ],
29                 }
30             else:
31                 permission_menu_dict[menu_pk]["children"].append({
32                     "title": item[permissions__title],
33                     "url": item[permissions__url],
34                 })
35     print(permission_menu_dict)
36     request.session[permission_list] = permission_list
37     request.session[permission_names] = permission_names
38     request.session[permission_menu_dict] = permission_menu_dict
rbac.py
技术图片
import re

from django.template import Library

register=Library()
@register.inclusion_tag("rbac/menu.html")
def get_menu_style(request):
    permission_menu_dict = request.session.get(permission_menu_dict)
    for val in permission_menu_dict.values():
        for item in val["children"]:
            val["class"] = "active"
            ret = re.search("^{}$".format(item[url]), request.path)
            if ret:
                val["class"] = ""
    return {permission_menu_dict:permission_menu_dict}

@register.filter
def has_permission(btn_url, request):
    permission_names=request.session.get("permission_names")
    return btn_url in permission_names
templatetags/rbac.py
技术图片
 1 from django.db import models
 2 
 3 # Create your models here.
 4 class User(models.Model):
 5     name=models.CharField(max_length=11)
 6     pwd=models.CharField(max_length=11)
 7     roles=models.ManyToManyField(Role)
 8     def __str__(self):
 9         return self.name
10 
11 class Role(models.Model):
12     title=models.CharField(max_length=11)
13     permissions=models.ManyToManyField(Permission)
14     def __str__(self):
15         return self.title
16 
17 class Menu(models.Model):
18     title=models.CharField(max_length=32,verbose_name="菜单")
19     icon=models.CharField(max_length=32,verbose_name="图标",null=True,blank=True)
20 
21 class Permission(models.Model):
22     title=models.CharField(max_length=32)
23     url=models.CharField(max_length=32)
24     menu=models.ForeignKey(Menu,on_delete=models.CASCADE,null=True)
25     name=models.CharField("url别名",max_length=32,default="")
26 
27     def __str__(self):
28         return self.title
models.py
技术图片
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>首页</title>
 6     <link href="https://cdn.bootcss.com/twitter-bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet">
 7     <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
 8     <style>
 9         .active {
10             border-left: 2px solid #FF34B3;
11         }
12     </style>
13 </head>
14 <body>
15 <div class="container-fluid">
16     <div class="row">
17         <div class="col-md-3" style="background-color: pink">
18             {% load rbac %}
19             {% get_menu_style request %}
20 
21         </div>
22         <div class="col-md-9">
23             {% if "customers_add"|has_permission:request %}
24                 <a href="/customers/add/" class="btn btn-warning"><i class="fa fa-plus-square" aria-hidden="true"></i> 添加客户</a>
25             {% endif %}
26             <table class="text-center table table-bordered table-hover">
27                 <thead>
28                     <tr>
29                         {% if "orders_edit"|has_permission:request %}
30                             <th>编辑</th>
31                         {% endif %}
32 
33                         <th>删除</th>
34                     </tr>
35                 </thead>
36                 <tbody>
37                     <tr>
38                         {% if "orders_edit"|has_permission:request %}
39                         <td><a href="/orders/editor/2"><i class="fa fa-edit" aria-hidden="true"></i></a></td>
40                         {% endif %}
41                         <td><a href="/orders/delete/3"><i class="fa fa-trash-o fa-lg"></i></a></td>
42                     </tr>
43                 </tbody>
44             </table>
45             {% block content %}
46 
47             {% endblock %}
48         </div>
49     </div>
50 </div>
51 </body>
52 </html>
index.html
  1 class Customer(models.Model):
  2     """
  3     客户表
  4     """
  5     qq = models.CharField(verbose_name=QQ, max_length=64, unique=True, help_text=QQ号必须唯一)
  6 
  7     name = models.CharField(verbose_name=学生姓名, max_length=16)
  8     gender_choices = ((1, ), (2, ))
  9     gender = models.SmallIntegerField(verbose_name=性别, choices=gender_choices)
 10 
 11     education_choices = (
 12         (1, 重点大学),
 13         (2, 普通本科),
 14         (3, 独立院校),
 15         (4, 民办本科),
 16         (5, 大专),
 17         (6, 民办专科),
 18         (7, 高中),
 19         (8, 其他)
 20     )
 21     education = models.IntegerField(verbose_name=学历, choices=education_choices, blank=True, null=True, )
 22     graduation_school = models.CharField(verbose_name=毕业学校, max_length=64, blank=True, null=True)
 23     major = models.CharField(verbose_name=所学专业, max_length=64, blank=True, null=True)
 24 
 25     experience_choices = [
 26         (1, 在校生),
 27         (2, 应届毕业),
 28         (3, 半年以内),
 29         (4, 半年至一年),
 30         (5, 一年至三年),
 31         (6, 三年至五年),
 32         (7, 五年以上),
 33     ]
 34     experience = models.IntegerField(verbose_name=工作经验, blank=True, null=True, choices=experience_choices)
 35     work_status_choices = [
 36         (1, 在职),
 37         (2, 无业)
 38     ]
 39     work_status = models.IntegerField(verbose_name="职业状态", choices=work_status_choices, default=1, blank=True,
 40                                       null=True)
 41     company = models.CharField(verbose_name="目前就职公司", max_length=64, blank=True, null=True)
 42     salary = models.CharField(verbose_name="当前薪资", max_length=64, blank=True, null=True)
 43 
 44     source_choices = [
 45         (1, "qq群"),
 46         (2, "内部转介绍"),
 47         (3, "官方网站"),
 48         (4, "百度推广"),
 49         (5, "360推广"),
 50         (6, "搜狗推广"),
 51         (7, "腾讯课堂"),
 52         (8, "广点通"),
 53         (9, "高校宣讲"),
 54         (10, "渠道代理"),
 55         (11, "51cto"),
 56         (12, "智汇推"),
 57         (13, "网盟"),
 58         (14, "DSP"),
 59         (15, "SEO"),
 60         (16, "其它"),
 61     ]
 62     source = models.SmallIntegerField(客户来源, choices=source_choices, default=1)
 63     referral_from = models.ForeignKey(
 64         self,
 65         blank=True,
 66         null=True,
 67         verbose_name="转介绍自学员",
 68         help_text="若此客户是转介绍自内部学员,请在此处选择内部学员姓名",
 69         related_name="internal_referral"
 70     )
 71     # course = models.ManyToManyField(verbose_name="咨询课程", to="Course")
 72 
 73     status_choices = [
 74         (1, "已报名"),
 75         (2, "未报名")
 76     ]
 77     status = models.IntegerField(
 78         verbose_name="状态",
 79         choices=status_choices,
 80         default=2,
 81         help_text=u"选择客户此时的状态"
 82     )
 83 
 84     consultant = models.ForeignKey(verbose_name="课程顾问", to=UserInfo, related_name=consultanter,
 85                                    limit_choices_to={depart_id: 1001},null=True,blank=True)
 86     course = MultiSelectField(咨询课程,choices=course_choices,null=True,blank=True)
 87     date = models.DateField(verbose_name="咨询日期", auto_now_add=True)
 88     recv_date = models.DateField(verbose_name="当前课程顾问的接单日期", null=True, blank=True)
 89     last_consult_date = models.DateField(verbose_name="最后跟进日期", null=True,blank=True)
 90     class_list = models.ManyToManyField(ClassList,verbose_name=已报班级,blank=True)
 91 
 92     def __str__(self):
 93         return self.name
 94     def get_classlist(self):
 95         l=[]
 96         for cls in self.class_list.all():
 97             l.append(str(cls))
 98         return mark_safe(<br>.join(l))
 99     def get_status(self):
100         status_color={
101             1:green,
102             2:orange
103         }
104         return mark_safe("<span style=‘background-color:%s;color:white‘>%s</span>"%(status_color[self.status],self.get_status_display()))

 

权限控制到按钮

标签:删除   distinct   来源   val   perm   center   opened   oct   ini   

原文地址:https://www.cnblogs.com/xuqidong/p/12556305.html

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