diff --git a/app/forms/forum.py b/app/forms/forum.py index 30bcd90..fbcf5c4 100644 --- a/app/forms/forum.py +++ b/app/forms/forum.py @@ -2,25 +2,45 @@ from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, TextAreaField, MultipleFileField from wtforms.validators import InputRequired, Length import app.utils.validators as vd +from app.utils.antibot_field import AntibotField class CommentForm(FlaskForm): - message = TextAreaField('Message', validators=[InputRequired()]) - attachments = MultipleFileField('Pièces-jointes', - validators=[vd.file.optional, vd.file.count, vd.file.extension, - vd.file.size, vd.file.namelength]) + message = TextAreaField( + 'Message', + validators=[InputRequired()]) + + attachments = MultipleFileField( + 'Pièces-jointes', + validators=[ + vd.file.optional, + vd.file.count, + vd.file.extension, + vd.file.size, + vd.file.namelength + ]) + submit = SubmitField('Commenter') class AnonymousCommentForm(CommentForm): - pseudo = StringField('Pseudo', + pseudo = StringField( + 'Pseudo', validators=[InputRequired(), vd.name.valid, vd.name.available]) + ab = AntibotField() class CommentEditForm(CommentForm): # Boolean fields to remove files are added dynamically - attachments = MultipleFileField('Ajouter des pièces jointes', - validators=[vd.file.optional, vd.file.count, vd.file.extension, - vd.file.size, vd.file.namelength]) + attachments = MultipleFileField( + 'Ajouter des pièces jointes', + validators=[ + vd.file.optional, + vd.file.count, + vd.file.extension, + vd.file.size, + vd.file.namelength + ]) + submit = SubmitField('Modifier') @@ -29,10 +49,12 @@ class AnonymousCommentEditForm(CommentEditForm, AnonymousCommentForm): class TopicCreationForm(CommentForm): - title = StringField('Nom du sujet', + title = StringField( + 'Nom du sujet', validators=[InputRequired(), Length(min=3, max=128)]) + submit = SubmitField('Créer le sujet') class AnonymousTopicCreationForm(TopicCreationForm, AnonymousCommentForm): - pass + ab = AntibotField() diff --git a/app/static/css/form.css b/app/static/css/form.css index 7fe9dfb..bab32f3 100644 --- a/app/static/css/form.css +++ b/app/static/css/form.css @@ -102,6 +102,10 @@ /*width: 20%;*/ } +.form input.abfield { + display: none; +} + .form form .msgerror { color: var(--error); font-weight: 400; diff --git a/app/templates/forum/forum.html b/app/templates/forum/forum.html index 8a33226..0c6e440 100644 --- a/app/templates/forum/forum.html +++ b/app/templates/forum/forum.html @@ -62,6 +62,7 @@ {{ error }} {% endfor %} + {{ form.ab }} {% endif %}
diff --git a/app/templates/forum/topic.html b/app/templates/forum/topic.html index 5853b28..712ac86 100644 --- a/app/templates/forum/topic.html +++ b/app/templates/forum/topic.html @@ -33,11 +33,14 @@ {{ form.hidden_tag() }} {% if form.pseudo %} +
{{ form.pseudo.label }} {{ form.pseudo }} {% for error in form.pseudo.errors %} {{ error }} {% endfor %} + {{ form.ab }} +
{% endif %} {{ widget_editor.text_editor(form.message, label=False) }} diff --git a/app/utils/antibot_field.py b/app/utils/antibot_field.py new file mode 100644 index 0000000..4ceedf3 --- /dev/null +++ b/app/utils/antibot_field.py @@ -0,0 +1,20 @@ +from wtforms.fields.html5 import EmailField +from wtforms.validators import Optional, ValidationError + +def antibot_validator(form, field): + if field.data: + raise ValidationError('Bas les pattes!') + return True + +class AntibotField(EmailField): + + def __init__(self, *args, **kwargs): + super().__init__( + "L'adresse email", + *args, + validators=[Optional(), antibot_validator], + **kwargs) + + def __call__(self, *args, **kwargs): + return super().__call__(*args, **kwargs, + class_="abfield", autocomplete="no", tabindex="-1")