diff --git a/hiboo/account/forms.py b/hiboo/account/forms.py index 709181a879880d2e76efa548ee8530d5c13d61eb..11ddbc9cc4e778050832b5b1e54d1ac686015cdb 100644 --- a/hiboo/account/forms.py +++ b/hiboo/account/forms.py @@ -1,3 +1,4 @@ +from hiboo.captcha import captcha from wtforms import validators, fields from flask_babel import lazy_gettext as _ @@ -19,6 +20,7 @@ class SignupForm(flask_wtf.FlaskForm): password = fields.PasswordField(_('Password'), [validators.DataRequired()]) password2 = fields.PasswordField(_('Confirm password'), [validators.DataRequired(), validators.EqualTo('password')]) + captcha = captcha.GeneratedTextImageCaptchaField('Prove that you are human, copy the following text') submit = fields.SubmitField(_('Sign up')) diff --git a/hiboo/captcha/captcha.py b/hiboo/captcha/captcha.py index 3ce9038fcaf8a1d6d858f6a018ebd4b70b4fdb8b..0b93874fb2a44933b25470050958f9e01a22518f 100644 --- a/hiboo/captcha/captcha.py +++ b/hiboo/captcha/captcha.py @@ -1,9 +1,12 @@ from wtforms import widgets +from hiboo import captcha from hiboo.captcha import fields +from flask_babel import lazy_gettext as _ import random import string import io +import os import base64 from PIL import Image, ImageDraw, ImageFont @@ -49,9 +52,10 @@ class ImageCaptchaField(fields.CaptchaField): widget = widgets.TextInput() def __init__(self, *args, **kwargs): - self.font = ImageFont.truetype("captcha.ttf", 14) + font_path = os.path.join(captcha.__path__[0], "captcha.ttf") + self.font = ImageFont.truetype(font_path, 14) self.bgcolor = "#ffffff" - self.fgcolor = "#ff6677" + self.fgcolor = "#000000" super(ImageCaptchaField, self).__init__(*args, **kwargs) def __call__(self, **kwargs): @@ -59,7 +63,6 @@ class ImageCaptchaField(fields.CaptchaField): self.make_image(self.context["challenge"]).save(png, "PNG") output = base64.b64encode(png.getvalue()).decode("ascii") return widgets.HTMLString( - '<p>{}</p>'.format(self.context["challenge"]) + '<p><img src="data:image/png;base64,{}"></p>'.format(output) + super(ImageCaptchaField, self).__call__(**kwargs) ) @@ -105,7 +108,6 @@ class GeneratedTextImageCaptchaField(ImageCaptchaField): charset = string.ascii_uppercase + string.digits def challenge(self): - self.label = "Please copy the following text: " length = random.randint(5,8) return "".join(random.choice(self.charset) for _ in range(length)) @@ -113,4 +115,4 @@ class GeneratedTextImageCaptchaField(ImageCaptchaField): return challenge.lower() == field.data.lower() def make_image(self, challenge): - return self.make_text(challenge) + return self.make_text(challenge + " ") diff --git a/hiboo/captcha/captcha.ttf b/hiboo/captcha/captcha.ttf new file mode 100644 index 0000000000000000000000000000000000000000..e5f7eecce43be41ff0703ed99e1553029b849f14 Binary files /dev/null and b/hiboo/captcha/captcha.ttf differ diff --git a/hiboo/captcha/forms.py b/hiboo/captcha/forms.py deleted file mode 100644 index 41aadbbd9b47e068261dd642fd4e506987ca1078..0000000000000000000000000000000000000000 --- a/hiboo/captcha/forms.py +++ /dev/null @@ -1,10 +0,0 @@ -from wtforms import fields -from hiboo.captcha import captcha - -import flask_wtf - - -class DisplayForm(flask_wtf.FlaskForm): - test = fields.StringField() - captcha = captcha.GeneratedTextImageCaptchaField() - submit = fields.SubmitField() diff --git a/hiboo/captcha/views.py b/hiboo/captcha/views.py index 6ee1df1ec2b5414a905f09457cc763078fa96e45..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 --- a/hiboo/captcha/views.py +++ b/hiboo/captcha/views.py @@ -1,12 +0,0 @@ -from hiboo.captcha import blueprint, forms - -import flask - - -@blueprint.route("/captcha", methods=["GET", "POST"]) -def captcha(): - form = forms.DisplayForm() - if form.validate_on_submit(): - return "captcha is ok" - else: - return flask.render_template("captcha.html", form=form)