captcha.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from ....admin.forms import YesNoSwitch
  4. from .base import ChangeSettingsForm
  5. class ChangeCaptchaSettingsForm(ChangeSettingsForm):
  6. settings = [
  7. "captcha_type",
  8. "recaptcha_site_key",
  9. "recaptcha_secret_key",
  10. "qa_question",
  11. "qa_help_text",
  12. "qa_answers",
  13. "enable_stop_forum_spam",
  14. "stop_forum_spam_confidence",
  15. ]
  16. captcha_type = forms.ChoiceField(
  17. label=_("Enable CAPTCHA"),
  18. choices=[
  19. ("no", _("No CAPTCHA")),
  20. ("re", _("reCaptcha")),
  21. ("qa", _("Question and answer")),
  22. ],
  23. widget=forms.RadioSelect(),
  24. )
  25. recaptcha_site_key = forms.CharField(
  26. label=_("Site key"), max_length=100, required=False
  27. )
  28. recaptcha_secret_key = forms.CharField(
  29. label=_("Secret key"), max_length=100, required=False
  30. )
  31. qa_question = forms.CharField(
  32. label=_("Test question"), max_length=100, required=False
  33. )
  34. qa_help_text = forms.CharField(
  35. label=_("Question help text"), max_length=250, required=False
  36. )
  37. qa_answers = forms.CharField(
  38. label=_("Valid answers"),
  39. help_text=_("Enter each answer in new line. Answers are case-insensitive."),
  40. widget=forms.Textarea({"rows": 4}),
  41. max_length=250,
  42. required=False,
  43. )
  44. enable_stop_forum_spam = YesNoSwitch(
  45. label=_("Validate new registrations against SFS database"),
  46. help_text=_(
  47. "Turning this option on will result in Misago validating new user's e-mail "
  48. "and IP address against SFS database."
  49. ),
  50. )
  51. stop_forum_spam_confidence = forms.IntegerField(
  52. label=_("Minimum SFS confidence required"),
  53. help_text=_(
  54. "SFS compares user e-mail and IP address with database of known spammers "
  55. "and assigns the confidence score in range of 0 to 100 that user is a "
  56. "spammer themselves. If this score is equal or higher than specified, "
  57. "Misago will block user from registering and ban their IP address "
  58. "for 24 hours."
  59. ),
  60. min_value=0,
  61. max_value=100,
  62. )
  63. def clean(self):
  64. cleaned_data = super().clean()
  65. if cleaned_data.get("captcha_type") == "re":
  66. if not cleaned_data.get("recaptcha_site_key"):
  67. self.add_error(
  68. "recaptcha_site_key",
  69. _(
  70. "You need to enter site key if "
  71. "selected CAPTCHA type is reCaptcha."
  72. ),
  73. )
  74. if not cleaned_data.get("recaptcha_secret_key"):
  75. self.add_error(
  76. "recaptcha_secret_key",
  77. _(
  78. "You need to enter secret key if "
  79. "selected CAPTCHA type is reCaptcha."
  80. ),
  81. )
  82. if cleaned_data.get("captcha_type") == "qa":
  83. if not cleaned_data.get("qa_question"):
  84. self.add_error(
  85. "qa_question",
  86. _("You need to set question if selected CAPTCHA type is Q&A."),
  87. )
  88. if not cleaned_data.get("qa_answers"):
  89. self.add_error(
  90. "qa_answers",
  91. _(
  92. "You need to set question answers if "
  93. "selected CAPTCHA type is Q&A."
  94. ),
  95. )
  96. return cleaned_data