Guilherme Gondim avatar Guilherme Gondim committed 66fe727

add anti-spam contact form and take out phone input

Comments (0)

Files changed (4)

 from django import forms
 from django.utils.translation import ugettext_lazy as _
 
+from contato.widgets import AntiSpamInput
+
 
 class BaseContactForm(forms.Form):
     name = forms.CharField(label=_('Name'), max_length=50)
         widget=forms.Textarea()
     )
 
+
 class ContactForm(BaseContactForm):
-    phone_number = forms.CharField(
-        label=_('Phone number'),
-        required=False,
-    )
+    pass
+
+
+class AntiSpamContactForm(ContactForm):
+    # fake email input to prevent spam
+    email_ = forms.CharField(required=False, widget=AntiSpamInput)
+
+    def clean_email_(self):
+        data = self.cleaned_data.get('email_')
+        if data:
+            msg = _("Field to detect spam bots. Don't provide any data here.")
+            raise forms.ValidationError(msg)
+        return data

contato/templates/contato/body.txt

-{% load i18n %}
 {{ message }}
-
-{% if phone_number %}
-{% blocktrans %}Phone number: {{ phone_number }}{% endblocktrans %}
-{% endif %}
 from django.views.generic import FormView, TemplateView
 
 
-from contato.forms import ContactForm
+from contato.forms import AntiSpamContactForm, ContactForm
 
 
 class SentView(TemplateView):
 
     def get_body(self, form_data):
         message = form_data.get('message')
-        phone_number = form_data.get('phone_number', None)
         template = self.get_body_template()
-        context = dict(message=message, phone_number=phone_number)
+        context = dict(message=message)
         string = render_to_string(template, context)
         return string.lstrip()
+
+
+class AntiSpamContactView(ContactView):
+    form_class = AntiSpamContactForm

contato/widgets.py

+from django import forms
+from django.utils.safestring import mark_safe
+
+
+class AntiSpamInput(forms.TextInput):
+
+    def render(self, name, value, attrs=None):
+        html = '<style type="text/css" scoped>#id_email_ {display:none;}</style>'
+        html += super(AntiSpamInput, self).render(name, value, attrs)
+        return mark_safe(html)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.