-
Notifications
You must be signed in to change notification settings - Fork 2
/
commonmt.py
100 lines (85 loc) · 3.03 KB
/
commonmt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#coding:UTF-8
from django.http import HttpResponseRedirect,HttpResponse
from datetime import *
#全局打印,Debug专用,正式发布时清空此方法体
def gprint(str, decode="", encode=""):
print "-------------start gprint-------------"
if decode:
print str.decode("utf8").encode("gbk")
else:
print str
print "-------------end gprint-------------"
#判断字符串 stri 是否为空,为空返回 False,否则返回去掉空格的 stri
def gisempty(stri):
if(stri==None or stri==""):
return False
stri=stri.strip()
if(stri != ""):
return stri
else:
return False
#验证码
def verifycode(request):
import Image,ImageDraw,ImageFont,md5,cStringIO,random
background = (random.randrange(230,255),random.randrange(230,255),random.randrange(230,255))
line_color = (random.randrange(0,255),random.randrange(0,255),random.randrange(0,255))
font_color = ['black','darkblue','darkred']
img_width = 60
img_height = 30
font = ImageFont.truetype("wqy-microhei.ttc", 17) #开源字体
#font=ImageFont.load_default() #默认字体好过没有,不能设置大小!
im = Image.new('RGB',(img_width,img_height),background)
draw = ImageDraw.Draw(im)
mp = md5.new()
mp_src = mp.update(str(datetime.now()))
mp_src = mp.hexdigest()
rand_str = mp_src[0:4]
#画干扰线
for i in range(random.randrange(2,10)):
xy = (random.randrange(0,img_width),random.randrange(0,img_height),
random.randrange(0,img_width),random.randrange(0,img_height))
draw.line(xy,fill=line_color,width=1)
#写入验证码文字
x = 2
for i in rand_str:
y = random.randrange(0,10)
draw.text((x,y), i, font=font, fill=random.choice(font_color))
x += 14
del x
del draw
request.session['captcha'] = rand_str #增加到session
gprint(rand_str)
buf = cStringIO.StringIO()
im.save(buf, 'png')
buf.closed
return HttpResponse(buf.getvalue(),'image/png')
def giflogin(request):
'''
用户是否登陆
magicalboy 11.11.24
'''
loginid = request.session.get('uid')
if( not loginid or loginid=='None'):
return False;
return loginid;
def ggetsession(request):
'''
所有页面可以取得的session
magicalboy 11.11.24
'''
uid = request.session.get('uid')
username = request.session.get('username')
nickname = request.session.get('nickname')
realname = request.session.get('realname')
email = request.session.get('email')
gender = request.session.get('gender')
province = request.session.get('province')
city = request.session.get('city')
county = request.session.get('county')
address = request.session.get('address')
phone = request.session.get('phone')
return {
'uid':str(uid),'username':username,'nickname':nickname,'realname':realname,
'email':email,'gender':gender,'province':province,'city':city,'county':county,
'address':address,'phone':phone
}