Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
1170:b213e4b333bb | 1172:b957e4829a03 |
---|---|
1 """forms for the contact application""" | 1 """forms for the contact application""" |
2 | |
3 import logging | |
2 | 4 |
3 from django import forms | 5 from django import forms |
4 from django.conf import settings | 6 from django.conf import settings |
5 from django.template.loader import render_to_string | 7 from django.template.loader import render_to_string |
6 from django.contrib.sites.models import Site | 8 from django.contrib.sites.models import Site |
9 import requests | |
10 | |
11 from core.functions import get_ip | |
7 from core.functions import send_mail | 12 from core.functions import send_mail |
13 | |
14 | |
15 logger = logging.getLogger(__name__) | |
8 | 16 |
9 | 17 |
10 class ContactForm(forms.Form): | 18 class ContactForm(forms.Form): |
11 """Form used to contact the website admins""" | 19 """Form used to contact the website admins""" |
12 name=forms.CharField(label="Your Name") | 20 name=forms.CharField(label="Your Name") |
18 widget=forms.Textarea(), | 26 widget=forms.Textarea(), |
19 max_length=3000) | 27 max_length=3000) |
20 | 28 |
21 recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS] | 29 recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS] |
22 | 30 |
31 def __init__(self, *args, **kwargs): | |
32 self.request = kwargs.pop('request', None) | |
33 super(ContactForm, self).__init__(*args, **kwargs) | |
34 | |
23 def clean_honeypot(self): | 35 def clean_honeypot(self): |
24 value = self.cleaned_data['honeypot'] | 36 value = self.cleaned_data['honeypot'] |
25 if value: | 37 if value: |
26 raise forms.ValidationError(self.fields['honeypot'].label) | 38 raise forms.ValidationError(self.fields['honeypot'].label) |
27 return value | 39 return value |
40 | |
41 def clean(self): | |
42 super(ContactForm, self).clean() | |
43 captcha_response = self.request.POST.get('g-recaptcha-response') | |
44 if not captcha_response: | |
45 raise forms.ValidationError('Missing reCAPTCHA response') | |
46 r = requests.post(settings.RECAPTCHA_URL, data={ | |
47 'secret': settings.RECAPTCHA_SECRET_KEY, | |
48 'response': captcha_response, | |
49 'remoteip': get_ip(self.request), | |
50 }) | |
51 result = r.json() | |
52 logger.info("Contact Form captcha response: %s %s", | |
53 result.get('success', '<Missing>'), | |
54 result.get('error-codes', '<Missing>')) | |
55 success = result.get('success', False) | |
56 if not success: | |
57 raise forms.ValidationError('reCAPTCHA failure') | |
28 | 58 |
29 def save(self): | 59 def save(self): |
30 # Send the feedback message email | 60 # Send the feedback message email |
31 | 61 |
32 site = Site.objects.get_current() | 62 site = Site.objects.get_current() |