-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
72 additions
and
8 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import cgi | ||
|
||
import re | ||
import webapp2 | ||
|
||
rot13_form = """ | ||
|
@@ -13,10 +14,10 @@ | |
signup_form = """ | ||
<h1>Signup form</h1 required="True"> | ||
<form method="post" action="/signup"> | ||
<div>Name: <input title="Please, provide your name" type="text" name="username" placeholder="John Doe"><span style="color: red">%(username_error)s</span></div> | ||
<div>Password: <input title="Please, provide a strong password" type="password" name="password"><span style="color: red">%(password_error)s</span></div> | ||
<div>Verify passowrd: <input title="Please, verify your password" type="password" name="verify"><span style="color: red">%(verify_error)s</span></div> | ||
<div>Your email: <input title="Please, provide your email address" type="email" name="email" placeholder="[email protected]"><span style="color: red">%(email_error)s</span></div> | ||
<div>Name: <input title="Please, provide your name" type="text" name="username" value="%(username)s" placeholder="John Doe"><span style="color: red">%(username_error)s</span></div> | ||
<div>Password: <input title="Please, provide a strong password" type="password" name="password" value=""><span style="color: red">%(password_error)s</span></div> | ||
<div>Verify password: <input title="Please, verify your password" type="password" name="verify" value=""><span style="color: red">%(verify_error)s</span></div> | ||
<div>Your email: <input title="Please, provide your email address" type="text" name="email" value="%(email)s" placeholder="[email protected]"><span style="color: red">%(email_error)s</span></div> | ||
<button type="submit">Sign up</button> | ||
</form> | ||
""" | ||
|
@@ -55,15 +56,77 @@ def rot13(self, text): | |
|
||
|
||
class Signup(webapp2.RequestHandler): | ||
|
||
def get(self): | ||
return self.write_form() | ||
|
||
def post(self): | ||
self.redirect("/welcome") | ||
|
||
def write_form(self, username_error="", password_error="", verify_pass_error="", email_error=""): | ||
username = self.request.get("username") | ||
password = self.request.get("password") | ||
password_verify = self.request.get("verify") | ||
email = self.request.get("email") | ||
|
||
username_error = "" | ||
password_error = "" | ||
verify_pass_error = "" | ||
email_error = "" | ||
|
||
have_error = False | ||
|
||
is_username_valid = self.is_username_valid(username=username) | ||
is_password_valid = self.is_password_valid(password=password) | ||
is_password_verify_valid = self.is_password_verify_valid(password=password, password_verify=password_verify) | ||
is_email_valid = self.is_email_valid(email=email) | ||
|
||
if not is_username_valid: | ||
username_error = "Invalid username" | ||
have_error = True | ||
|
||
if not is_password_valid: | ||
password_error = "Invalid password" | ||
have_error = True | ||
|
||
if not is_password_verify_valid: | ||
verify_pass_error = "Passwords don't match" | ||
have_error = True | ||
|
||
if not is_email_valid: | ||
email_error = "Invalid email" | ||
have_error = True | ||
|
||
if have_error: | ||
self.write_form( | ||
username=username, | ||
email=email, | ||
username_error=username_error, | ||
password_error=password_error, | ||
verify_pass_error=verify_pass_error, | ||
email_error=email_error | ||
) | ||
else: | ||
self.redirect("/welcome?username=" + username) | ||
|
||
|
||
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$") | ||
def is_username_valid(self, username): | ||
return username and self.USER_RE.match(username) | ||
|
||
PASS_RE = re.compile(r"^.{3,20}$") | ||
def is_password_valid(self, password): | ||
return password and self.PASS_RE.match(password) | ||
|
||
def is_password_verify_valid(self, password, password_verify): | ||
return password == password_verify | ||
|
||
EMAIL_RE = re.compile(r"^[\S]+@[\S]+.[\S]+$") | ||
def is_email_valid(self, email): | ||
return not email or self.EMAIL_RE.match(email) | ||
|
||
def write_form(self, username="", email="", username_error="", password_error="", verify_pass_error="", email_error=""): | ||
self.response.headers['Content-Type'] = 'text/html' | ||
self.response.out.write(signup_form % { | ||
"username": username, | ||
"email": email, | ||
"username_error": username_error, | ||
"password_error": password_error, | ||
"verify_error": verify_pass_error, | ||
|
@@ -73,7 +136,8 @@ def write_form(self, username_error="", password_error="", verify_pass_error="", | |
|
||
class Welcome(webapp2.RequestHandler): | ||
def get(self): | ||
return self.response.out.write("Welcome") | ||
username = self.request.get('username') | ||
return self.response.out.write("Welcome, " + username) | ||
|
||
|
||
app = webapp2.WSGIApplication([ | ||
|