2019-02-04 17:02:25 +01:00
|
|
|
from flask_login import current_user
|
2019-02-03 16:52:42 +01:00
|
|
|
from wtforms.validators import ValidationError
|
2019-06-07 01:44:04 +02:00
|
|
|
from app.models.users import Member
|
2019-06-06 01:59:49 +02:00
|
|
|
from app.utils.valid_name import valid_name
|
|
|
|
from app.utils.unicode_names import normalize
|
|
|
|
from config import V5Config
|
2019-02-03 16:52:42 +01:00
|
|
|
|
2019-06-05 11:35:54 +02:00
|
|
|
|
2019-03-30 22:37:57 +01:00
|
|
|
def name_valid(form, name):
|
2019-06-06 01:59:49 +02:00
|
|
|
valid = valid_name(name.data)
|
|
|
|
default = "Nom d'utilisateur invalide (erreur interne)"
|
|
|
|
msg = {
|
|
|
|
"too-short":
|
|
|
|
"Le nom d'utilisateur doit faire au moins "
|
|
|
|
f"{V5Config.USER_NAME_MINLEN} caractères.",
|
|
|
|
"too-long":
|
|
|
|
"Le nom d'utilisateur doit faire au plus "
|
|
|
|
f"{V5Config.USER_NAME_MAXLEN} caractères.",
|
|
|
|
"cant-normalize":
|
|
|
|
"Ce nom d'utilisateur contient des caractères interdits. Les "
|
|
|
|
"caractères autorisés sont les lettres, lettres accentuées, "
|
|
|
|
'chiffres ainsi que "-" (tiret), "." (point), "~" (tilde) et '
|
|
|
|
'"_" (underscore).',
|
|
|
|
"no-letter":
|
|
|
|
"Le nom d'utilisateur doit contenir au moins une lettre.",
|
|
|
|
"forbidden":
|
|
|
|
"Ce nom d'utilisateur est interdit."
|
|
|
|
}
|
|
|
|
if valid is not True:
|
|
|
|
err = ' '.join(msg.get(code, default) for code in valid)
|
|
|
|
raise ValidationError(err)
|
2019-03-30 22:37:57 +01:00
|
|
|
|
2019-06-05 11:35:54 +02:00
|
|
|
|
2019-03-30 22:37:57 +01:00
|
|
|
def name_available(form, name):
|
2019-06-06 01:59:49 +02:00
|
|
|
# If the name is invalid, name_valid() will return a meaningful message
|
|
|
|
try:
|
|
|
|
norm = normalize(name.data)
|
|
|
|
except ValueError:
|
|
|
|
return
|
|
|
|
|
|
|
|
member = Member.query.filter_by(norm=norm).first()
|
2019-02-03 16:52:42 +01:00
|
|
|
if member is not None:
|
2019-06-06 01:59:49 +02:00
|
|
|
raise ValidationError("Ce nom d'utilisateur est indisponible.")
|
2019-02-03 16:52:42 +01:00
|
|
|
|
2019-06-05 11:35:54 +02:00
|
|
|
|
2019-02-03 16:52:42 +01:00
|
|
|
def email(form, email):
|
|
|
|
member = Member.query.filter_by(email=email.data).first()
|
|
|
|
if member is not None:
|
|
|
|
raise ValidationError('Adresse email déjà utilisée.')
|
|
|
|
|
2019-06-05 11:35:54 +02:00
|
|
|
|
2019-02-03 16:52:42 +01:00
|
|
|
def password(form, password):
|
|
|
|
if len(password.data) != 0 and len(password.data) < 10:
|
2019-02-04 17:02:25 +01:00
|
|
|
raise ValidationError('Mot de passe est trop court (10 caractères minimum).')
|
2019-02-03 16:52:42 +01:00
|
|
|
# TODO: add more rules >:]
|
|
|
|
|
2019-06-05 11:35:54 +02:00
|
|
|
|
2019-02-04 17:02:25 +01:00
|
|
|
def avatar(form, avatar):
|
|
|
|
pass
|
|
|
|
|
2019-06-05 11:35:54 +02:00
|
|
|
|
2019-02-04 17:02:25 +01:00
|
|
|
def old_password(form, field):
|
|
|
|
if field.data:
|
|
|
|
if not form.old_password.data:
|
|
|
|
raise ValidationError('Votre ancien mot de passe est requis pour cette modification.')
|
|
|
|
if not current_user.check_password(form.old_password.data):
|
|
|
|
raise ValidationError('Mot de passe actuel erroné.')
|
2019-06-07 01:44:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def id_exists(object):
|
|
|
|
"""Check if an id exists in a table"""
|
|
|
|
def _id_exists(form, id):
|
|
|
|
try:
|
|
|
|
id = int(id.data)
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError('L\'id n\'est pas un entier valide')
|
|
|
|
r = object.query.filter_by(id=id)
|
|
|
|
if not r:
|
|
|
|
raise ValidationError('L\'id n\'existe pas dans la BDD')
|
|
|
|
return _id_exists
|
|
|
|
|
|
|
|
|
|
|
|
def css(form, css):
|
|
|
|
"""Check if input is valid and sane CSS"""
|
|
|
|
pass
|