-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (115 loc) · 4.17 KB
/
Copy pathmain.py
File metadata and controls
134 lines (115 loc) · 4.17 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
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
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2
import cgi
import re
form = """
<head>
<style>
.error{color:red}
</style>
</head>
<form action="" method="post">
<table>
<tr>
<td><h1>Signup</h1></td>
</tr>
<tr>
<td><label>Username</label></td>
<td>
<input name="username" type="text" value="%(username)s" required>
<span class="error">%(error_username)s</span>
</td>
</tr>
</br>
</br>
<tr>
<tr>
<td><label>Password</label></td>
<td>
<input name="password" type="password" value="" required>
<span class="error">%(error_password)s</span>
</td>
</tr>
</br>
</br>
<tr>
<td><label>Verify Password</label></td>
<td>
<input name="verify" type="password" value="" required>
<span class="error">%(error_verify)s</span>
</td>
</tr>
</br>
</br>
<tr>
<td><label>Email</label></td>
<td>
<input name="email" type="email" pattern= "[^ @]*@[^ @]*" value="%(email)s">
<span class="error">%(error_email)s</span>
</td>
</tr>
</table>
</br>
</br>
<input type="submit">
</form>
"""
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def valid_username(username):
return username and USER_RE.match(username)
PASS_RE = re.compile(r"^.{3,20}$")
def valid_password(password):
return password and PASS_RE.match(password)
EMAIL_RE = re.compile(r"^[\S]+@[\S]+.[\S]$")
def valid_email(email):
return email and EMAIL_RE.match(email)
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write(form %{"username":"", "email":"","error_username":"", "error_password":"", "error_verify":"", "error_email":""})
def post(self):
has_error = False
username = self.request.get('username')
password = self.request.get('password')
verify = self.request.get('verify')
email = self.request.get('email')
params = dict(username=username, email=email, error_username = "", error_password = "", error_verify = "", error_email = "")
if not valid_username(username):
params['error_username'] = "That is not a valid username"
has_error = True
if not valid_password(password):
params['error_password'] = "That is not a valid password"
has_error = True
elif password != verify:
params['error_verify'] = "Your passwords did not match"
has_error = True
if email != "" and not valid_email(email):
params['error_email'] = "That is not a valid email"
has_error = True
if has_error:
self.response.out.write(form % params)
else:
self.redirect("/welcome?username=%s" %username)
class WelcomeHandler(webapp2.RequestHandler):
"""docstring for welcome."""
def get(self):
username = self.request.get('username')
self.response.out.write("Welcome " + username + "!")
app = webapp2.WSGIApplication([
('/', MainHandler),
('/welcome', WelcomeHandler)
], debug=True)