-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.py
172 lines (128 loc) · 4.24 KB
/
validators.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
#
#
# Imports
#
#
import re
from auth import check_pw_hash
from models import User, Blog
# blog key needed to make ancestor queries that have strong consistency
BLOG_KEY = Blog.blog_key()
#
#
# Form validation
#
#
def valid_username(username):
user_re = re.compile(r"^[a-zA-Z0-9_-]{3,50}$")
return username and user_re.match(username)
def valid_password(password):
password_re = re.compile(r"^.{3,50}$")
return password and password_re.match(password)
def valid_email(email):
email_re = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
return not email or email_re.match(email)
def valid_subject(subject):
subject_re = re.compile(r"^.{3,200}$")
return subject and subject_re.match(subject)
def valid_content(content):
return content
def validate_signup_form(username, password, verify, email):
"""
Validate the signup form.
If the form is valid, return a tuple of (True, None).
If the form is not valid, return a tuple of (False, dictionary of
error messages).
"""
have_error = False
errors = {}
user = User.query(User.name == username, ancestor=BLOG_KEY).get()
if not valid_username(username):
errors['error_username'] = "That's not a valid username."
have_error = True
elif user:
errors['error_username'] = "This username is already taken."
have_error = True
if not valid_password(password):
errors['error_password'] = "That wasn't a valid password."
have_error = True
elif password != verify:
errors['error_verify'] = "Your passwords didn't match."
have_error = True
if not valid_email(email):
errors['error_email'] = "That's not a valid email."
have_error = True
if have_error:
return (False, errors)
else:
return (True, None)
def validate_create_post_form(subject, content):
"""
Validate the form to create a post.
If the form is valid, return a tuple of (True, None).
If the form is not valid, return a tuple of (False, dictionary of
error messages).
"""
have_error = False
errors = {}
if not valid_subject(subject):
errors['error_subject'] = """The subject must be between 3 and 200
characters long."""
have_error = True
if not valid_content(content):
errors['error_content'] = """Come on, only one character is required,
you can do this!"""
have_error = True
if have_error:
return (False, errors)
else:
return (True, None)
def validate_update_post_form(subject, content):
"""
Validate the form used to update a post.
If the form is valid, return a tuple of (True, None).
If the form is not valid, return a tuple of (False, dictionary of
error messages).
"""
have_error = False
errors = {}
if not valid_subject(subject):
errors['error_subject'] = """The subject must be between 3 and 200
characters long."""
have_error = True
if not valid_content(content):
errors['error_content'] = """Come on, only one character is required,
you can do this!"""
have_error = True
if have_error:
return (False, errors)
else:
return (True, None)
def validate_login_form(username, password):
"""
Validate the login form.
If the form is valid, return a tuple of (True, None).
If the form is not valid, return a tuple of (False, dictionary of
error messages).
"""
have_error = False
errors = {}
user = User.query(User.name == username, ancestor=BLOG_KEY).get()
if not user:
errors['error_username'] = "This username doesn't exist."
have_error = True
elif user and not (check_pw_hash(username,
password,
user.hashed_password)):
errors['error_password'] = "The password is incorrect."
have_error = True
elif not valid_username(username):
errors['error_username'] = "That's not a valid username."
have_error = True
if not valid_password(password):
errors['error_password'] = "That wasn't a valid password."
have_error = True
if have_error:
return (False, errors)
else:
return (True, None)