整理代码

This commit is contained in:
zemal
2017-08-20 08:35:59 +08:00
parent 0647312124
commit 3b1f02c356
10 changed files with 200 additions and 268 deletions

View File

@@ -17,30 +17,30 @@ from math import ceil
from six import BytesIO
from PIL import Image, ImageDraw, ImageFont
__version__ = '0.3.3'
__version__ = "0.3.3"
current_path = os.path.normpath(os.path.dirname(__file__))
class Captcha(object):
class Captcha(object):
def __init__(self, request):
""" something init
"""
self.django_request = request
self.session_key = '_django_captcha_key'
self.session_key = "_django_captcha_key"
self.words = ["hello", "word"]
# image size (pix)
self.img_width = 150
self.img_height = 30
self.type = 'number'
self.mode = 'number'
self.type = "number"
self.mode = "number"
def _get_font_size(self):
s1 = int(self.img_height * 0.8)
s2 = int(self.img_width/len(self.code))
return int(min((s1, s2)) + max((s1, s2))*0.05)
s2 = int(self.img_width / len(self.code))
return int(min((s1, s2)) + max((s1, s2)) * 0.05)
def _get_words(self):
""" words list
@@ -51,7 +51,6 @@ class Captcha(object):
if self.words:
return set(self.words)
def _set_answer(self, answer):
self.django_request.session[self.session_key] = str(answer)
@@ -86,16 +85,16 @@ class Captcha(object):
"""
# font color
self.font_color = ['black', 'darkblue', 'darkred']
self.font_color = ["black", "darkblue", "darkred"]
# background color
self.background = (random.randrange(230, 255), random.randrange(230, 255), random.randrange(230, 255))
# font path
self.font_path = os.path.join(current_path, 'timesbi.ttf') # or Menlo.ttc
self.font_path = os.path.join(current_path, "timesbi.ttf") # or Menlo.ttc
self.django_request.session[self.session_key] = ''
im = Image.new('RGB', (self.img_width, self.img_height), self.background)
self.django_request.session[self.session_key] = ""
im = Image.new("RGB", (self.img_width, self.img_height), self.background)
self.code = self._generate()
# set font size automaticly
@@ -105,20 +104,20 @@ class Captcha(object):
draw = ImageDraw.Draw(im)
# draw noisy point/line
if self.mode == 'word':
c = int(8/len(self.code)*3) or 3
elif self.mode == 'number':
if self.mode == "word":
c = int(8 / len(self.code) * 3) or 3
elif self.mode == "number":
c = 4
for i in range(random.randrange(c-2, c)):
for i in range(random.randrange(c - 2, c)):
line_color = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
xy = (random.randrange(0, int(self.img_width*0.2)), random.randrange(0, self.img_height),
random.randrange(int(3*self.img_width/4), self.img_width), random.randrange(0, self.img_height))
draw.line(xy, fill=line_color, width=int(self.font_size*0.1))
xy = (random.randrange(0, int(self.img_width * 0.2)), random.randrange(0, self.img_height),
random.randrange(int(3 * self.img_width / 4), self.img_width), random.randrange(0, self.img_height))
draw.line(xy, fill=line_color, width=int(self.font_size * 0.1))
# main part
j = int(self.font_size*0.3)
k = int(self.font_size*0.5)
j = int(self.font_size * 0.3)
k = int(self.font_size * 0.5)
x = random.randrange(j, k)
for i in self.code:
@@ -126,21 +125,21 @@ class Captcha(object):
m = int(len(self.code))
y = random.randrange(1, 3)
if i in ('+', '=', '?'):
if i in ("+", "=", "?"):
# 对计算符号等特殊字符放大处理
m = ceil(self.font_size*0.8)
m = ceil(self.font_size * 0.8)
else:
# 字体大小变化量,字数越少,字体大小变化越多
m = random.randrange(0, int(45 / self.font_size) + int(self.font_size/5))
m = random.randrange(0, int(45 / self.font_size) + int(self.font_size / 5))
self.font = ImageFont.truetype(self.font_path.replace('\\', '/'), self.font_size + int(ceil(m)))
self.font = ImageFont.truetype(self.font_path.replace("\\", "/"), self.font_size + int(ceil(m)))
draw.text((x, y), i, font=self.font, fill=random.choice(self.font_color))
x += self.font_size*0.9
x += self.font_size * 0.9
del x
del draw
with BytesIO() as buf:
im.save(buf, 'gif')
im.save(buf, "gif")
buf_str = buf.getvalue()
return buf_str
@@ -152,7 +151,6 @@ class Captcha(object):
return False
code = code.strip()
_code = self.django_request.session.get(self.session_key) or ''
self.django_request.session[self.session_key] = ''
_code = self.django_request.session.get(self.session_key) or ""
self.django_request.session[self.session_key] = ""
return _code.lower() == str(code).lower()

View File

@@ -13,7 +13,7 @@ class Command(BaseCommand):
"would you like to reset it's password?\n"
"Input yes to confirm: "))
if input() == "yes":
# for dev
# todo remove this in product env
# rand_password = rand_str(length=6)
rand_password = "rootroot"
admin.save()