-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
324 lines (279 loc) · 11.7 KB
/
forms.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Standard Library
import datetime
# Third Party
import flask
import flask_admin.form
import flask_security
import flask_wtf
import phonenumbers
import pyotp
import pytz
import wtforms
import wtforms.ext.sqlalchemy.fields
# Local
import main
import models
import utils
def simple_field_filter(field):
special_fields = (wtforms.HiddenField, wtforms.FileField, wtforms.SubmitField)
return not isinstance(field, special_fields)
def time_since(in_dt):
"""
Calculate time since a given (date)time.
Takes a datetime object and returns the time between it and now as a nicely
formatted string, e.g. "10 minutes". If d occurs after now, then "0 minutes" is
returned.
"""
chunks = (
(60 * 60 * 24 * 365, "{} year"),
(60 * 60 * 24 * 30, "{} month"),
(60 * 60 * 24 * 7, "{} week"),
(60 * 60 * 24, "{} day"),
(60 * 60, "{} hour"),
(60, "{} minute"),
)
# Convert datetime.date to datetime.datetime for comparison.
if not isinstance(in_dt, datetime.datetime):
in_dt = datetime.datetime(in_dt.year, in_dt.month, in_dt.day)
now = datetime.datetime.utcnow()
delta = now - in_dt
# ignore microseconds
since = delta.days * 24 * 60 * 60 + delta.seconds
if since <= 0:
# in_dt is in the future compared to now, stop processing.
return "0 minutes"
for i, (seconds, name) in enumerate(chunks):
count = since // seconds
if count != 0:
break
result = name.format(count)
if count > 1:
result += "s"
if i + 1 < len(chunks):
# Now get the second item
seconds2, name2 = chunks[i + 1]
count2 = (since - (seconds * count)) // seconds2
if count2 != 0:
result += ", {}".format(name2.format(count2))
if count2 > 1:
result += "s"
return result
main.app.jinja_env.filters["simple_field_filter"] = simple_field_filter
main.app.jinja_env.filters["time_since"] = time_since
REQUIRED = wtforms.validators.DataRequired()
def valid_user_email(form, field):
try:
utils.format_phone(form.data)
except phonenumbers.NumberParseException:
raise wtforms.ValidationError("Invalid mobile number.")
class ContactFormMixin(object):
country_code = wtforms.StringField(default="1")
phone = wtforms.StringField(label="Mobile Number")
email = wtforms.StringField(
validators=[
wtforms.validators.Email(),
wtforms.validators.Optional(),
valid_user_email,
]
)
class ContactForm(
flask_wtf.FlaskForm, ContactFormMixin, flask_security.forms.NextFormMixin
):
def __init__(self, *args, **kwargs): # noqa: D107
if "next" in flask.session:
del flask.session["next"]
super(ContactForm, self).__init__(*args, **kwargs)
if not self.next.data:
self.next.data = flask.request.args.get("next", "")
def validate(self):
if not super(ContactForm, self).validate():
return False
if not (self.email.data or utils.format_phone(self.data)):
message = "Please provide either a mobile number or email address."
self.phone.errors.append(message)
self.email.errors.append(message)
return False
return True
minutes = [(x, "{:02d}".format(x)) for x in range(0, 60, 15)]
weekday_choices = [
(6, "Sunday"),
(0, "Monday"),
(1, "Tuesday"),
(2, "Wednesday"),
(3, "Thursday"),
(4, "Friday"),
(5, "Saturday"),
]
timezone_choices = list(zip(pytz.common_timezones, pytz.common_timezones))
timezone_choices.insert(0, ("", ""))
class ScheduleForm(flask_wtf.FlaskForm, flask_security.forms.NextFormMixin):
days_of_week = wtforms.SelectMultipleField(
"Days of the Week", [REQUIRED], choices=weekday_choices, coerce=int
)
hour = wtforms.SelectField(
choices=[(x, x) for x in range(1, 13)],
validators=[REQUIRED],
default=6,
coerce=int,
)
minute = wtforms.SelectField(choices=minutes, coerce=int)
am_pm = wtforms.RadioField("Time of Day", choices=[("am", "am"), ("pm", "pm")])
timezone = flask_admin.form.Select2Field(
choices=timezone_choices, validators=[REQUIRED]
)
def __init__(self, *args, **kwargs): # noqa: D107
if "next" in flask.session:
del flask.session["next"]
super(ScheduleForm, self).__init__(*args, **kwargs)
if not self.next.data:
self.next.data = flask.request.args.get("next", "")
class MobileVerifyForm(flask_wtf.FlaskForm, flask_security.forms.NextFormMixin):
code = wtforms.IntegerField()
def __init__(self, *args, **kwargs): # noqa: D107
super(MobileVerifyForm, self).__init__(*args, **kwargs)
if not self.next.data:
self.next.data = flask.request.args.get("next", "")
class SuggestMethodForm(flask_wtf.FlaskForm):
name = wtforms.StringField(label="Method Name", validators=[REQUIRED])
section = wtforms.FieldList(
wtforms.StringField(validators=[REQUIRED]), label="Sections", min_entries=2
)
class SuggestActionForm(flask_wtf.FlaskForm):
action_name = flask_admin.form.Select2TagsField(save_as_list=True)
class FeedbackForm(flask_wtf.FlaskForm):
message = wtforms.TextAreaField(validators=[REQUIRED])
class TwoFactorConfirmationForm(
flask_wtf.FlaskForm, flask_security.forms.NextFormMixin
):
token = wtforms.StringField(validators=[REQUIRED])
def __init__(self, *args, **kwargs): # noqa: D107
super(TwoFactorConfirmationForm, self).__init__(*args, **kwargs)
if not self.next.data:
self.next.data = flask.request.args.get("next", "")
def validate_token(self, field):
if field.data:
user = getattr(self, "user", flask_security.current_user)
totp = pyotp.TOTP(user.secret)
if not totp.verify(field.data):
raise wtforms.ValidationError("Invalid token")
def unique_user_email(form, field):
user = models.User.query.filter_by(email=field.data).first()
if user is not None and user.confirmed_at is not None:
get_message = flask_security.utils.get_message
msg = get_message("EMAIL_ALREADY_ASSOCIATED", email=field.data)[0]
raise wtforms.ValidationError(msg)
class LoginForm(
flask_wtf.FlaskForm, flask_security.forms.NextFormMixin, ContactFormMixin
):
password = wtforms.PasswordField("Password")
remember = wtforms.BooleanField("Remember Me")
submit = wtforms.SubmitField("Login")
def __init__(self, *args, **kwargs): # noqa: D107
super(LoginForm, self).__init__(*args, **kwargs)
if not self.next.data:
self.next.data = flask.request.args.get("next", "")
def validate(self):
if not super(LoginForm, self).validate():
return False
security = flask_security
email = self.email.data
phone = utils.format_phone(self.data)
password = self.password.data
if not (email or phone):
message = "Please enter either an email address or phone number."
self.phone.errors.append(message)
self.email.errors.append(message)
return False
if password.strip() == "" or password is None:
message = security.utils.get_message("PASSWORD_NOT_PROVIDED")[0]
self.password.errors.append(message)
return False
self.user = main.user_datastore.get_user(self.email.data)
if self.user is None and phone:
query = main.user_datastore.user_model.query
self.user = query.filter_by(phone=phone).first()
if self.user is None:
message = security.utils.get_message("USER_DOES_NOT_EXIST")[0]
if phone:
self.phone.errors.append(message)
if email:
self.email.errors.append(message)
return False
if not self.user.password:
message = security.utils.get_message("PASSWORD_NOT_SET")[0]
self.password.errors.append(message)
return False
if not security.utils.verify_and_update_password(self.password.data, self.user):
message = security.utils.get_message("INVALID_PASSWORD")[0]
self.password.errors.append(message)
return False
if security.confirmable.requires_confirmation(self.user):
message = security.utils.get_message("CONFIRMATION_REQUIRED")[0]
if phone:
self.phone.errors.append(message)
if email:
self.email.errors.append(message)
return False
if not self.user.is_active():
message = security.utils.get_message("DISABLED_ACCOUNT")[0]
if phone:
self.phone.errors.append(message)
if email:
self.email.errors.append(message)
return False
return True
class ConfirmRegisterForm(
flask_wtf.FlaskForm, ContactFormMixin, flask_security.forms.RegisterFormMixin
):
password = wtforms.PasswordField(
"Password", [REQUIRED, wtforms.validators.Length(6, 128)]
)
def validate(self):
url_for_security = flask_security.utils.url_for_security
if not super(ConfirmRegisterForm, self).validate():
return False
email = self.email.data
phone = utils.format_phone(self.data)
if not (email or phone):
message = "Please enter either an email address or phone number."
self.phone.errors.append(message)
self.email.errors.append(message)
return False
user = main.user_datastore.get_user(self.email.data)
if user is None and phone:
query = main.user_datastore.user_model.query
user = query.filter_by(phone=phone).first()
if user and user.password and user.confirmed_at:
login_url = flask.url_for("login")
forgot_url = url_for_security("forgot_password")
message = "You have already successfully registered. You should be"
message += ' able to <a href="{}" class="alert-link">login</a>. If'
message += "you have forgotten your password, please visit the"
message += ' <a href="{}" class="alert-link">forgotten'
message += " password</a> page."
flask.flash(message.format(login_url, forgot_url), "error")
errors = self.phone.errors if phone else self.email.errors
errors.append("Already registered")
return False
if user and email and user.password and user.confirmed_at is None:
confirm_url = url_for_security("send_confirmation")
message = "You have already registered but need to confirm your"
message += " email address. If you have deleted or did not receive"
message += ' your confirmation email, you may <a href="{}"'
message += ' class="alert-link">send a new request</a>.'
flask.flash(message.format(confirm_url), "error")
self.email.errors.append("Registration pending")
return False
if user and phone and user.password and user.confirmed_at is None:
next_url = flask_security.utils.get_post_register_redirect()
confirm_url = flask.url_for(
"confirm_mobile", action="re-send", next=next_url
)
message = "You have already registered but need to confirm your"
message += " mobile number. If you have deleted or did not receive"
message += ' your confirmation SMS, you may <a href="{}"'
message += ' class="alert-link">send a new request</a>.'
flask.flash(message.format(confirm_url), "error")
self.phone.errors.append("Registration pending")
return False
return True