-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforms.py
101 lines (80 loc) · 3.08 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
# -*- coding: utf-8 -*-
'''
The WTForms specification for forms generated on the server
'''
# framework imports
from flask_login import current_user
from flask_wtf import FlaskForm
from wtforms import BooleanField, PasswordField, StringField, SubmitField
from wtforms.fields.html5 import EmailField, TelField
from wtforms.validators import InputRequired, Email, EqualTo, Regexp, ValidationError
# app imports
from mjserver.models import Player
#%% --- fields
class MyEmailField(EmailField):
@staticmethod
def validate_email(form, email):
user = Player.query.filter_by(email=email.data).first()
if user is not None and user is not current_user:
raise ValidationError('Email already in use. Please use a different one.')
#%% --- forms
class EmailForm(FlaskForm):
email = StringField('Email', validators=[InputRequired(), Email()])
submit = SubmitField('Please send me a password-reset link')
class LoginForm(FlaskForm):
''' login an existing user '''
name = StringField('Name', validators=[InputRequired()], render_kw={'autofocus': True},)
password = PasswordField('Password', validators=[InputRequired()])
remember_me = BooleanField('Remember Me')
submit = SubmitField('Sign In')
class PasswordForm(FlaskForm):
''' to reset the password '''
password = PasswordField('Password', validators=[InputRequired()])
password2 = PasswordField(
'Repeat Password', validators=[InputRequired(), EqualTo('password')])
submit = SubmitField('Register')
class ProfileForm(FlaskForm):
''' user can update their own details '''
email = MyEmailField(
validators = [InputRequired() , Email(), MyEmailField.validate_email],
label = 'Your email',
)
token = StringField(
'Device token (to tie a device to this account)',
render_kw={'readonly': True},
)
_reg = '[0-9]{4}'
pin = TelField(
label = '4-digit PIN number',
validators = [InputRequired(), Regexp(_reg, message='Must be 4 digits')],
render_kw={
'minlength': 4,
'maxlength': 4,
'pattern': _reg,
'title': 'four digits'
}
)
class RegistrationForm(FlaskForm):
''' register a new user '''
name = StringField('Name', validators=[InputRequired()])
email = MyEmailField('Email')
validate_email = MyEmailField.validate_email
password = PasswordField('Password', validators=[InputRequired()])
password2 = PasswordField(
'Repeat Password', validators=[InputRequired(), EqualTo('password')])
_reg = '[0-9]{4}'
pin = TelField(
label = '4-digit PIN number',
validators = [InputRequired(), Regexp(_reg, message='Must be 4 digits')],
render_kw={
'minlength': 4,
'maxlength': 4,
'pattern': _reg,
'title': 'four digits'
}
)
submit = SubmitField('Register')
def validate_name(self, name):
user = Player.query.filter_by(name=name.data).first()
if user is not None:
raise ValidationError('Name already in use. Please use a different one.')