File size: 1,436 Bytes
cb18dab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from flask_wtf import FlaskForm
from wtforms import TextAreaField, MultipleFileField, StringField, SubmitField
from wtforms.validators import Optional, Length

class PostForm(FlaskForm):
    nickname = StringField('Pseudo', validators=[Optional(), Length(max=50)])
    content = TextAreaField('Message', validators=[Optional(), Length(max=2000)])
    files = MultipleFileField('Fichiers', validators=[Optional()])
    honeypot = StringField('Site Web', validators=[Optional()]) # Anti-spam field
    submit = SubmitField('Envoyer')

    def validate(self, extra_validators=None):
        initial_validation = super(PostForm, self).validate(extra_validators)
        if not initial_validation:
            return False
        
        # Custom validation: Must have either content or files
        if not self.content.data and not self.files.data:
            # Check if files list is empty or contains empty file
            has_files = False
            if self.files.data:
                for f in self.files.data:
                    if f.filename:
                        has_files = True
                        break
            
            if not has_files and not self.content.data:
                self.content.errors.append('Vous devez fournir un message ou un fichier.')
                return False
        
        # Honeypot check
        if self.honeypot.data:
            return False
            
        return True