forum: anti-bot fields for guest topics and comments (#51)
This commit is contained in:
parent
888006cf86
commit
1837f8f9a6
|
@ -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()
|
||||
|
|
|
@ -102,6 +102,10 @@
|
|||
/*width: 20%;*/
|
||||
}
|
||||
|
||||
.form input.abfield {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.form form .msgerror {
|
||||
color: var(--error);
|
||||
font-weight: 400;
|
||||
|
|
|
@ -62,6 +62,7 @@
|
|||
<span class="msgerror">{{ error }}</span>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{{ form.ab }}
|
||||
{% endif %}
|
||||
|
||||
<div>
|
||||
|
|
|
@ -33,11 +33,14 @@
|
|||
{{ form.hidden_tag() }}
|
||||
|
||||
{% if form.pseudo %}
|
||||
<div>
|
||||
{{ form.pseudo.label }}
|
||||
{{ form.pseudo }}
|
||||
{% for error in form.pseudo.errors %}
|
||||
<span class="msgerror">{{ error }}</span>
|
||||
{% endfor %}
|
||||
{{ form.ab }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{{ widget_editor.text_editor(form.message, label=False) }}
|
||||
|
|
|
@ -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")
|
Loading…
Reference in New Issue