Browse Source

[enh] Add anti bot feature

ljf 6 years ago
parent
commit
46201f61ed
2 changed files with 30 additions and 1 deletions
  1. 25 1
      coin/members/forms.py
  2. 5 0
      coin/static/css/local.css

+ 25 - 1
coin/members/forms.py

@@ -4,6 +4,7 @@ from __future__ import unicode_literals
 from django import forms
 from django.contrib.auth.forms import PasswordResetForm, ReadOnlyPasswordHashField
 from django.forms.utils import ErrorList
+from django.forms.forms import BoundField
 
 from coin.members.models import Member
 
@@ -11,6 +12,10 @@ from registration.forms import RegistrationForm
 
 
 class MemberRegistrationForm(RegistrationForm):
+    # Protect against robot
+    trap = forms.CharField(required=False, label='Trap',
+            widget=forms.TextInput(attrs={'style' : 'display:none'}),
+        help_text="Si vous êtes humain ne ignorez ce champ")
 
     def __init__(self, *args, **kwargs):
         super(MemberRegistrationForm, self).__init__(*args, **kwargs)
@@ -18,10 +23,29 @@ class MemberRegistrationForm(RegistrationForm):
         for fieldname in ['email', 'organization_name', 'password2']:
             self.fields[fieldname].help_text = None
 
+    def is_valid(self):
+         valid = super(MemberRegistrationForm,self).is_valid()
+         avoid_trap = not self.data['trap']
+         if valid and avoid_trap:
+             return True
+         else:
+             return False
+
+    def as_p(self):
+        """"
+        Hide captcha
+        """
+        def css_classes(self, extra_classes=None):
+            return 'captcha'
+
+        func_type = type(BoundField.css_classes)
+        self['trap'].css_classes = func_type(css_classes, self, BoundField)
+        return super(MemberRegistrationForm, self).as_p()
+
     class Meta:
         model = Member
         fields = ['type', 'first_name', 'last_name',
-                  'organization_name', 'email', 'username']
+                  'organization_name', 'email', 'username', 'trap']
 
 class MemberCreationForm(forms.ModelForm):
 

+ 5 - 0
coin/static/css/local.css

@@ -420,3 +420,8 @@ form .helptext {
 .list-filter {
 	text-align: right;
 }
+
+/* Registration */
+.captcha {
+    display:none;
+}