mirror of
https://github.com/Hopiu/django-fobi.git
synced 2026-05-21 10:51:52 +00:00
clean up
This commit is contained in:
parent
c7fab52d64
commit
ff3e1cdc4a
4 changed files with 0 additions and 92 deletions
|
|
@ -1,51 +0,0 @@
|
|||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
import requests
|
||||
|
||||
__title__ = 'fobi.reusable.invisible_recaptcha.validators'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2013-2018 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('validate_invisible_recaptcha',)
|
||||
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def validate_invisible_recaptcha(data):
|
||||
RECAPTCHA_FIELD = 'g-recaptcha-response'
|
||||
SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'
|
||||
site_secret = getattr(settings, 'FOBI_INVISIBLE_RECAPTCHA_SITE_SECRET', '')
|
||||
recaptcha_response = data.get(RECAPTCHA_FIELD)
|
||||
if not (site_secret and recaptcha_response):
|
||||
return False, 'Captcha validation error'
|
||||
|
||||
response = requests.post(
|
||||
SITE_VERIFY_URL,
|
||||
data={
|
||||
'secret': site_secret,
|
||||
'response': recaptcha_response,
|
||||
}
|
||||
)
|
||||
|
||||
if response.status_code != requests.codes.ok:
|
||||
LOGGER.error(
|
||||
'recaptcha: Error: %s: %s',
|
||||
response.status_code,
|
||||
response.reason
|
||||
)
|
||||
return False, 'Captcha validation error'
|
||||
else:
|
||||
response = response.json()
|
||||
|
||||
error_codes = response.get('error-codes')
|
||||
if error_codes:
|
||||
LOGGER.error('recaptcha verification error: %s', error_codes)
|
||||
|
||||
result = response.get('success')
|
||||
if result:
|
||||
return True, ''
|
||||
|
||||
return False, 'Incorrect captcha, please try again'
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
from django.forms.widgets import CheckboxInput
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
__title__ = 'fobi.reusable.invisible_recaptcha.widgets'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2018 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('InvisibleRecaptchaWidget',)
|
||||
|
||||
|
||||
class InvisibleRecaptchaWidget(CheckboxInput):
|
||||
"""Invisible recaptcha widget."""
|
||||
|
||||
class Media(object):
|
||||
"""Media."""
|
||||
css = {
|
||||
'all': ('pretty.css',)
|
||||
}
|
||||
js = ('animations.js', 'actions.js')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
attrs = kwargs.get('attrs', {})
|
||||
attrs.update({'data-customforms': 'disabled'})
|
||||
kwargs.update({'attrs': attrs})
|
||||
super(InvisibleRecaptchaWidget, self).__init__(*args, **kwargs)
|
||||
|
||||
def render(self, *args, **kwargs):
|
||||
"""
|
||||
Returns this Widget rendered as HTML, as a Unicode string.
|
||||
"""
|
||||
html = super(InvisibleRecaptchaWidget, self).render(*args, **kwargs)
|
||||
invisible_recaptcha_html = """
|
||||
<script src="https://www.google.com/recaptcha/api.js" async defer>
|
||||
</script>
|
||||
<script>
|
||||
function g_recaptcha_onSubmit(token) {
|
||||
document.getElementById("fobi-form").submit();
|
||||
}
|
||||
</script>
|
||||
"""
|
||||
return html + mark_safe(invisible_recaptcha_html)
|
||||
Loading…
Reference in a new issue