mirror of
https://github.com/Balshgit/different
synced 2025-09-12 01:20:43 +03:00
28 lines
937 B
Python
28 lines
937 B
Python
from django.conf import settings
|
|
from django.contrib import messages
|
|
from functools import wraps
|
|
|
|
import requests
|
|
|
|
|
|
def check_recaptcha(function):
|
|
@wraps(function)
|
|
def new_func(request, *args, **kwargs):
|
|
request.recaptcha_is_valid = None
|
|
if request.method == 'POST':
|
|
recaptcha_response = request.POST.get('g-recaptcha-response')
|
|
data = {
|
|
'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
|
|
'response': recaptcha_response
|
|
}
|
|
r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
|
|
result = r.json()
|
|
if result['success']:
|
|
request.recaptcha_is_valid = True
|
|
else:
|
|
request.recaptcha_is_valid = False
|
|
messages.error(request, 'Invalid reCAPTCHA. Please try again.')
|
|
return function(request, *args, **kwargs)
|
|
|
|
return new_func
|