diff --git a/main.py b/main.py
index fe561c8..055f52e 100644
--- a/main.py
+++ b/main.py
@@ -1,5 +1,6 @@
import cgi
+import re
import webapp2
rot13_form = """
@@ -13,10 +14,10 @@
signup_form = """
Signup 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([