Blame view

flaskr/forms.py 4.12 KB
37fd3b20   Antoine Goutenoir   Update the base F...
1
from flask_wtf import FlaskForm
c81df6e7   Goutte   Add the forms mod...
2
3
4
5
6
7
8
9
from wtforms import \
    StringField, \
    PasswordField, \
    TextAreaField, \
    BooleanField
from wtforms import validators

from .models import User
d8a2ae6b   Antoine Goutenoir   Provide configura...
10
from .content import content_dict as content
35a99ac7   Goutte   Move more text co...
11
12

form_content = content['estimate']['form']
c81df6e7   Goutte   Add the forms mod...
13
14
15
16


# ESTIMATION FORM #############################################################

37fd3b20   Antoine Goutenoir   Update the base F...
17
class EstimateForm(FlaskForm):
f47a875d   Antoine Goutenoir   Remove the email ...
18
19
20
21
22
23
24
25
    # email = StringField(
    #     label=form_content['email']['label'],
    #     description=form_content['email']['description'],
    #     validators=[
    #         validators.DataRequired(),
    #         validators.Email(),
    #     ],
    # )
c81df6e7   Goutte   Add the forms mod...
26
    first_name = StringField(
35a99ac7   Goutte   Move more text co...
27
28
        label=form_content['first_name']['label'],
        description=form_content['first_name']['description'],
c81df6e7   Goutte   Add the forms mod...
29
30
31
32
        validators=[
            validators.Optional(),
            validators.Length(max=1024),
        ],
f47a875d   Antoine Goutenoir   Remove the email ...
33
34
35
        render_kw={
            "placeholder": form_content['first_name']['placeholder']
        },
c81df6e7   Goutte   Add the forms mod...
36
37
    )
    last_name = StringField(
35a99ac7   Goutte   Move more text co...
38
39
        label=form_content['last_name']['label'],
        description=form_content['last_name']['description'],
c81df6e7   Goutte   Add the forms mod...
40
41
42
43
        validators=[
            validators.Optional(),
            validators.Length(max=1024),
        ],
f47a875d   Antoine Goutenoir   Remove the email ...
44
45
46
        render_kw={
            "placeholder": form_content['last_name']['placeholder']
        },
c81df6e7   Goutte   Add the forms mod...
47
48
    )
    institution = StringField(
35a99ac7   Goutte   Move more text co...
49
50
        label=form_content['institution']['label'],
        description=form_content['institution']['description'],
c81df6e7   Goutte   Add the forms mod...
51
52
53
54
55
56
        validators=[
            validators.Optional(),
            validators.Length(max=1024),
        ],
    )
    comment = TextAreaField(
35a99ac7   Goutte   Move more text co...
57
58
        label=form_content['comment']['label'],
        description=form_content['comment']['description'],
c81df6e7   Goutte   Add the forms mod...
59
60
61
62
63
64
        validators=[
            validators.Optional(),
            validators.Length(max=2048),
        ],
    )
    origin_addresses = TextAreaField(
35a99ac7   Goutte   Move more text co...
65
66
        label=form_content['origin_addresses']['label'],
        description=form_content['origin_addresses']['description'],
c81df6e7   Goutte   Add the forms mod...
67
68
69
        validators=[
            validators.DataRequired(),
        ],
35a99ac7   Goutte   Move more text co...
70
71
72
        render_kw={
            "placeholder": form_content['origin_addresses']['placeholder']
        },
c81df6e7   Goutte   Add the forms mod...
73
74
    )
    destination_addresses = TextAreaField(
35a99ac7   Goutte   Move more text co...
75
76
        label=form_content['destination_addresses']['label'],
        description=form_content['destination_addresses']['description'],
c81df6e7   Goutte   Add the forms mod...
77
78
79
        validators=[
            validators.DataRequired(),
        ],
35a99ac7   Goutte   Move more text co...
80
81
82
83
84
85
86
87
88
89
90
        render_kw={
            "placeholder": form_content['destination_addresses']['placeholder']
        },
    )
    compute_optimal_destination = BooleanField(
        label=form_content['compute_optimal_destination']['label'],
        description=form_content['compute_optimal_destination']['description'],
        default=False,
        validators=[
            validators.Optional(),
        ],
c81df6e7   Goutte   Add the forms mod...
91
    )
35a99ac7   Goutte   Move more text co...
92
93
94
    use_atmosfair_rfi = BooleanField(
        label=form_content['use_atmosfair_rfi']['label'],
        description=form_content['use_atmosfair_rfi']['description'],
c81df6e7   Goutte   Add the forms mod...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        default=False,
        validators=[
            validators.Optional(),
        ],
    )

    def validate(self):
        check_validate = super(EstimateForm, self).validate()

        # Do our validators pass?
        if not check_validate:
            return False

        return True


# LOGIN FORM ##################################################################

37fd3b20   Antoine Goutenoir   Update the base F...
113
class LoginForm(FlaskForm):
c81df6e7   Goutte   Add the forms mod...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    username = StringField(u'Username', validators=[validators.required()])
    password = PasswordField(u'Password', validators=[validators.optional()])

    def validate(self):
        check_validate = super(LoginForm, self).validate()

        # Do our validators pass?
        if not check_validate:
            return False

        # Does the user even exist?
        user = User.query.filter_by(username=self.username.data).first()
        if not user:
            self.username.errors.append('Invalid username or password')
            return False

        # Do the passwords match?
        if not user.check_password(self.password.data):
            self.username.errors.append('Invalid username or password')
            return False

        return True