rabc 报障权限系统、KindEditor、验证码、图片预览:往浏览器存储图片or内存存储图片,时间分类,ORM内置函数

1、KindEditor 前端JS插件

 

KindEditor 前端JS插件 (还有:CKEditor,TinyEditor,UEEditor)
文件夹说明:
asp # asp 示例(工作场景可酌情删去不用)
asp.net # asp.net 示例(工作场景可酌情删去不用)
attached # 空文件夹,放置关联文件 attached
examples # HTML 示例(工作场景可酌情删去不用)
jsp # java 示例(工作场景可酌情删去不用)
kindeditor-all-min.js # 全部 js (压缩)
kindeditor-all.js # 全部 js (未压缩)
kindeditor-min.js # 仅 kindeditor js (压缩)
kindeditor.js # 仅 kineditor js (未压缩)
lang # 支持语言
license.txt # license
php # php示例(工作场景可酌情删去不用)
themes # kindeditor 主题

中文文档:
http://kindeditor.net/docs/option.html


2、beautifulsoup4
html.parser 是 BeautifulSoup 内部的解析器,他会把 content 里内容解析成对应的对象
soup = BeautifulSoup(content,'html.parser')
属性:
find
find_all
attrs del attrs[xx]
name
decompose
clear

decode() soup 对象转换成字符串
encode() soup 对象转换成字节


权限管理:rbac: role basic access control
根据用户获取权限,session中,中间件实现权限控制




3、验证码
utils目录下random_check_code文件
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random

def rd_check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
code = []
img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')

def rndChar():
"""
生成随机字母
:return:
"""
return chr(random.randint(65, 90))

def rndColor():
"""
生成随机颜色
:return:
"""
return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))

# 写文字
font = ImageFont.truetype(font_file, font_size)
for i in range(char_length):
char = rndChar()
code.append(char)
h = random.randint(0, 4)
draw.text([i * width / char_length, h], char, font=font, fill=rndColor())

# 写干扰点
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

# 写干扰圆圈
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
x = random.randint(0, width)
y = random.randint(0, height)
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())

# 画干扰线
for i in range(5):
x1 = random.randint(0, width)
y1 = random.randint(0, height)
x2 = random.randint(0, width)
y2 = random.randint(0, height)

draw.line((x1, y1, x2, y2), fill=rndColor())

img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
return img,''.join(code)




views.py
def check_code(request):
from utils.random_check_code import rd_check_code
from io import BytesIO # 在内存中开辟空间用于存储IO操作
img, code = rd_check_code()
stream = BytesIO() # 在内存中开辟空间
img.save(stream, 'png') # 把图片保存到开辟的内存空间

# stream.getvalue() # 从内存空间把图片获取出来使用
print(code)
  request.session['cheke_code'] = code


return HttpResponse(stream.getvalue())

4、浏览器存储图片
  var u = windows.URL.createObjectURL(obj) # obj = 文件对象  (文件对象获取:$(this)[0].files[0]  来获取单个对象)
  # u 的返回值得到的是 浏览器上存储的图片 url 地址 可直接使用


<script>

# 页面标签加载完成后执行  bindAvatar
$(function(){
bindAvatar();
});


function changeCode(ths){
ths.src = ths.src + "?";
}

# 判断浏览器支持哪种方法

function bindAvatar(){
if(window.URL.createObjectURL){
bindAvatar2();
}else if(window.FileReader){
bindAvatar3()
}else{
bindAvatar1();
}
}


/*
Ajax上传
*/
function bindAvatar1(){
$('#imgSelect').change(function(){
var obj = $(this)[0].files[0];
// Ajax发送后台,并获取路径
// img.src = 获取路径
})
}


/*
本地上传预览
*/
function bindAvatar2(){
$('#imgSelect').change(function(){
var obj = $(this)[0].files[0];
// Ajax发送后台,并获取路径
// img.src = 获取路径
var v = window.URL.createObjectURL(obj);
$('#previewImg').attr('src',v);
$('#previewImg').load(function(){
window.URL.revokeObjectURL(v);  # 这种方法需要自己手动释放存储空间
});


})
}


function bindAvatar3(){
$('#imgSelect').change(function(){
var obj = $(this)[0].files[0];
// Ajax发送后台,并获取路径
// img.src = 获取路径
var reader = new FileReader();
reader.onload = function(e){
$('#previewImg').attr('src',this.result);
};
reader.readAsDataURL(obj);
})
}


</script>


4、时间分类

# select date_format(create_time,'%Y-%m'),count(nid) as c from article where blog_id=1 group by date_format(create_time,'%Y-%m')
# 2015-09 10
# 2015-10 8


# MySQL                                                              # .extra  额外取一列数据
# date_list = models.Article.objects.filter(blog=blog).extra(select={'c': "date_format(create_time,'%%Y-%%m')"}).values('c').annotate(ct=Count('nid'))

# SQLlite
# date_list = models.Article.objects.filter(blog=blog).extra(select={'c': "strftime('%%Y-%%m',create_time)"}).values('c').annotate(ct=Count('nid'))

"""
nid xx create_time c
1 x 2018-01-01 11:11 2018-01

"""

 

from django.db.models import functions
# models.Article.objects.filter(blog=blog).annotate(x=functions.Extract('create_time','YEAR_MONTH'))
# models.Article.objects.filter(blog=blog).annotate(x=functions.ExtractYear('create_time'))
"""
nid xx create_time x
1 x 2018-09-01 11:11 201809


"""
# models.Article.objects.filter(blog=blog).annotate(x=functions.TruncMonth('create_time'))
# models.Article.objects.filter(blog=blog).annotate(x=functions.Trunc('create_time',''))
"""
nid xx create_time x
1 x 2018-09-01 11:11 09


"""


5、ORM内置函数

# from django.db.models import FloatField
# from django.db.models import Value
# v = models.Article.objects.annotate(c=functions.Cast('nid', FloatField()))  类型转换
# v = models.Article.objects.annotate(c=functions.Coalesce('title','summary'))
# v = models.Article.objects.annotate(c=functions.Concat('nid','title','summary'))  字段拼接
# v = models.Article.objects.annotate(c=functions.Concat('nid','title','summary',Value('666')))  字段拼接+自定义列
# v = models.Article.objects.annotate(c=functions.Greatest('nid','num'))  获取最大值 ps:还有获取最小值
# v = models.Article.objects.annotate(c=functions.Length('title'))  获取长度
# v = models.Article.objects.annotate(c=functions.Substr('title',1,1))  获取子序列:索引位置取值
#
# """
# select *,upper(title) from xxx
# nid title summary create_time num c
# 1 xx s 2018-09-01 11:11 9 x
# """





posted @ 2018-06-14 10:58  G500  阅读(178)  评论(0)    收藏  举报