gremmie@1: """forms for the contact application""" gremmie@1: bgneal@1172: import logging bgneal@1172: gremmie@1: from django import forms gremmie@1: from django.conf import settings gremmie@1: from django.template.loader import render_to_string gremmie@1: from django.contrib.sites.models import Site bgneal@1172: import requests bgneal@1172: bgneal@1172: from core.functions import get_ip gremmie@1: from core.functions import send_mail gremmie@1: gremmie@1: bgneal@1172: logger = logging.getLogger(__name__) bgneal@1172: bgneal@1172: gremmie@1: class ContactForm(forms.Form): bgneal@819: """Form used to contact the website admins""" bgneal@1070: name=forms.CharField(label="Your Name") bgneal@1070: email=forms.EmailField(label="Your Email") bgneal@1070: subject=forms.CharField() bgneal@819: honeypot=forms.CharField(max_length=64, required=False, bgneal@819: label='If you enter anything in this field your message will be treated as spam') bgneal@819: message=forms.CharField(label="Your Message", bgneal@1070: widget=forms.Textarea(), bgneal@819: max_length=3000) gremmie@1: bgneal@819: recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS] gremmie@1: bgneal@1172: def __init__(self, *args, **kwargs): bgneal@1172: self.request = kwargs.pop('request', None) bgneal@1172: super(ContactForm, self).__init__(*args, **kwargs) bgneal@1172: bgneal@819: def clean_honeypot(self): bgneal@819: value = self.cleaned_data['honeypot'] bgneal@819: if value: bgneal@819: raise forms.ValidationError(self.fields['honeypot'].label) bgneal@819: return value gremmie@1: bgneal@1172: def clean(self): bgneal@1172: super(ContactForm, self).clean() bgneal@1172: captcha_response = self.request.POST.get('g-recaptcha-response') bgneal@1172: if not captcha_response: bgneal@1172: raise forms.ValidationError('Missing reCAPTCHA response') bgneal@1172: r = requests.post(settings.RECAPTCHA_URL, data={ bgneal@1172: 'secret': settings.RECAPTCHA_SECRET_KEY, bgneal@1172: 'response': captcha_response, bgneal@1172: 'remoteip': get_ip(self.request), bgneal@1172: }) bgneal@1172: result = r.json() bgneal@1172: logger.info("Contact Form captcha response: %s %s", bgneal@1172: result.get('success', ''), bgneal@1172: result.get('error-codes', '')) bgneal@1172: success = result.get('success', False) bgneal@1172: if not success: bgneal@1172: raise forms.ValidationError('reCAPTCHA failure') bgneal@1172: bgneal@819: def save(self): bgneal@819: # Send the feedback message email gremmie@1: bgneal@819: site = Site.objects.get_current() gremmie@1: bgneal@819: msg = render_to_string('contact/contact_email.txt', { bgneal@819: 'site_name': site.name, bgneal@819: 'user_name': self.cleaned_data['name'], bgneal@819: 'user_email': self.cleaned_data['email'], bgneal@892: 'subject': self.cleaned_data['subject'], bgneal@819: 'message': self.cleaned_data['message'], bgneal@819: }) gremmie@1: bgneal@819: subject = site.name + ' Feedback: ' + self.cleaned_data['subject'] bgneal@892: from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain bgneal@892: send_mail(subject, msg, from_email, self.recipient_list, bgneal@892: reply_to=self.cleaned_data['email'])