Example #1
0
def send_register_email(domain, username, email):
    db = get_db()
    code = urlsafe_b64encode(urandom(64)).decode('utf-8')

    # 过期时间30分钟
    delta = timedelta(minutes=30)
    ctime = int(time.time())
    expired = ctime + delta.seconds

    db.execute(
        'INSERT INTO user_email_verify'
        ' (email, code, expired, ctime) VALUES (?,?,?,?)',
        (email, code, expired, ctime)
    )
    db.commit()

    celery = create_celery(current_app)
    celery.send_task(name='tasks.send_register_email',
                     args=[
                         domain,
                         username,
                         email,
                         code
                     ])

    return 'test'
Example #2
0
def find_password():
    if request.method == 'POST':
        email = request.form['email']
        if email is None:
            flash('Email address is empty', 'warning')
        elif email and not check_email_safe(email):
            flash('Email address is invalidate', 'warning')
        else:
            db = get_db()
            row = db.execute(
                'SELECT * FROM user WHERE email = ?',
                (email,)
            ).fetchone()

            if row is None:
                flash('Email is not registered', 'warning')
            else:
                row = db.execute(
                    'SELECT * FROM find_password_verify WHERE email = ?'
                    ' ORDER BY id DESC',
                    (email,)
                ).fetchone()

                ctime = int(time.time())

                if row and \
                        not check_find_password_email_is_expired(row, ctime):
                    flash('so quickly, please slowly', 'warning')
                else:
                    code = urlsafe_b64encode(urandom(64)).decode('utf-8')

                    # 过期时间30分钟
                    delta = timedelta(minutes=30)

                    expired = ctime + delta.seconds

                    db.execute(
                        'INSERT INTO find_password_verify'
                        ' (email, code, expired, ctime) VALUES (?,?,?,?)',
                        (email, code, expired, ctime)
                    )
                    db.commit()

                    celery = create_celery(current_app)
                    celery.send_task(
                        name='tasks.find_pass_email',
                        args=[
                            get_http_domain(),
                            email,
                            code
                        ])
                    flash('Email send success', 'info')

    return render_template('auth/find_password.j2')
Example #3
0
def login():
    if request.method == 'POST':
        username = request.form[
            'username'] if 'username' in request.form else ''
        password = request.form[
            'password'] if 'password' in request.form else ''
        code = request.form[
            'verification-code'] if 'verification-code' in request.form else ''

        db = get_db()
        error = None

        user = db.execute(
            'SELECT * FROM user '
            ' WHERE (username = ? OR email = ?)'
            ' AND email <> "" and status = 1',
            (username, username)
        ).fetchone()

        if user is None:
            error = 'Incorrect username.'
            current_app.logger.error(
                '%s logged fail - username is wrong', username)
        elif not check_password_hash(user['password'], password):
            current_app.logger.error(
                '%s logged fail - password is wrong', username)
            error = 'Incorrect password.'
        elif not code:
            error = 'Verification code is required'
        elif code and not Captcha.captcha_validate(code):
            error = 'Verification code is wrong'

        if error is None:
            session.clear()
            session['user_id'] = user['id']

            celery = create_celery(current_app)
            if celery:
                celery.send_task(
                    name='tasks.send_login_email',
                    args=[
                        get_http_domain(),
                        user['username'],
                        user['email']
                    ]
                )

            return redirect(url_for('blog.index'))

        flash(error)

    return render_template('auth/login.j2')
def together():
    celery = create_celery(current_app)
    result = celery.send_task(name='tasks.add_together', args=(2, 3))
    print(result.wait())
    return 'success'
Example #5
0
# -*- coding: utf-8 -*-
# @Author: durban.zhang
# @Date:   2019-11-13 16:30:07
# @Last Modified by:   durban.zhang
# @Last Modified time: 2019-11-28 10:41:14
from flask_mail import Message
from baby.extensions import mail
from baby import create_app
from baby.celery import create_celery
celery = create_celery(create_app())


@celery.task(name="tasks.add_together")
def add_together(a, b):
    return a + b


@celery.task(name="tasks.send_login_email")
def send_login_email(domain, user, email):

    subject = 'Hello, %s' % user
    html = '<p>Login <a target="_blank" href="%s">%s</a> Success</p>'
    msg = Message(
        sender='Walkerfree <*****@*****.**>',
        subject=subject,
        html=html % (domain, domain),
        recipients=[email]
    )

    mail.send(msg)