From 7c3ced06f8e5f076c8cc009b780b71434334712a Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Sun, 30 Jan 2022 15:26:22 +0100 Subject: [PATCH] Some small cleanups (#976) Co-authored-by: Glandos --- ihatemoney/forms.py | 34 +++++++++++++++++----------------- ihatemoney/manage.py | 2 +- ihatemoney/templates/home.html | 3 +-- ihatemoney/utils.py | 9 +++++---- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/ihatemoney/forms.py b/ihatemoney/forms.py index bbdef334e..2a7ba24ff 100644 --- a/ihatemoney/forms.py +++ b/ihatemoney/forms.py @@ -162,8 +162,8 @@ def logging_preference(self): else: return LoggingMode.ENABLED - def validate_default_currency(form, field): - project = Project.query.get(form.id.data) + def validate_default_currency(self, field): + project = Project.query.get(self.id.data) if ( project is not None and field.data == CurrencyConverter.no_currency @@ -234,13 +234,13 @@ def save(self): ) return project - def validate_id(form, field): - form.id.data = slugify(field.data) - if (form.id.data == "dashboard") or Project.query.get(form.id.data): + def validate_id(self, field): + self.id.data = slugify(field.data) + if (self.id.data == "dashboard") or Project.query.get(self.id.data): message = _( 'A project with this identifier ("%(project)s") already exists. ' "Please choose a new identifier", - project=form.id.data, + project=self.id.data, ) raise ValidationError(Markup(message)) @@ -250,7 +250,7 @@ class ProjectFormWithCaptcha(ProjectForm): _("Which is a real currency: Euro or Petro dollar?"), default="" ) - def validate_captcha(form, field): + def validate_captcha(self, field): if not field.data.lower() == _("euro"): message = _("Please, validate the captcha to proceed.") raise ValidationError(Markup(message)) @@ -277,11 +277,11 @@ def __init__(self, *args, **kwargs): self.id = SimpleNamespace(data=kwargs.pop("id", "")) super().__init__(*args, **kwargs) - def validate_password(form, field): - project = Project.query.get(form.id.data) + def validate_password(self, field): + project = Project.query.get(self.id.data) if project is None: raise ValidationError(_("Unknown error")) - if not check_password_hash(project.password, form.password.data): + if not check_password_hash(project.password, self.password.data): raise ValidationError(_("Invalid private code.")) @@ -300,7 +300,7 @@ class PasswordReminder(FlaskForm): id = StringField(_("Project identifier"), validators=[DataRequired()]) submit = SubmitField(_("Send me the code by email")) - def validate_id(form, field): + def validate_id(self, field): if not Project.query.get(field.data): raise ValidationError(_("This project does not exists")) @@ -398,14 +398,14 @@ def __init__(self, project, edit=False, *args, **kwargs): self.project = project self.edit = edit - def validate_name(form, field): - if field.data == form.name.default: + def validate_name(self, field): + if field.data == self.name.default: raise ValidationError(_("The participant name is invalid")) if ( - not form.edit + not self.edit and Person.query.filter( Person.name == field.data, - Person.project == form.project, + Person.project == self.project, Person.activated, ).all() ): # NOQA @@ -428,8 +428,8 @@ class InviteForm(FlaskForm): emails = StringField(_("People to notify"), render_kw={"class": "tag"}) submit = SubmitField(_("Send invites")) - def validate_emails(form, field): - for email in [email.strip() for email in form.emails.data.split(",")]: + def validate_emails(self, field): + for email in [email.strip() for email in self.emails.data.split(",")]: try: email_validator.validate_email(email) except email_validator.EmailNotValidError: diff --git a/ihatemoney/manage.py b/ihatemoney/manage.py index 805a07f3c..ad822f4d3 100755 --- a/ihatemoney/manage.py +++ b/ihatemoney/manage.py @@ -62,7 +62,7 @@ def gen_secret_key(): random.SystemRandom().choice( "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)" ) - for i in range(50) + for _ in range(50) ] ) diff --git a/ihatemoney/templates/home.html b/ihatemoney/templates/home.html index de88ea73e..d1d14cf04 100644 --- a/ihatemoney/templates/home.html +++ b/ihatemoney/templates/home.html @@ -42,8 +42,7 @@

{{ _("Manage your shared
expenses, easily") }}

id="authentication-form" class="form-horizontal" action="{{ url_for('.authenticate') }}" - method="post" - > + method="post">
{{ forms.authenticate(auth_form, home=True) }} diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py index 95ceaf25e..d06020987 100644 --- a/ihatemoney/utils.py +++ b/ihatemoney/utils.py @@ -12,11 +12,12 @@ from babel import Locale from babel.numbers import get_currency_name, get_currency_symbol -from flask import current_app, escape, redirect, render_template +from flask import current_app, redirect, render_template from flask_babel import get_locale, lazy_gettext as _ import jinja2 -from markupsafe import Markup -from werkzeug.routing import HTTPException, RoutingException +from markupsafe import Markup, escape +from werkzeug.exceptions import HTTPException +from werkzeug.routing import RoutingException def slugify(value): @@ -329,7 +330,7 @@ def em_surround(string, regex_escape=False): string = escape(string) if regex_escape: - return fr'{string}<\/em>' + return rf'{string}<\/em>' else: return f'{string}'