from django import forms from django.utils.translation import gettext_lazy as _ from .base import ChangeSettingsForm class ChangeCaptchaSettingsForm(ChangeSettingsForm): settings = [ "captcha_type", "recaptcha_site_key", "recaptcha_secret_key", "qa_question", "qa_help_text", "qa_answers", ] captcha_type = forms.ChoiceField( label=_("Enable CAPTCHA"), choices=[ ("no", _("No CAPTCHA")), ("re", _("reCaptcha")), ("qa", _("Question and answer")), ], widget=forms.RadioSelect(), ) recaptcha_site_key = forms.CharField( label=_("Site key"), max_length=100, required=False ) recaptcha_secret_key = forms.CharField( label=_("Secret key"), max_length=100, required=False ) qa_question = forms.CharField( label=_("Test question"), max_length=100, required=False ) qa_help_text = forms.CharField( label=_("Question help text"), max_length=250, required=False ) qa_answers = forms.CharField( label=_("Valid answers"), help_text=_("Enter each answer in new line. Answers are case-insensitive."), widget=forms.Textarea({"rows": 4}), max_length=250, required=False, ) def clean(self): cleaned_data = super().clean() if cleaned_data.get("captcha_type") == "re": if not cleaned_data.get("recaptcha_site_key"): self.add_error( "recaptcha_site_key", _( "You need to enter site key if " "selected CAPTCHA type is reCaptcha." ), ) if not cleaned_data.get("recaptcha_secret_key"): self.add_error( "recaptcha_secret_key", _( "You need to enter secret key if " "selected CAPTCHA type is reCaptcha." ), ) if cleaned_data.get("captcha_type") == "qa": if not cleaned_data.get("qa_question"): self.add_error( "qa_question", _("You need to set question if selected CAPTCHA type is Q&A."), ) if not cleaned_data.get("qa_answers"): self.add_error( "qa_answers", _( "You need to set question answers if " "selected CAPTCHA type is Q&A." ), ) return cleaned_data