Python学习路程day19
Python之路,Day19 - Django 进阶
本节内容
自定义template tags
中间件
CRSF
权限管理
分页
Django分页
https://docs.djangoproject.com/en/1.9/topics/pagination/
自定义template tags
https://docs.djangoproject.com/es/1.9/howto/custom-template-tags/
权限管理
django 自带有基本的权限管理 ,但粒度和限制权限的维度都只是针对具体的表,如果我们想根据业务功能来限制权限,那就得自己写了, 不过也不用完全自己的写,我们可以在django 自带的权限基础上轻松的实现扩展。
自己写权限要注意:
- 权限系统的设计对开发者、用户要实现透明,即他们不需要改变自己原有的使用系统或调用接口的方式
- 权限要易扩展,灵活
- 权限要能实现非常小的粒度的控制,甚至细致到一个按键某个用户是否能按。
想对一个功能实现权限控制,要做到只能过在views方法上加一个装饰器就行了,比如:
@check_permission
@login_required
def customer_detail(request,customer_id):
customer_obj = models.Customer.objects.get(id=customer_id)
customer_form = forms.CustomerDetailForm(instance=customer_obj) if request.method == 'POST':
customer_form = forms.CustomerDetailForm(request.POST,instance=customer_obj)
if customer_form.is_valid():
customer_form.save()
parent_base_url = '/'.join(request.path.split('/')[:-2])
print("url:",parent_base_url )
return redirect(parent_base_url)
else:
print(customer_form.errors)
return render(request,'crm/customer_detail.html',{'customer_form':customer_form})
check_permission的代码实现
#_*_coding:utf-8_*_
__author__ = 'Alex Li'
from django.core.urlresolvers import resolve
from django.shortcuts import render,redirect perm_dic = {
'view_customer_list': ['customer_list','GET',[]],
'view_customer_info': ['customer_detail','GET',[]],
'edit_own_customer_info': ['customer_detail','POST',['test']],
} def perm_check(*args,**kwargs):
request = args[0]
url_resovle_obj = resolve(request.path_info)
current_url_namespace = url_resovle_obj.url_name
#app_name = url_resovle_obj.app_name #use this name later
print("url namespace:",current_url_namespace)
matched_flag = False # find matched perm item
matched_perm_key = None
if current_url_namespace is not None:#if didn't set the url namespace, permission doesn't work
print("find perm...")
for perm_key in perm_dic:
perm_val = perm_dic[perm_key]
if len(perm_val) == 3:#otherwise invalid perm data format
url_namespace,request_method,request_args = perm_val
print(url_namespace,current_url_namespace)
if url_namespace == current_url_namespace: #matched the url
if request.method == request_method:#matched request method
if not request_args:#if empty , pass
matched_flag = True
matched_perm_key = perm_key
print('mtched...')
break #no need looking for other perms
else:
for request_arg in request_args: #might has many args
request_method_func = getattr(request,request_method) #get or post mostly
#print("----->>>",request_method_func.get(request_arg))
if request_method_func.get(request_arg) is not None:
matched_flag = True # the arg in set in perm item must be provided in request data
else:
matched_flag = False
print("request arg [%s] not matched" % request_arg)
break #no need go further
if matched_flag == True: # means passed permission check ,no need check others
print("--passed permission check--")
matched_perm_key = perm_key
break else:#permission doesn't work
return True if matched_flag == True:
#pass permission check
perm_str = "crm.%s" %(matched_perm_key)
if request.user.has_perm(perm_str):
print("\033[42;1m--------passed permission check----\033[0m")
return True
else:
print("\033[41;1m ----- no permission ----\033[0m")
print(request.user,perm_str)
return False
else:
print("\033[41;1m ----- no matched permission ----\033[0m")
def check_permission(func): def wrapper(*args,**kwargs):
print("---start check perms",args[0])
if not perm_check(*args,**kwargs):
return render(args[0],'crm/403.html')
return func(*args,**kwargs)
#print("---done check perms")
return wrapper
50行实现细粒度的权限控制
Middleware中间件
https://docs.djangoproject.com/es/1.9/topics/http/middleware/#process_request
Python学习路程day19的更多相关文章
- Python学习路程day18
Python之路,Day18 - Django适当进阶篇 本节内容 学员管理系统练习 Django ORM操作进阶 用户认证 Django练习小项目:学员管理系统设计开发 带着项目需求学习是最有趣和效 ...
- Python学习路程day16
Python之路,Day14 - It's time for Django 本节内容 Django流程介绍 Django url Django view Django models Django te ...
- Python学习路程day8
Socket语法及相关 socket概念 A network socket is an endpoint of a connection across a computer network. Toda ...
- Python学习路程day6
shelve 模块 shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式 import shelve d = shelve.open ...
- python 学习路程(一)
好早之前就一直想学python,可是一直没有系统的学习过,给自己立个flag,从今天开始一步步掌握python的用法: python是一种脚本形式的语言,据说是面向废程序员学习开发使用的,我觉得很适合 ...
- Python学习路程-常用设计模式学习
本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复 ...
- Python学习路程day15
Python之路[第十五篇]:Web框架 Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/en ...
- Python学习路程day17
常用算法与设计模式 选择排序 时间复杂度 二.计算方法 1.一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费 ...
- Python学习路程day12
前端内容学习:HTML和CSS <!DOCTYPE html> <html lang="en"> <head> <meta http-eq ...
随机推荐
- 【翻译】安卓新播放器EXOplayer介绍
http://developer.android.com/guide/topics/media/exoplayer.html 前言: Playing videos and music is a p ...
- Shell脚本快速入门
最近看了下Shell脚本.曾经遇到很多现成的工具包里边就多次用到了Shell脚本.总之这东西的作用无非就是将一系列的操作进行整合. ·整合后使得一套工作更加模块化规范化. ·批量处理要比手动操作快得多 ...
- Seajs教程 配置文档
seajs.config Obj alias Obj 别名配置,配置之后可在模块中使用require调用require('jQuery'); seajs.config({ alias:{ 'jquer ...
- chrome 扩展包 postman 的安装
由于chrome网上应用不能使用.添加扩展程序,需要其他的办法. 1.下载postman安装包.下载地址 2.这一步按照这个下载包中的方法,也可以,可以忽略其错误. 先解压出crx,使用两个办法,使用 ...
- Kettle6.0安装及问题总结-白痴教程
1.安装JDK 配置java环境变量 2.安装KETTLE: 官方下载地址:http://community.pentaho.com/projects/data-integration/ 下载完后,解 ...
- HDFS副本存放策略
在client向DataNode写入block之前,会与NameNode有一次通信,由NameNode来选择指定数目的DataNode来存放副本.具体的副本选择策略在BlockPlacementPol ...
- Git & GitHub
使用 Git 和 GitHub 有一段时间了,总结下经验. 起初接触 Git 是先遇到 GitHub 的,当时傻傻分不清这两者的区别,毕竟名字都那么像,刚开始只想用酷酷的方法 clone 代码(SSH ...
- 20140701立项 移植WatermarkLabelSys
开始移植WatermarkLabelSys,从一个版本中抽离出最原始的内核,不求完善,只求能运行.时间半个月. 顺利的话针对不同的后缀.进程开始添加规则细节,时间1个月. 在顺利的话,兼容性测试,完善 ...
- 用C#实现的内存映射
当文件过大时,无法一次性载入内存时,就需要分次,分段的载入文件 主要是用了以下的WinAPI LPVOID MapViewOfFile(HANDLE hFileMappingObject, DWORD ...
- 用Pyinstaller打包发布exe应用 (转)经测可用
安装Pyinstaller 1 按照习惯,我们使用pip来安装模块.我们一直以来强调,要用最偷懒的方法.写代码的人尤其如此.人生苦短,你要偷懒~ 0Python | 如何用pip安装模块和包 ...