Я пытаюсь добавить Google reCAPTCHA на сайт Flask, но у меня проблемы с его реализацией. У меня есть reCAPTCHA, отображаемая в форме регистрации на сайте, и у меня есть необходимые ключи сайта Google, но я не могу понять, как отформатировать проверки и запустить их. Пожалуйста, помогите.
@app.route('/register', methods=['POST'])
def register():
cur = g.db.cursor()
user = request.form
errors = []
if user['code']:
cur.execute('select * from users where referral_code=%s', (user['code'],))
referrer = cur.fetchone()
if not referrer:
errors.append('invalid-code')
if not user['email']:
errors.append('missing-email')
elif not validate_email.validate_email(user['email'], check_mx=True):
errors.append('invalid-email')
else:
cur.execute('select id from users where email=%s', (user['email'],))
if cur.fetchone():
errors.append('email-exists')
if not user['first_name']:
errors.append('missing-first-name')
if not user['last_name']:
errors.append('missing-last-name')
if not user['county']:
errors.append('missing-county')
if not user['password']:
errors.append('missing-password')
elif user['password'] != user['password2']:
errors.append('passwords-dont-match')
token = uuid.uuid4().hex
if not errors:
try:
cur.execute("insert into users (email, email_token, password, first_name, last_name, county, created, last_login) values(%s, %s, %s, %s, %s, %s, utc_timestamp(), utc_timestamp())",
(user['email'], token, user['password'], user['first_name'], user['last_name'], user['county']))
except MySQLdb.IntegrityError:
errors.append('email-exists')
if errors:
user = user.copy()
user['errors'] = errors
session['registration-user'] = user
return redirect('/registration')
else:
#cur.commit()
session.pop('registration-user', None)
user_id = cur.lastrowid
done = False
while not done:
referral_code = ''.join([random.choice('bcdfghjklmnpqrstvwxyz') for _ in range(4)])
try:
cur.execute('update users set referral_code=%s where id=%s', (referral_code, user_id))
done = True
except MySQLdb.IntegrityError:
pass
if user['code']:
cur.execute('update users set referrer_id=%s where id=%s', (referrer['id'], user_id))
session['UID'] = user_id
send_confirmation_email(user_id, request.url_root)
cur.execute('select * from users')
users = cur.fetchall()
for u in users:
if u['email'] in ADMINS:
send_mail('------ <t------@-----.com>', u['id'], 'new-user', u['email'], 'New user created', 'A new user, %s %s, was created'%(user['first_name'], user['last_name']))
return redirect('/account')