annotate contact/forms.py @ 1190:ce5a5c9cd9d8

Tweaks to MP3 comp tool
author Brian Neal <bgneal@gmail.com>
date Sun, 13 Mar 2022 13:09:37 -0500
parents a1a223ab0c8f
children
rev   line source
gremmie@1 1 """forms for the contact application"""
gremmie@1 2
bgneal@1172 3 import logging
bgneal@1172 4
gremmie@1 5 from django import forms
gremmie@1 6 from django.conf import settings
gremmie@1 7 from django.template.loader import render_to_string
gremmie@1 8 from django.contrib.sites.models import Site
bgneal@1172 9 import requests
bgneal@1172 10
bgneal@1172 11 from core.functions import get_ip
gremmie@1 12 from core.functions import send_mail
gremmie@1 13
gremmie@1 14
bgneal@1172 15 logger = logging.getLogger(__name__)
bgneal@1172 16
bgneal@1172 17
gremmie@1 18 class ContactForm(forms.Form):
bgneal@819 19 """Form used to contact the website admins"""
bgneal@1070 20 name=forms.CharField(label="Your Name")
bgneal@1070 21 email=forms.EmailField(label="Your Email")
bgneal@1070 22 subject=forms.CharField()
bgneal@819 23 honeypot=forms.CharField(max_length=64, required=False,
bgneal@819 24 label='If you enter anything in this field your message will be treated as spam')
bgneal@819 25 message=forms.CharField(label="Your Message",
bgneal@1070 26 widget=forms.Textarea(),
bgneal@819 27 max_length=3000)
gremmie@1 28
bgneal@819 29 recipient_list = [mail_tuple[1] for mail_tuple in settings.MANAGERS]
gremmie@1 30
bgneal@1172 31 def __init__(self, *args, **kwargs):
bgneal@1172 32 self.request = kwargs.pop('request', None)
bgneal@1172 33 super(ContactForm, self).__init__(*args, **kwargs)
bgneal@1172 34
bgneal@819 35 def clean_honeypot(self):
bgneal@819 36 value = self.cleaned_data['honeypot']
bgneal@819 37 if value:
bgneal@819 38 raise forms.ValidationError(self.fields['honeypot'].label)
bgneal@819 39 return value
gremmie@1 40
bgneal@1172 41 def clean(self):
bgneal@1172 42 super(ContactForm, self).clean()
bgneal@1172 43 captcha_response = self.request.POST.get('g-recaptcha-response')
bgneal@1172 44 if not captcha_response:
bgneal@1173 45 raise forms.ValidationError("Please verify you aren't a robot")
bgneal@1172 46 r = requests.post(settings.RECAPTCHA_URL, data={
bgneal@1172 47 'secret': settings.RECAPTCHA_SECRET_KEY,
bgneal@1172 48 'response': captcha_response,
bgneal@1172 49 'remoteip': get_ip(self.request),
bgneal@1172 50 })
bgneal@1172 51 result = r.json()
bgneal@1172 52 logger.info("Contact Form captcha response: %s %s",
bgneal@1172 53 result.get('success', '<Missing>'),
bgneal@1172 54 result.get('error-codes', '<Missing>'))
bgneal@1172 55 success = result.get('success', False)
bgneal@1172 56 if not success:
bgneal@1172 57 raise forms.ValidationError('reCAPTCHA failure')
bgneal@1172 58
bgneal@819 59 def save(self):
bgneal@819 60 # Send the feedback message email
gremmie@1 61
bgneal@819 62 site = Site.objects.get_current()
gremmie@1 63
bgneal@819 64 msg = render_to_string('contact/contact_email.txt', {
bgneal@819 65 'site_name': site.name,
bgneal@819 66 'user_name': self.cleaned_data['name'],
bgneal@819 67 'user_email': self.cleaned_data['email'],
bgneal@892 68 'subject': self.cleaned_data['subject'],
bgneal@819 69 'message': self.cleaned_data['message'],
bgneal@819 70 })
gremmie@1 71
bgneal@819 72 subject = site.name + ' Feedback: ' + self.cleaned_data['subject']
bgneal@892 73 from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain
bgneal@892 74 send_mail(subject, msg, from_email, self.recipient_list,
bgneal@892 75 reply_to=self.cleaned_data['email'])