Mercurial > public > sg101
diff contact/forms.py @ 1172:b957e4829a03
Add reCAPTCHA to contact form
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 14 Apr 2018 13:53:05 -0500 |
parents | 6ac56115e0a8 |
children | a1a223ab0c8f |
line wrap: on
line diff
--- a/contact/forms.py Mon Jan 01 10:39:48 2018 -0600 +++ b/contact/forms.py Sat Apr 14 13:53:05 2018 -0500 @@ -1,12 +1,20 @@ """forms for the contact application""" +import logging + from django import forms from django.conf import settings from django.template.loader import render_to_string from django.contrib.sites.models import Site +import requests + +from core.functions import get_ip from core.functions import send_mail +logger = logging.getLogger(__name__) + + class ContactForm(forms.Form): """Form used to contact the website admins""" name=forms.CharField(label="Your Name") @@ -20,12 +28,34 @@ recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS] + def __init__(self, *args, **kwargs): + self.request = kwargs.pop('request', None) + super(ContactForm, self).__init__(*args, **kwargs) + def clean_honeypot(self): value = self.cleaned_data['honeypot'] if value: raise forms.ValidationError(self.fields['honeypot'].label) return value + def clean(self): + super(ContactForm, self).clean() + captcha_response = self.request.POST.get('g-recaptcha-response') + if not captcha_response: + raise forms.ValidationError('Missing reCAPTCHA response') + r = requests.post(settings.RECAPTCHA_URL, data={ + 'secret': settings.RECAPTCHA_SECRET_KEY, + 'response': captcha_response, + 'remoteip': get_ip(self.request), + }) + result = r.json() + logger.info("Contact Form captcha response: %s %s", + result.get('success', '<Missing>'), + result.get('error-codes', '<Missing>')) + success = result.get('success', False) + if not success: + raise forms.ValidationError('reCAPTCHA failure') + def save(self): # Send the feedback message email