| from flask_wtf import FlaskForm | |
| from wtforms import ( | |
| StringField, | |
| PasswordField, | |
| SelectField, | |
| SubmitField, | |
| FloatField, | |
| TextAreaField, | |
| ) | |
| from wtforms.validators import ( | |
| DataRequired, | |
| Length, | |
| EqualTo, | |
| ValidationError, | |
| NumberRange, | |
| ) | |
| from flask_login import current_user | |
| from app.models import User | |
| import re | |
| def validate_phone(form, field): | |
| phone = field.data | |
| country_code = form.country_code.data if hasattr(form, "country_code") else None | |
| if not re.match(r"^\d+$", phone): | |
| raise ValidationError( | |
| "Le numéro de téléphone ne doit contenir que des chiffres." | |
| ) | |
| if len(phone) < 8 or len(phone) > 15: | |
| raise ValidationError( | |
| "Le numéro de téléphone doit contenir entre 8 et 15 chiffres." | |
| ) | |
| def validate_password(form, field): | |
| password = field.data | |
| if len(password) < 8: | |
| raise ValidationError("Le mot de passe doit contenir au moins 8 caractères.") | |
| if not re.search(r"[A-Z]", password): | |
| raise ValidationError("Le mot de passe doit contenir au moins une majuscule.") | |
| if not re.search(r"\d", password): | |
| raise ValidationError("Le mot de passe doit contenir au moins un chiffre.") | |
| class LoginForm(FlaskForm): | |
| phone = StringField("Téléphone", validators=[DataRequired(), validate_phone]) | |
| password = PasswordField("Mot de passe", validators=[DataRequired()]) | |
| country_code = SelectField( | |
| "Pays", | |
| choices=[ | |
| ("+228", "Togo (+228)"), | |
| ("+229", "Bénin (+229)"), | |
| ("+226", "Burkina Faso (+226)"), | |
| ("+225", "Côte d'Ivoire (+225)"), | |
| ("+224", "Guinée (+224)"), | |
| ("+223", "Mali (+223)"), | |
| ("+221", "Sénégal (+221)"), | |
| ("+237", "Cameroun (+237)"), | |
| ("+241", "Gabon (+241)"), | |
| ], | |
| validators=[DataRequired()], | |
| ) | |
| submit = SubmitField("Se connecter") | |
| class RegistrationForm(FlaskForm): | |
| name = StringField( | |
| "Nom / Pseudo", | |
| validators=[ | |
| DataRequired(message="Le nom est requis"), | |
| Length( | |
| min=2, max=50, message="Le nom doit contenir entre 2 et 50 caractères" | |
| ), | |
| ], | |
| ) | |
| phone = StringField("Téléphone", validators=[DataRequired(), validate_phone]) | |
| password = PasswordField( | |
| "Mot de passe", validators=[DataRequired(), validate_password] | |
| ) | |
| confirm_password = PasswordField( | |
| "Confirmer le mot de passe", | |
| validators=[ | |
| DataRequired(), | |
| EqualTo("password", message="Les mots de passe ne correspondent pas"), | |
| ], | |
| ) | |
| country_code = SelectField( | |
| "Pays", | |
| choices=[ | |
| ("+228", "Togo (+228)"), | |
| ("+229", "Bénin (+229)"), | |
| ("+226", "Burkina Faso (+226)"), | |
| ("+225", "Côte d'Ivoire (+225)"), | |
| ("+224", "Guinée (+224)"), | |
| ("+223", "Mali (+223)"), | |
| ("+221", "Sénégal (+221)"), | |
| ("+237", "Cameroun (+237)"), | |
| ("+241", "Gabon (+241)"), | |
| ], | |
| validators=[DataRequired()], | |
| ) | |
| referral_code = StringField("Code de parrainage (optionnel)") | |
| submit = SubmitField("S'inscrire") | |
| def validate_phone(self, field): | |
| phone = field.data | |
| country_code = self.country_code.data | |
| full_phone = country_code + phone | |
| if User.query.filter_by(phone=phone).first(): | |
| raise ValidationError("Ce numéro de téléphone est déjà utilisé.") | |
| class DepositForm(FlaskForm): | |
| amount = FloatField( | |
| "Montant (FCFA)", | |
| validators=[ | |
| DataRequired(), | |
| NumberRange(min=100, message="Le montant minimum est de 100 FCFA"), | |
| ], | |
| ) | |
| submit = SubmitField("Déposer") | |
| class WithdrawalForm(FlaskForm): | |
| amount = FloatField( | |
| "Montant (FCFA)", | |
| validators=[ | |
| DataRequired(), | |
| NumberRange( | |
| min=500, message="Le montant minimum de retrait est de 500 FCFA" | |
| ), | |
| ], | |
| ) | |
| phone = StringField( | |
| "Téléphone de destination", validators=[DataRequired(), validate_phone] | |
| ) | |
| country_code = SelectField( | |
| "Pays", | |
| choices=[ | |
| ("+228", "Togo (+228)"), | |
| ("+229", "Bénin (+229)"), | |
| ("+226", "Burkina Faso (+226)"), | |
| ("+225", "Côte d'Ivoire (+225)"), | |
| ("+224", "Guinée (+224)"), | |
| ("+223", "Mali (+223)"), | |
| ("+221", "Sénégal (+221)"), | |
| ("+237", "Cameroun (+237)"), | |
| ("+241", "Gabon (+241)"), | |
| ], | |
| validators=[DataRequired()], | |
| ) | |
| submit = SubmitField("Retirer") | |