Blame view

flaskr/forms.py 5.2 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
77e86148   Antoine Goutenoir   Fake support for ...
11
from .core import models
35a99ac7   Goutte   Move more text co...
12
13

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


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

37fd3b20   Antoine Goutenoir   Update the base F...
18
class EstimateForm(FlaskForm):
77e86148   Antoine Goutenoir   Fake support for ...
19

f47a875d   Antoine Goutenoir   Remove the email ...
20
21
22
23
24
25
26
27
    # email = StringField(
    #     label=form_content['email']['label'],
    #     description=form_content['email']['description'],
    #     validators=[
    #         validators.DataRequired(),
    #         validators.Email(),
    #     ],
    # )
77e86148   Antoine Goutenoir   Fake support for ...
28

c81df6e7   Goutte   Add the forms mod...
29
    first_name = StringField(
35a99ac7   Goutte   Move more text co...
30
31
        label=form_content['first_name']['label'],
        description=form_content['first_name']['description'],
c81df6e7   Goutte   Add the forms mod...
32
33
34
35
        validators=[
            validators.Optional(),
            validators.Length(max=1024),
        ],
f47a875d   Antoine Goutenoir   Remove the email ...
36
37
38
        render_kw={
            "placeholder": form_content['first_name']['placeholder']
        },
c81df6e7   Goutte   Add the forms mod...
39
40
    )
    last_name = StringField(
35a99ac7   Goutte   Move more text co...
41
42
        label=form_content['last_name']['label'],
        description=form_content['last_name']['description'],
c81df6e7   Goutte   Add the forms mod...
43
44
45
46
        validators=[
            validators.Optional(),
            validators.Length(max=1024),
        ],
f47a875d   Antoine Goutenoir   Remove the email ...
47
48
49
        render_kw={
            "placeholder": form_content['last_name']['placeholder']
        },
c81df6e7   Goutte   Add the forms mod...
50
51
    )
    institution = StringField(
35a99ac7   Goutte   Move more text co...
52
53
        label=form_content['institution']['label'],
        description=form_content['institution']['description'],
c81df6e7   Goutte   Add the forms mod...
54
55
56
57
58
59
        validators=[
            validators.Optional(),
            validators.Length(max=1024),
        ],
    )
    comment = TextAreaField(
35a99ac7   Goutte   Move more text co...
60
61
        label=form_content['comment']['label'],
        description=form_content['comment']['description'],
c81df6e7   Goutte   Add the forms mod...
62
63
64
65
66
67
        validators=[
            validators.Optional(),
            validators.Length(max=2048),
        ],
    )
    origin_addresses = TextAreaField(
35a99ac7   Goutte   Move more text co...
68
69
        label=form_content['origin_addresses']['label'],
        description=form_content['origin_addresses']['description'],
c81df6e7   Goutte   Add the forms mod...
70
71
72
        validators=[
            validators.DataRequired(),
        ],
35a99ac7   Goutte   Move more text co...
73
74
75
        render_kw={
            "placeholder": form_content['origin_addresses']['placeholder']
        },
c81df6e7   Goutte   Add the forms mod...
76
77
    )
    destination_addresses = TextAreaField(
35a99ac7   Goutte   Move more text co...
78
79
        label=form_content['destination_addresses']['label'],
        description=form_content['destination_addresses']['description'],
c81df6e7   Goutte   Add the forms mod...
80
81
82
        validators=[
            validators.DataRequired(),
        ],
35a99ac7   Goutte   Move more text co...
83
84
85
86
        render_kw={
            "placeholder": form_content['destination_addresses']['placeholder']
        },
    )
77e86148   Antoine Goutenoir   Fake support for ...
87

7f7c6b10   Antoine Goutenoir   Disable RFI in th...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    # compute_optimal_destination = BooleanField(
    #     label=form_content['compute_optimal_destination']['label'],
    #     description=form_content['compute_optimal_destination']['description'],
    #     default=False,
    #     validators=[
    #         validators.Optional(),
    #     ],
    # )
    # use_atmosfair_rfi = BooleanField(
    #     label=form_content['use_atmosfair_rfi']['label'],
    #     description=form_content['use_atmosfair_rfi']['description'],
    #     default=False,
    #     validators=[
    #         validators.Optional(),
    #     ],
    # )
c81df6e7   Goutte   Add the forms mod...
104
105
106
107
108
109
110
111

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

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

77e86148   Antoine Goutenoir   Fake support for ...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        uses_at_least_one_model = False
        for model in models:
            use_model = getattr(self, 'use_model_%s' % model.slug)
            #print("Model data", model.slug, use_model.data)
            if use_model.data:
                uses_at_least_one_model = True

        if not uses_at_least_one_model:
            last_model = getattr(self, 'use_model_%s' % models[-1].slug)
            last_model.errors.append("Please select at least one model."
                                      "  "  # It's been a while
                                      "<em>What are you doing?</em>")
            return False

c81df6e7   Goutte   Add the forms mod...
126
127
128
        return True


77e86148   Antoine Goutenoir   Fake support for ...
129
130
131
132
133
134
135
136
# Add the models' checkboxes to the above Form
for model in models:
    setattr(  # setattr() takes no keyword arguments -.-
        EstimateForm,
        'use_model_%s' % model.slug,
        BooleanField(
            label=model.name,
            # description=model.short_description,
845bbf90   Antoine Goutenoir   Activate the `sel...
137
            default=model.selected,
77e86148   Antoine Goutenoir   Fake support for ...
138
139
140
141
142
143
144
            validators=[
                validators.Optional(),
            ],
        )
    )


c81df6e7   Goutte   Add the forms mod...
145
146
# LOGIN FORM ##################################################################

37fd3b20   Antoine Goutenoir   Update the base F...
147
class LoginForm(FlaskForm):
c81df6e7   Goutte   Add the forms mod...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    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