权限管理——页面封装操作
1.不封装:
<body> {% if "add" in request.permisstion_code_list %} <a href="">添加</a> {% endif %} <table> {% for foo in data_list %} <tr> <td>{{ foo.id }}</td> <td>{{ foo.name }}</td> {% if 'del' in request.permisstion_code_list %} <td><a href="">删除</a></td> {% endif %} {% if 'edit' in request.permisstion_code_list %} <td><a href="">编辑</a></td> {% endif %} </tr> {% endfor %} </table> </body>
def userinfo(request): print(request.permisstion_code_list) data_list = [ {'id':1,'name':'xxx1'}, {'id':2,'name':'xxx2'}, {'id':3,'name':'xxx3'}, {'id':4,'name':'xxx4'}, {'id':5,'name':'xxx5'}, ] return render(request,'userinfo.html',{'data_list':data_list})
2.封装
class BasePagePermission(object): def __init__(self,code_list): self.code_list = code_list def has_add(self): if "add" in self.code_list: return True def has_edit(self): if "edit" in self.code_list: return True def has_del(self): if 'del' in self.code_list: return True def userinfo(request): print(request.permission_code_list) # return HttpResponse('用户列表页面') page_permission= BasePagePermission(request.permission_code_list) data_list = [ {'id':1 , 'name':'xxx1'}, {'id':2 , 'name':'xxx2'}, {'id':3 , 'name':'xxx3'}, {'id':4 , 'name':'xxx4'}, {'id':5 , 'name':'xxx5'}, ] return render(request,'userinfo.html',{'data_list':data_list,'page_permission':page_permission})
def userinfo_add(request):
page_permission = BasePagePermission(request.permisstion_code_list)
return HttpResponse('添加用户页面')
class OrderPagePermission(BasePagePermission):
def has_report(self):
if 'report' in self.code_list:
return True
def order(request):
order_permission = OrderPagePermission(request.permisstion_code_list)
{% if page_permission.has_add %} <a href="#">添加</a> {% endif %} <table> {% for row in data_list %} <tr> <td>{{ row.id }}</td> <td>{{ row.name }}</td> {% if page_permission.has_edit %} <td><a href="#">编辑</a></td> {% endif %} {% if page_permission.has_del %} <td><a href="#">删除</a></td> {% endif %} </tr> {% endfor %} </table>