69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
from flask_login import current_user
|
||
from wtforms.validators import ValidationError
|
||
from app.models.user import Member
|
||
from app.models.trophy import Title
|
||
from werkzeug.exceptions import NotFound
|
||
|
||
from app.utils.validators.file import *
|
||
from app.utils.validators.name import *
|
||
from app.utils.validators.password import *
|
||
|
||
import re
|
||
|
||
|
||
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.')
|
||
|
||
|
||
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"""
|
||
prop = r'[a-zA-Z-]+\s*:\s*[^;{}\'"]+'
|
||
stylesheet = rf'\s*(?:{prop};\s*)*{prop};?\s*'
|
||
|
||
if css.data and re.fullmatch(stylesheet, css.data) is None:
|
||
raise ValidationError('CSS invalide (les caractères ;{}\'" sont '+\
|
||
'interdits dans les valeurs)')
|
||
return True
|
||
|
||
def own_title(form, title):
|
||
# Everyone can use "Member"
|
||
if title.data == -1:
|
||
return True
|
||
|
||
try:
|
||
t = Title.query.get_or_404(title.data)
|
||
except NotFound:
|
||
return False
|
||
except ValueError:
|
||
return False
|
||
|
||
if t in current_user.trophies:
|
||
return True
|
||
else:
|
||
return False
|
||
|
||
def attachment_exists(form, number):
|
||
try:
|
||
n = int(number.data)
|
||
if n <= 0 or n > len(form.attachments.data):
|
||
raise Exception()
|
||
except Exception as e:
|
||
raise ValidationError('L’illustration doit être le numéro d’une pièce-jointe')
|
||
|
||
return True
|