1
0

views.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from django.http import HttpResponse
  4. from django.template import engines
  5. from captcha.fields import CaptchaField
  6. TEST_TEMPLATE = r"""
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  8. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  9. <html>
  10. <head>
  11. <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  12. <title>captcha test</title>
  13. </head>
  14. <body>
  15. {% if passed %}
  16. <p style="color:green">Form validated</p>
  17. {% endif %}
  18. {% if form.errors %}
  19. {{form.errors}}
  20. {% endif %}
  21. <form action="{% url 'captcha-test' %}" method="post">
  22. {{form.as_p}}
  23. <p><input type="submit" value="Continue &rarr;"></p>
  24. </form>
  25. </body>
  26. </html>
  27. """
  28. def _get_template(template_string):
  29. return engines["django"].from_string(template_string)
  30. def _test(request, form_class):
  31. passed = False
  32. if request.POST:
  33. form = form_class(request.POST)
  34. if form.is_valid():
  35. passed = True
  36. else:
  37. form = form_class()
  38. t = _get_template(TEST_TEMPLATE)
  39. return HttpResponse(
  40. t.render(context=dict(passed=passed, form=form), request=request)
  41. )
  42. def test(request):
  43. class CaptchaTestForm(forms.Form):
  44. subject = forms.CharField(max_length=100)
  45. sender = forms.EmailField()
  46. captcha = CaptchaField(help_text="asdasd")
  47. return _test(request, CaptchaTestForm)
  48. def test_model_form(request):
  49. class CaptchaTestModelForm(forms.ModelForm):
  50. subject = forms.CharField(max_length=100)
  51. sender = forms.EmailField()
  52. captcha = CaptchaField(help_text="asdasd")
  53. class Meta:
  54. model = User
  55. fields = ("subject", "sender", "captcha")
  56. return _test(request, CaptchaTestModelForm)
  57. def test_custom_generator(request):
  58. class CaptchaTestModelForm(forms.ModelForm):
  59. subject = forms.CharField(max_length=100)
  60. sender = forms.EmailField()
  61. captcha = CaptchaField(generator=lambda: ("111111", "111111"))
  62. class Meta:
  63. model = User
  64. fields = ("subject", "sender", "captcha")
  65. return _test(request, CaptchaTestModelForm)
  66. def test_custom_error_message(request):
  67. class CaptchaTestErrorMessageForm(forms.Form):
  68. captcha = CaptchaField(
  69. help_text="asdasd", error_messages=dict(invalid="TEST CUSTOM ERROR MESSAGE")
  70. )
  71. return _test(request, CaptchaTestErrorMessageForm)
  72. def test_per_form_format(request):
  73. class CaptchaTestFormatForm(forms.Form):
  74. captcha = CaptchaField(
  75. help_text="asdasd",
  76. error_messages=dict(invalid="TEST CUSTOM ERROR MESSAGE"),
  77. output_format=(
  78. "%(image)s testPerFieldCustomFormatString "
  79. "%(hidden_field)s %(text_field)s"
  80. ),
  81. )
  82. return _test(request, CaptchaTestFormatForm)
  83. def test_non_required(request):
  84. class CaptchaTestForm(forms.Form):
  85. sender = forms.EmailField()
  86. subject = forms.CharField(max_length=100)
  87. captcha = CaptchaField(help_text="asdasd", required=False)
  88. return _test(request, CaptchaTestForm)
  89. def test_id_prefix(request):
  90. class CaptchaTestForm(forms.Form):
  91. sender = forms.EmailField()
  92. subject = forms.CharField(max_length=100)
  93. captcha1 = CaptchaField(id_prefix="form1")
  94. captcha2 = CaptchaField(id_prefix="form2")
  95. return _test(request, CaptchaTestForm)