testappproj/testapp/forms.py
changeset 4 4d5422e5a45d
parent 3 34d0c21e3352
--- a/testappproj/testapp/forms.py	Mon Jun 14 01:00:59 2010 +0530
+++ b/testappproj/testapp/forms.py	Mon Jun 14 22:02:14 2010 +0530
@@ -1,3 +1,4 @@
+from recaptcha.client import captcha
 from django import forms
 from registration.forms import RegistrationForm
 from django.utils.translation import ugettext_lazy as _
@@ -5,6 +6,9 @@
 from registration.models import RegistrationProfile
 #from google.appengine.ext.db import djangoforms
 import models
+from django.conf import settings
+from django.utils.safestring import mark_safe
+
 
 
 attrs_dict = { 'class': 'required' }
@@ -26,6 +30,47 @@
   )
 
 
+# class ReCaptcha(forms.Widget):
+#     input_type = None # Subclasses must define this.
+
+#     def render(self, name, value, attrs=None):
+#         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
+#         html = u"<script>var RecaptchaOptions = {theme : '%s'};</script>" % (
+#             final_attrs.get('theme', 'white'))
+#         html += captcha.displayhtml(settings.RECAPTCHA_PUBLIC)
+#         return mark_safe(html)
+
+#     def value_from_datadict(self, data, files, name):
+#         return {
+#             'recaptcha_challenge_field': data.get('recaptcha_challenge_field', None),
+#             'recaptcha_response_field': data.get('recaptcha_response_field', None),
+#         }
+
+# class ReCaptchaField(forms.FileField):
+#     widget = ReCaptcha
+#     default_error_messages = {
+#         'invalid-site-public-key': u"Invalid public key",
+#         'invalid-site-private-key': u"Invalid private key",
+#         'invalid-request-cookie': u"Invalid cookie",
+#         'incorrect-captcha-sol': u"Invalid entry, please try again.",
+#         'verify-params-incorrect': u"The parameters to verify were incorrect, make sure you are passing all the required parameters.",
+#         'invalid-referrer': u"Invalid referrer domain",
+#         'recaptcha-not-reachable': u"Could not contact reCAPTCHA server",
+#     }
+
+#     def clean(self, data, initial):
+#         if initial is None or initial == '':
+#             raise Exception("ReCaptchaField requires the client's IP be set to the initial value")
+#         ip = initial
+#         resp = captcha.submit(data.get("recaptcha_challenge_field", None),
+#                               data.get("recaptcha_response_field", None),
+#                               settings.RECAPTCHA_PRIVATE, ip)
+#         if not resp.is_valid:
+#             raise forms.ValidationError(self.default_error_messages.get(
+#                     resp.error_code, "Unknown error: %s" % (resp.error_code)))
+
+
+
 class ProblemForm(forms.Form):
   Description = forms.CharField(widget=forms.Textarea(attrs={'rows': '10', 'cols': '80'}),required=True,help_text="To be used as the question")
   Problem_type = forms.ChoiceField(choices=PROBLEM_CHOICES,required=True)
@@ -44,7 +89,8 @@
 
   Fullname = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
   Address =  forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
-  
+#  captcha = ReCaptchaField()
+
   class Meta:
         exclude = ('special_user','exam_done')