-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
94 lines (80 loc) · 2.89 KB
/
forms.py
File metadata and controls
94 lines (80 loc) · 2.89 KB
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
import re
# Third-party imports
from wtforms import (
Form, StringField, PasswordField, TextAreaField, validators,
ValidationError, SubmitField)
# Internal project imports
from hashutils import check_against_hash
from model import User
USERNAME_RE = re.compile(r"^[a-zA-Z0-9_-]+$")
# Custom validators.
def user_exists(form, field):
if User.by_prop('name', field.data):
raise ValidationError('User "{}" already exists!'.format(field.data))
def length(min, max):
def _length(form, field):
value = len(field.data) if field.data else 0
if value < min:
adjective = 'short'
elif value > max:
adjective = 'long'
else:
return
message = (
"{field_name} is too {adjective}! Must be {min} to {max} "
"characters long".format(
field_name=field.label.text, adjective=adjective, min=min,
max=max))
raise ValidationError(message)
return _length
class SignupForm(Form):
username = StringField(
'Username',
[user_exists,
validators.input_required(message='You must specify username!'),
length(3, 20),
validators.regexp(
USERNAME_RE,
message='Invalid username! May contain only latin letters, '
'digits, dash and underscore')]
)
password = PasswordField(
'Password',
[validators.input_required(message='You must specify password!'),
length(3, 20)]
)
verify = PasswordField(
'Verify password',
[validators.equal_to('password', message='Passwords do not match!')]
)
email = StringField(
"Email (optional)",
[validators.optional(),
validators.Email(message='Invalid email address!')])
submit = SubmitField('Create')
class LoginForm(Form):
username = StringField('Username')
password = PasswordField('Password')
submit = SubmitField('Sign In')
_user = None
# Username validation was put inside because there's not other way (?) to
# control order in which fields are validated.
def validate_password(form, field):
message = 'Something is wrong with your username or password'
form._user = User.by_prop('name', form.username.data)
if not form._user:
raise ValidationError(message)
pwd_hash = form._user.password_hash
if not check_against_hash(form.username.data + field.data, pwd_hash):
raise ValidationError(message)
class EditForm(Form):
head = StringField(
'Article head',
[validators.input_required('Head cannot be empty!'),
validators.length(
max=256,
message="Article head is too long! Must not exceed 256 "
"characters")],
id='wiki-head')
body = TextAreaField('Article body', id='wiki-body')
submit = SubmitField('Save')