From 7072fb33b921624e048ea4a92aa63402927358e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Tue, 23 Nov 2021 19:20:38 +0100 Subject: [PATCH] Rework how captcha is handled in the form. Prior to this commit, things were done by activating or deactivating a "captcha" property on the class on-the-fly, which caused side-effects. This is now using subclasses, which makes the code simpler to understand, and less prone to side-effects. Thanks @zorun for the idea. --- ihatemoney/api/common.py | 4 ++-- ihatemoney/forms.py | 17 ++++++----------- ihatemoney/web.py | 4 ++-- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/ihatemoney/api/common.py b/ihatemoney/api/common.py index f9f20165..923a5391 100644 --- a/ihatemoney/api/common.py +++ b/ihatemoney/api/common.py @@ -50,7 +50,7 @@ def need_auth(f): class ProjectsHandler(Resource): def post(self): - form = ProjectForm(meta={"csrf": False}, bypass_captcha=True) + form = ProjectForm(meta={"csrf": False}) if form.validate() and current_app.config.get("ALLOW_PUBLIC_PROJECT_CREATION"): project = form.save() db.session.add(project) @@ -71,7 +71,7 @@ class ProjectHandler(Resource): return "DELETED" def put(self, project): - form = EditProjectForm(id=project.id, meta={"csrf": False}, bypass_captcha=True) + form = EditProjectForm(id=project.id, meta={"csrf": False}) if form.validate() and current_app.config.get("ALLOW_PUBLIC_PROJECT_CREATION"): form.update(project) db.session.commit() diff --git a/ihatemoney/forms.py b/ihatemoney/forms.py index f2de4462..4e241c86 100644 --- a/ihatemoney/forms.py +++ b/ihatemoney/forms.py @@ -197,10 +197,6 @@ class ProjectForm(EditProjectForm): password = PasswordField(_("Private code"), validators=[DataRequired()]) submit = SubmitField(_("Create the project")) - def __init__(self, *args, **kwargs): - self.bypass_captcha = kwargs.get("bypass_captcha", False) - super().__init__(*args, **kwargs) - def save(self): """Create a new project with the information given by this form. @@ -232,15 +228,14 @@ class ProjectForm(EditProjectForm): ) raise ValidationError(Markup(message)) - @classmethod - def enable_captcha(cls): - captchaField = StringField( - _("Which is a real currency: Euro or Petro dollar?"), - ) - setattr(cls, "captcha", captchaField) + +class ProjectFormWithCaptcha(ProjectForm): + captcha = StringField( + _("Which is a real currency: Euro or Petro dollar?"), + ) def validate_captcha(form, field): - if not field.data.lower() == _("euro") and not form.bypass_captcha: + if not field.data.lower() == _("euro"): message = _("Please, validate the captcha to proceed.") raise ValidationError(Markup(message)) diff --git a/ihatemoney/web.py b/ihatemoney/web.py index 2b365c1f..9188a531 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -47,6 +47,7 @@ from ihatemoney.forms import ( MemberForm, PasswordReminder, ProjectForm, + ProjectFormWithCaptcha, ResetPasswordForm, UploadForm, get_billform_for, @@ -263,8 +264,7 @@ def authenticate(project_id=None): def get_project_form(): if current_app.config.get("ENABLE_CAPTCHA", False): - ProjectForm.enable_captcha() - + return ProjectFormWithCaptcha() return ProjectForm()