Created
March 2, 2013 14:33
-
-
Save thisboyiscrazy/5071258 to your computer and use it in GitHub Desktop.
A Django Form you can inherite from to use areyouhuman.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
import ayah | |
ayah.configure( | |
settings.AREYOUHUMAN_PUBLISHER_KEY, | |
settings.AREYOUHUMAN_SCORING_KEY, | |
) | |
from django.forms import Field, ValidationError, Form | |
from django.forms.widgets import Widget | |
#Create a wdiget that renders the AreYouHuman html | |
class AreYouHumanWidget(Widget): | |
def render(self, name, value, attrs=None): | |
return ayah.get_publisher_html() | |
#AreYouHuman uses a test field (session_secret) for validation | |
class AreYouHumanField(Field): | |
#Use the AreYouHuman HTML | |
widget = AreYouHumanWidget | |
#Validate agianst AreYouHuman | |
def validate(self, value): | |
if not ayah.score_result(value): | |
raise ValidationError("You may not be human.") | |
# Because AreYouHuman does currently | |
# allow you to change the field name | |
# (session secret) we have to create a form | |
class AreYouHumanForm(Form): | |
session_secret = AreYouHumanField(label="Are You Human") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! I ran into a small issue: the return from the render method needs to be wrapped in mark_safe as in "return mark_safe(ayah.get_publisher_html())".