Some small cleanups (#976)

Co-authored-by: Glandos <bugs-github@antipoul.fr>
This commit is contained in:
Alexis Metaireau 2022-01-30 15:26:22 +01:00 committed by GitHub
parent ed6676485a
commit 7c3ced06f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 24 deletions

View file

@ -162,8 +162,8 @@ class EditProjectForm(FlaskForm):
else: else:
return LoggingMode.ENABLED return LoggingMode.ENABLED
def validate_default_currency(form, field): def validate_default_currency(self, field):
project = Project.query.get(form.id.data) project = Project.query.get(self.id.data)
if ( if (
project is not None project is not None
and field.data == CurrencyConverter.no_currency and field.data == CurrencyConverter.no_currency
@ -234,13 +234,13 @@ class ProjectForm(EditProjectForm):
) )
return project return project
def validate_id(form, field): def validate_id(self, field):
form.id.data = slugify(field.data) self.id.data = slugify(field.data)
if (form.id.data == "dashboard") or Project.query.get(form.id.data): if (self.id.data == "dashboard") or Project.query.get(self.id.data):
message = _( message = _(
'A project with this identifier ("%(project)s") already exists. ' 'A project with this identifier ("%(project)s") already exists. '
"Please choose a new identifier", "Please choose a new identifier",
project=form.id.data, project=self.id.data,
) )
raise ValidationError(Markup(message)) raise ValidationError(Markup(message))
@ -250,7 +250,7 @@ class ProjectFormWithCaptcha(ProjectForm):
_("Which is a real currency: Euro or Petro dollar?"), default="" _("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"): if not field.data.lower() == _("euro"):
message = _("Please, validate the captcha to proceed.") message = _("Please, validate the captcha to proceed.")
raise ValidationError(Markup(message)) raise ValidationError(Markup(message))
@ -277,11 +277,11 @@ class DestructiveActionProjectForm(FlaskForm):
self.id = SimpleNamespace(data=kwargs.pop("id", "")) self.id = SimpleNamespace(data=kwargs.pop("id", ""))
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def validate_password(form, field): def validate_password(self, field):
project = Project.query.get(form.id.data) project = Project.query.get(self.id.data)
if project is None: if project is None:
raise ValidationError(_("Unknown error")) 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.")) raise ValidationError(_("Invalid private code."))
@ -300,7 +300,7 @@ class PasswordReminder(FlaskForm):
id = StringField(_("Project identifier"), validators=[DataRequired()]) id = StringField(_("Project identifier"), validators=[DataRequired()])
submit = SubmitField(_("Send me the code by email")) 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): if not Project.query.get(field.data):
raise ValidationError(_("This project does not exists")) raise ValidationError(_("This project does not exists"))
@ -398,14 +398,14 @@ class MemberForm(FlaskForm):
self.project = project self.project = project
self.edit = edit self.edit = edit
def validate_name(form, field): def validate_name(self, field):
if field.data == form.name.default: if field.data == self.name.default:
raise ValidationError(_("The participant name is invalid")) raise ValidationError(_("The participant name is invalid"))
if ( if (
not form.edit not self.edit
and Person.query.filter( and Person.query.filter(
Person.name == field.data, Person.name == field.data,
Person.project == form.project, Person.project == self.project,
Person.activated, Person.activated,
).all() ).all()
): # NOQA ): # NOQA
@ -428,8 +428,8 @@ class InviteForm(FlaskForm):
emails = StringField(_("People to notify"), render_kw={"class": "tag"}) emails = StringField(_("People to notify"), render_kw={"class": "tag"})
submit = SubmitField(_("Send invites")) submit = SubmitField(_("Send invites"))
def validate_emails(form, field): def validate_emails(self, field):
for email in [email.strip() for email in form.emails.data.split(",")]: for email in [email.strip() for email in self.emails.data.split(",")]:
try: try:
email_validator.validate_email(email) email_validator.validate_email(email)
except email_validator.EmailNotValidError: except email_validator.EmailNotValidError:

View file

@ -62,7 +62,7 @@ def generate_config(config_file):
random.SystemRandom().choice( random.SystemRandom().choice(
"abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)" "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)"
) )
for i in range(50) for _ in range(50)
] ]
) )

View file

@ -42,8 +42,7 @@
id="authentication-form" id="authentication-form"
class="form-horizontal" class="form-horizontal"
action="{{ url_for('.authenticate') }}" action="{{ url_for('.authenticate') }}"
method="post" method="post">
>
<fieldset class="form-group"> <fieldset class="form-group">
<legend></legend> <legend></legend>
{{ forms.authenticate(auth_form, home=True) }} {{ forms.authenticate(auth_form, home=True) }}

View file

@ -12,11 +12,12 @@ import socket
from babel import Locale from babel import Locale
from babel.numbers import get_currency_name, get_currency_symbol 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 _ from flask_babel import get_locale, lazy_gettext as _
import jinja2 import jinja2
from markupsafe import Markup from markupsafe import Markup, escape
from werkzeug.routing import HTTPException, RoutingException from werkzeug.exceptions import HTTPException
from werkzeug.routing import RoutingException
def slugify(value): def slugify(value):
@ -329,7 +330,7 @@ def em_surround(string, regex_escape=False):
string = escape(string) string = escape(string)
if regex_escape: if regex_escape:
return fr'<em class="font-italic">{string}<\/em>' return rf'<em class="font-italic">{string}<\/em>'
else: else:
return f'<em class="font-italic">{string}</em>' return f'<em class="font-italic">{string}</em>'