File size: 4,729 Bytes
08ec4c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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")