Last active
January 4, 2017 19:44
-
-
Save cstrachan88/4951413 to your computer and use it in GitHub Desktop.
Udacity course CS253 - Web Development
Problem Set 2 - 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Courtney S. - Udacity CS253 - Web Development | |
import webapp2 | |
import cgi | |
import re | |
form1=""" | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Unit 2 Signup</title> | |
</head> | |
<body> | |
<h2>Signup</h2> | |
<form method="post"> | |
<label>Username <input type="text" name="username" value="%(username)s"> | |
<span style="color: red">%(e_name)s</span></label> | |
<br> | |
<label>Password <input type="password" name="password"> | |
<span style="color: red">%(e_pass)s</span></label> | |
<br> | |
<label>Verify Password <input type="password" name="verify"> | |
<span style="color: red">%(e_verify)s</span></label> | |
<br> | |
<label>Email (optional) <input type="text" name="email" value="%(email)s"> | |
<span style="color: red">%(e_email)s</span></label> | |
<br> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> | |
""" | |
form2=""" | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Unit 2 Signup - Welcome</title> | |
</head> | |
<body> | |
<h2>Welcome, %s!</h2> | |
</body> | |
</html> | |
""" | |
def escape_html(s): | |
return cgi.escape(s, quote = True) | |
USER_RE = re.compile(r"^[a-zA-z0-9_-]{3,20}$") | |
PASS_RE = re.compile(r"^.{3,20}$") | |
EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$") | |
def valid_name(s): | |
return USER_RE.match(s) | |
def valid_pass(s): | |
return PASS_RE.match(s) | |
def valid_email(s): | |
if s == "": | |
return True | |
else: | |
return EMAIL_RE.match(s) | |
def match_pass(p1, p2): | |
return p1 == p2 | |
class Signup(webapp2.RequestHandler): | |
def write_form(self, username="", email="", | |
e_name="", e_pass="", e_verify="", e_email=""): | |
self.response.out.write(form1 % {"username": escape_html(username), | |
"email": escape_html(email), | |
"e_name": e_name, | |
"e_pass": e_pass, | |
"e_verify": e_verify, | |
"e_email": e_email}) | |
def get(self): | |
self.write_form() | |
def post(self): | |
user_name = self.request.get('username') | |
user_pass = self.request.get('password') | |
user_verify = self.request.get('verify') | |
user_email = self.request.get('email') | |
name = valid_name(user_name) | |
password = valid_pass(user_pass) | |
verify = valid_pass(user_verify) | |
email = valid_email(user_email) | |
e_name = '' | |
e_pass = '' | |
e_verify = '' | |
e_email = '' | |
if not name: | |
e_name = 'That is not a valid name' | |
if not password: | |
e_pass = 'That is not a valid password' | |
if not match_pass(user_pass, user_verify): | |
e_verify = 'The two passwords do not match' | |
if not email: | |
e_email = 'That is not a valid email' | |
if password and (not e_verify) and name: | |
self.redirect('/welcome?username=%s' % user_name) | |
else: | |
self.write_form(user_name, user_email, e_name, e_pass, e_verify, e_email) | |
class WelcomeHandler(webapp2.RequestHandler): | |
def get(self): | |
username = self.request.get('username') | |
self.response.out.write(form2 % username) | |
app = webapp2.WSGIApplication([ | |
('/', Signup), | |
('/welcome', WelcomeHandler) | |
], debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot! I didn't figure out about redirecting - self.redirect ( '/ welcome username =% s?'% User_name). Path differ from mapping handler - I stucked with it.
By the way: this code permits registration with incorrect email - it's wrong behavior.
By the way 2: style of registration page you can copy from instruction page - right click - view source code.
Good job.