Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Improve MathCaptchaForm #1

Merged
merged 1 commit into from

2 participants

Adrien Lemaire Justin Quick
Adrien Lemaire

please read the commit message

Adrien Lemaire Fandekasp Refined MathCaptchaForm. Using clean() is a bad idea can cause proble…
…ms with

other class using it as well. Here you clean only one field, so better call
clean_field. Solved a problem with django-registration, as I didn't want to call
MathCaptchaForm as mro first parent.

cleaned pep8 errors in forms.py as well
added .gitignore file
ac654e0
Justin Quick justquick merged commit 6d1b5f5 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2012
  1. Adrien Lemaire

    Refined MathCaptchaForm. Using clean() is a bad idea can cause proble…

    Fandekasp authored
    …ms with
    
    other class using it as well. Here you clean only one field, so better call
    clean_field. Solved a problem with django-registration, as I didn't want to call
    MathCaptchaForm as mro first parent.
    
    cleaned pep8 errors in forms.py as well
    added .gitignore file
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 14 deletions.
  1. +1 −0  .gitignore
  2. +18 −14 math_captcha/forms.py
1  .gitignore
View
@@ -0,0 +1 @@
+*.swp
32 math_captcha/forms.py
View
@@ -9,41 +9,46 @@ class NullWidget(forms.widgets.HiddenInput):
def render(self, *a, **kw):
return ''
+
def math_clean(form):
"""
Cleans a form, validating answer to math question in the process.
- The given ``form`` must be an instance of either ``MathCaptchaModelForm`` or ``MathCaptchaForm``.
- Answer keys are communicated in the ``math_captcha_question`` field which is evaluated to give the correct answer
+ The given ``form`` must be an instance of either ``MathCaptchaModelForm``
+ or ``MathCaptchaForm``.
+ Answer keys are communicated in the ``math_captcha_question`` field which
+ is evaluated to give the correct answer
after being validated against the ``SECRET_KEY``
"""
try:
value = form.cleaned_data['math_captcha_field']
- test_secret, question = decode(form.cleaned_data['math_captcha_question'])
+ test_secret, question = decode(
+ form.cleaned_data['math_captcha_question'])
assert len(test_secret) == 40 and question
except (TypeError, AssertionError):
# problem decoding, junky data
raise forms.ValidationError('Invalid token')
except KeyError:
return
-
+
if encode(question) != form.cleaned_data['math_captcha_question']:
# security problem, hack attempt
raise forms.ValidationError('Invalid token')
if eval(question) != value:
raise forms.ValidationError('Wrong answer, try again')
+
class MathCaptchaModelForm(forms.ModelForm):
"""
Subclass of ``django.forms.ModelForm`` which contains the math fields
Inherit this class in your forms to add math captcha support::
-
+
class MyForm(MathCaptchaModelForm):
field1 = forms.fields.CharField()
# ...
-
+
class Meta:
model = MyModel
-
+
"""
math_captcha_field = MathField(label=settings.QUESTION)
math_captcha_question = forms.fields.CharField(widget=NullWidget())
@@ -52,21 +57,20 @@ def clean(self):
super(MathCaptchaModelForm, self).clean()
math_clean(self)
return self.cleaned_data
-
+
+
class MathCaptchaForm(forms.Form):
"""
Subclass of ``django.forms.Form`` which contains the math fields.
Inherit this class in your forms to add math captcha support::
-
+
class MyForm(MathCaptchaForm):
field1 = forms.fields.CharField()
# ...
-
+
"""
- math_captcha_field = MathField(label=settings.QUESTION)
math_captcha_question = forms.fields.CharField(widget=NullWidget())
+ math_captcha_field = MathField(label=settings.QUESTION)
- def clean(self):
- super(MathCaptchaForm, self).clean()
+ def clean_math_captcha_field(self):
math_clean(self)
- return self.cleaned_data
Something went wrong with that request. Please try again.